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
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.
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.
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