 # Credit card checker invalid to valid

Here’s my take on step 7 of the credit card checker problem. You have to make an invalid card valid. I just generated a new array with random numbers and changed the last number so the sum of the array is divisible by ten. I just wanted to come up with the simplest solution that I know will definitely work every time.

``````const changeToValid = arr => {
let newArr = [];
let sum = 0;
for (let i = 0; i < arr.length; i++) {
let randomNum = Math.floor(Math.random() * 10);
newArr.push(randomNum);
sum += randomNum;
}
let remainder = sum % 10;
let last = newArr.length - 1;
if (newArr[last] - remainder < 0) {
newArr[last] += 10 - remainder;
} else {
newArr[last] -= remainder;
}
return newArr;
};
``````

I realized it’s probably good to preserve the first number for company identification. So here’s my solution.

``````const changeToValid = arr => {
let newArr = [];
let sum = 0;
for (let i = 0; i < arr.length; i++) {
let randomNum = Math.floor(Math.random() * 10);
if (newArr === undefined) {
newArr.push(arr);
sum = arr;
} else {
newArr.push(randomNum);
sum += randomNum;
}
}
let remainder = sum % 10;
let last = newArr.length - 1;
if (newArr[last] - remainder < 0) {
newArr[last] += 10 - remainder;
} else {
newArr[last] -= remainder;
}
return newArr;
};
``````