I need to write a function that takes an array element and a different array string as parameters, converts them into strings, and then counts the number of duplicate elements.
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'], []));
It is important that I find a way to achieve the same result using just a single parameter instead of two like 'm1'.
The expected output is: 1 1 0 2 6 0 it's not a number