Looping through a string in a nested array

Looping through a string in a nested array
0

#1

Hi,
I want to loop through each letter in a string in a nested array, and check if one of them matched the unicode.

The array would be for example [ [ ‘ABC’, 65 ], [ ‘HGR’, 74 ], [ ‘BYHT’, 74 ] ]

So for example I would like to check if A, B, or C’s unicode is 65 and then return true or false, and then do the same for H, G, R and 74 etc.

How can I do this? Is it less glitchy to use map or another higher order function than the for i, for j for loop form?

Thanks


#2

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:


#3

Hi,
So I’ve done quite a bit of the JavaScript things and finished basic algorithms but the intermediate ones are eluding me, so in the meantime, I’m going some 7 and 6 kyu challenges on CodeWars. These too are eluding me but I’ve learned a lot by doing them.

I’m able to make the sort of algorithm I want (slightly different return value) if there is just one string and one number.

function test(word, num) {
  let count = 0;
  for (var i = 0; i < word.length; i++) {
    for (var j = 0; j < word[i].length; j++) {
      if (word[i][j].charCodeAt(j) === num) {
      count ++;
      if (count >= 1) {
        return 'Winner!';
      }
  }
  else {
  return 'Loser!';
}
}
}
}
test("ABCD", 60);

However I can’t seem to work out how to get it to work with an argument like [[‘ABC’, 65], [‘HGR’, 74], [‘BYHT’, 74]] testing each one. I’ve tried a few ways, but using charCodeAt will tell me it can’t read it, I just can’t work out how to do this function on each subarray while adding to the count.

Sorry if this doesn’t provide more information, I just want to specifically target the item at 0 of each subarray (the string will always come first, and the number second) but I can’t quite work out how.


#4

I’ve tried this:

let ticket = [ [ 'ABC', 65 ], [ 'HGR', 74 ], [ 'BYHT', 74 ] ]

function test(arr) {
  for (var i = 0; i < arr.length; i++) {
      
      for (var j = 0; j < arr[i].length; j++) {
          
          for (var k = 0; k < arr[i][j].length; k++) {
            //console.log(arr[i][j].charCodeAt(k));
            if (arr[i][j].charCodeAt(k) === arr[i][1]) {
              console.log(true);
            }
            else {
              console.log(false);
            }
            
          }
   }  }
}

test(ticket)

but it only checks the first letter against the number.


#5

Will the characters always be at position 0 and the number at position 1?

If so, how could you make this work with one less nested loop? What if you just removed the k for loop and did your logic inside j? What would that look like?

Good luck :slight_smile: