My validateCred() function works for every array except for valid3, invalid3, and mystery1…these return NaN and I can’t figure out why. I’d appreciate any help. Here’s the code:
let sumTot = 0;
let validateCred = (a) => {
for (let i = 15; i >= 0; i–) {
if (i % 2 === 0 && a[i] < 5) {
sumTot = sumTot + (a[i] * 2);
//console.log(sumTot);
} else if (i % 2 === 0 && a[i] >= 5) {
sumTot = sumTot + ((a[i] * 2) - 9);
//console.log(sumTot)
}
if (i % 2 !== 0) {
sumTot = sumTot + a[i];
//console.log(sumTot);
}
} if (sumTot % 10 === 0) {
return ‘valid’;
} else {
return ‘invalid’;
}
};
console.log(validateCred(valid1));
It seems to work fine for every array except those three.
First time posting here, so not sure why only a portion of the code in the grey box there.
You have to highlight the whole text and press </>. Or conversely, press </> and insert in-between the ‘’’ marks.
@system1440254841 Welcome to the forums!!
Have you thought of all the edge cases? Are you sure all the test arrays are going to be 15 numbers long? I can see that being an issue if your loop is set to 15 and there’s a different length of number in the test.
I’d also put in log statements at key junctures just to make sure the inputs and returns are of the type you need them to be.
1 Like
Those arrays are actually shorter. Good thinking.
Thank you!
1 Like
No problem!
I actually never went through these modules. But it’s a common thing when you’re working on your own projects to think of all the ways your code can break (from my own painful experience
)… that’s what gave me the thought.