As per this response on the above post by mirja_t…
" A common and simple approach would be this:
- Write a function (leave the body empty for now) that has a parameter for the card array
- In the function body copy the array in order to leave the input array unaltered (e.g. slice() method as mentioned in another post or spread syntax )
- Reverse the copied array in order to iterate from end to start (reverse() method)
- Write a new variable and assign it to an empty array
- Write a for loop that iterates over each digit in the copied and reversed array
- Inside the for loop, find out which index is odd (because that omits the check digit)
- If the index is odd, double the digit (if statement)
- If the result is larger than 10, subtract 9 (same, if statement)
- Push the result to the new array – also the unaltered digits from the even indexes of the for loop
- Sum up each digit from the new array (reduce() method or another loop)
- Find out if there is a rest if you divide the sum by 10 (modulo operator)
- Return true from the function is there is no rest value. "
Before reading post I found that my code happened to be following these steps. I have since gone in a different direction but I just wanted to know how I could succeed if I persevered in my original route. I mainly had problem with step 6. It was iterating through the wrong numbers. What are your tips for step 6 in particular?
Thanks.
To check whether i
is even, you could do something like
(i % 2 === 1)
which would be true
when i
is odd, and would be false
when i
is even
Thanks for responses so far. Oops. I forgot to include my code. I hadn’t changed the sum to reduce yet. But good to know if I was going along the right lines as per the steps detailed above or whether I was way off. I may have been taking the copy, pop then reverse process literally instead of the more elegant way in the solution.
const validateCred = arr => {
let checkCard = […arr];
console.log(checkCard);
endDigit = checkCard.pop();
console.log(endDigit);
console.log(checkCard);
let reverseCard = checkCard.reverse();
console.log(reverseCard);
let numbers = ;
let dbl = 0;
for (let i = 0; i < reverseCard.length; i++) {
let n = reverseCard[i];
if (n % 2 !== 0) {
dbl = n *= 2;
if (dbl > 9) {
numbers.push(dbl -= 9);
} else {
numbers.push(n);
}
} else {
numbers.push(n);
}
console.log(numbers);
}
let sum = 0;
for (let a = 0; a < numbers.length; a++) {
sum += numbers[a];
}
if ((sum + endDigit) % 10 === 0) {
return true;
} else {
return false;
}
}
Most of it looks fine. But check the inside of the first for-loop.
I think you should have i % 2
instead of n % 2
to check if the index is odd or even, instead of if the number [at that index] is odd or even.
and you don’t need the *=
and -=
in situations where you don’t intend to change the value of the variable.
if (i % 2 !== 0) {
dbl = n * 2;
if (dbl > 9) {
numbers.push(dbl - 9);
You’re missing a let
or const
in front of the first endDigit
, otherwise you’re modifying a global variable.