Hi,
I am trying to make a function that counts , the number of times an item in an array is repeated , i have been able to do so but , when i console it out, i get repeat values like β
20:02:02.069 practice_2.js:30 a 4
20:02:02.070 practice_2.js:30 b 3
20:02:02.070 practice_2.js:30 b 3
20:02:02.070 practice_2.js:30 a 4
20:02:02.070 practice_2.js:30 a 4
20:02:02.070 practice_2.js:30 b 3
20:02:02.070 practice_2.js:30 z 1
so the values keep repeated themselves like a -4 b -3 etc, also when i try and print them outside the outer loop despite the fact that testStr is on outside scope it prints undefined β¦
Note - Is there any solution in not so difficult higher order function like filter reduce etc β¦ Thanksβ¦
Below is my code -
function permAlone(str) {
"use strict";
var i, j, count;
var tr = false;
var testStr = str.split("");
for ( i = 0; i < testStr.length; i++) {
var count =0;
for(j = 0; j < testStr.length; j++) {
if(testStr[i] === testStr[j]) {
tr = true;
count++;
}
}
}
if(tr) {
console.log(testStr[i],count);
}
// return str;
}
permAlone('aabbaabz');
