I'm trying to return a value inside a map function before reaching the default return value at the end of the function.
I noticed the map function is not returning a value for the validateSequence function, but a simple for loop can.
function validateSequence(sequence) {
const modifers = ['-', 'm', 'b', 'i'];
sequence.map((seq) => {
if(!modifers.includes(seq)) {
return false; // want to return false
};
});
return true;
};
validateSequence(['Z','e','p']); // returns true
function validateSequence(sequence) {
const modifers = ['-', 'm', 'b', 'i'];
for(let i=0; i<sequence.length; i++) {
if(!modifers.includes(sequence[i])) {
return false;
};
};
return true;
};
validateSequence(['Z','e','p']); // returns false
I expect the map function to return false before it reaches the default return value, true. I know that the map function is executed before reaching the default return value true, so why isn't it returning false?