Why Scope not working here?

Why Scope not working here?
0

#1

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');

#2

Below is one possible solution. I changed the function named to better reflect what the function is doing. I did not use a higher order function to solve the problem.

function countLetters(str) {
  var seenAlready = {};
  for (var letter of str)
    seenAlready[letter] = seenAlready[letter] ? seenAlready[letter]  + 1 : 1;
  for (var prop in seenAlready)
    console.log(prop + ' = ' + seenAlready[prop]);
}

countLetters('aabbaabz');

Yields the following:

a = 4
b = 3
z = 1


#3

@RandellDawson - Thanks, i am struggling a bit to understand what is going on with your code , my bad , i do not know the use of for loop with of and in , i am just thinking where i should practice to develop such skills :slight_smile:

Note - what mistake i was making with my code , why i was not getting a condensed result like yours though i was able to count the number of times an alphabet was present in the array ?


#4

The code I originally posted was a more condensed version making use of the for of syntax available in Javascript.

Here the same logic, without using the for of statement:

function countLetters(str) {
  var seenAlready = {};
  for (var i = 0; i < str.length; i++) {
    var letter = str[i];
    if (seenAlready[letter])
      seenAlready[letter] += 1;
    else
      seenAlready[letter] = 1;
  }
  var props = Object.keys(seenAlready); // creates an array of the keys in seenAlready
  for (var j = 0; j < props.length; j++) {
    var prop = props[j];
    console.log(prop + ' = ' + seenAlready[prop]);
  }
}

#5

Your code was not counting any letters in the array. I used your original code and put in some console.log statements, so you can see what the values of different variables as your code executes.


#6

Hello,
about your code, you get testStr[i] as undefined cause i at that point is already out of your array range. if your array length is 10 your loop will stop when i is also 10, but since array index starts with 0 your max index is 9 meaning array[10] will be out of range and will return undefined.


#7