# Stuck in Luhn Algoritme

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

My understanding of what the lesson and the Luhn algorithm require:

1. return true if array is a valid credit card
2. don’t mutate original array
3. validate using Luhn algorithm:
1. right-most digit is the “check digit”. Ignore this digit for steps 2 and 3.
2. iterating right to left, every other digit is doubled
3. if doubled digit is greater than 9 after doubling, subtract 9
4. sum up all digits in the credit card number, including the check digit
5. if the sum modulo 10 is 0, the number is valid (so the sum should be divisible by 10)

So I would use `.pop()` to remove the check digit from the array and save it for later, `.reverse()` to make it easier to iterate with a positive index, and in the `for` loop I would make the step `i = i + 2` to only work with the odd-positioned digits, which you have to double. And also have an `if` statement to check if doubling the digit makes it greater than 9.

Hope it helps!

Hi Irlfed

Thank you for your feedback. Even if I keep myself to this exercise-guidelines, I am still stuck with the same problem. I need to double the number if the index is odd (in the case you pop()).

How can I only select odd index numbered values? IndexOf(x) of my for loop doesn’t seem to do the trick.

const validateCred = (array) => {
array.pop();
for (let i = array.length - 1; i >= 0; i–){
if(array[i] > 9) {
(array[i]*2) - 9
};
if(array[i] <= 9) {
array[i]*2
};
}
console.log(array);
}

Kind regards

Yannick

If an odd number comes every second number, you could always decrement by 2. I don’t want to give exactly how, and you need to take into account the lengths of the arrays. This is just meant to be hinting.

1 Like

Like code neutrino said, you don’t have to go one by one in your for loop.

``````for (let i = array.length - 1; i >= 0; i–)
``````

that third part, the `i--`, could easily be `i = i - 2` and it would work too!

1 Like