mardi 29 septembre 2020

find the duplicate element count using array element and array string

I'm passing an array element and array string in a single function and convert into a string and find duplicate element count.

function count5numbers1(arr, arr1) {
  let m1 = arr.toString().match(/[5]/gi);
  let m2 = arr1.toString().match(/[5]/gi);
  if (m1 === null && m2 != null) {
    return m1 = 0;
  } else if (m1 != null) {
    return m1.length;
  } else if (m2 === null) {
    return m2 = "it's not a number";
  } else {
    return m2.length;
  }
}
console.log(count5numbers1([1, 2, 5, 43], [5]));
console.log(count5numbers1([1, 2, 3, 5], [5]));
console.log(count5numbers1([1, 2, 4, 2], [5]));
console.log(count5numbers1([2, 4, 54, 15], [5]));
console.log(count5numbers1([1, 5, 55, 555], [5]));
console.log(count5numbers1([6, 3, 2, 1], [5]));
console.log(count5numbers1(['notnumber,its a string'], []));

But I need to use a single parameter only m1 getting the same result.

The result will be: 1 1 0 2 6 0 it's not a number

Aucun commentaire:

Enregistrer un commentaire