Hi,

I am working through the JavaScript ‘Full Stack Path’ and have just created the following algorithm to solve the credit card checker problem. https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-challenge-project-credit-card-checker/projects/credit-card-checker

Would someone be kind enough to review my code and let me know how I could improve? What other solutions could I have used to ensure every other element from end to start was manipulated as per the instructions? As you can see I used a boolean implement this as I couldn’t work out how I could use the modular operator on an array that was iterated over in reverse and could be of varying length.

Thank you! - Kit

**Question:**

Create a function,

`validateCred()`

that has a parameter of an array. The purpose of`validateCred()`

is to return`true`

when an array contains digits of a valid credit card number and`false`

when it is invalid. This function should NOT mutate the values of the original array.To find out if a credit card number is valid or not, use the Luhn algorithm. Generally speaking, an algorithm is a series of steps that solve a problem — the Luhn algorithm is a series of mathematical calculations used to validate certain identification numbers, e.g. credit card numbers. The calculations in the Luhn algorithm can be broken down as the following steps:

- Starting from the farthest digit to the right, AKA the check digit, iterate to the left.
- As you iterate to the left, every other digit is doubled (the check digit is not doubled). If the number is greater than
`9`

after doubling, subtract`9`

from its value.- Sum up all the digits in the credit card number.
- If the sum modulo
`10`

is`0`

(if the sum divided by`10`

has a remainder of`0`

) then the number is valid, otherwise, it’s invalid.Here’s a visual that outlines the steps. Check your function using both the provided valid and invalid numbers.

**My Solution:**

```
function validateCred(array) {
const output = [];
let double = false;
let sum;
// Loop over each element in array from last to first.
for (let i = array.length - 1; i >= 0; i--) {
// Process every other element.
if (double) {
let doubled = array[i] * 2; // Double value in array.
if (doubled > 9) {
// If 'doubled' is greater than 9, minus 9.
doubled = doubled - 9;
}
output.unshift(doubled); // Add processed value to 'output' array.
double = false;
} else {
output.unshift(array[i]); // Add unprocessed value to 'output' array.
double = true;
}
}
// Add up all elements in 'output' array.
sum = output.reduce((total, currentValue) => total += currentValue, 0);
// Check whether total mod 10 is equal to 0.
return sum % 10 === 0;
}
```