I am doing an algorithm challenge for practice in JS. I have a program that runs through a loop, and when the condition is met, the function should return false. However, when the condition is met, the return doesn't work and the function always ends up returning true.
const isDiagonalLeftWristband = (band) => {
band.forEach((row, y) => {
row.forEach((item, x) => {
for(let i = 0; (i < band[y].length - x) && (i < band.length - y); i++) {
if (band[y][x] !== band[y+i][x+i]) {
console.log(false) //FALSE GETS OUTPUTTED MULTIPLE TIMES
return false;
}
}
})
})
return true;
}
const band3 = [
["A", "B", "C"],
["C", "Z", "B"],
["B", "C", "A"],
["A", "B", "C"]
];
console.log(isDiagonalLeftWristband(band3))
Output:
false //from console log
false //from console log
true //from function return statement
Any help would be greatly appreciated!