Hey Everyone
I am trying to solve the Luhn Algoritme exercise. I’m trying to make the whole formula myself. I’d rather have one i build myself than one i found on the internet that i don’t understand :D.
My idea is to select numbers based on their index for step on. So I need to select the numbers from left to right, always the second. Which means, the not-odd indices. Those I want to push in a my new variable leftRight. So I put in the code:
const validateCred = (array) => {
for (let i = 0; i < array.length; i++){
let leftRight = ;
if((array.indexOf(array[i])%2) !== 0){
leftRight.push(array[i]);
console.log(leftRight);
}
}
};
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
if i check this code with the above const valid4, leftRight should give [0, 1, 1, 4, 4, 4, 6, 2, 0], though I get as a result:
[ 0 ] [ 4 ] [ 4 ] [ 3 ] [ 4 ] [ 0 ] [ 8 ] [ 9 ] [ 0 ] [ 5 ];
I suppose my error lies in my IF-clause, but I cannot get why it wouldn’t work. Can someone explain what I am doing wrong trying to push the not odd numbers in a new array?
Thank you
Kind regards,
Yannick