# Credit Card Checker Question 3

Really struggling with question 3 of the credit card checker project. Any help would be greatly appreciated!
Here is my code so far for Q3:

``````// Add your functions below:
const validateCred = valid1 => {
let valid1Test = []
valid1Test = valid1.slice()
valid1Test.pop()
valid1Test.reverse()
//Test
console.log(valid1Test)

for (let i = 0; i < valid1Test.length; i+=2) {
valid1Test[i] *= 2;
}
for (let i = 0; i < valid1Test.length; i++){
if (valid1Test[i] > 9) {
valid1Test[i] -=9   }}

//Test
console.log(valid1Test)

let sumTotal = valid1Test.reduce((a,b) => a + b, 0)
valid1Test = sumTotal % 10
if (valid1Test === 0) {
return true;
}else {
return false;

//Test
console.log(sumTotal)
console.log(valid1Test)

}}

//Test
validateCred(valid1)
``````

Can you be more specific in which part of question 3 your are struggling with?

the last part:

``````let sumTotal = valid1Test.reduce((a,b) => a + b, 0)
valid1Test = sumTotal % 10
if (valid1Test === 0) {
return true;
}else {
return false;
//Test
console.log(sumTotal)
console.log(valid1Test)
``````

when I run, both the sumTotal and valid1Test do not log to console…

Please review How do I format code in my posts?
I’ve edited your previous posts, so you can see the difference. 1 Like

A few things to consider:

• What does `return` do?
• Where are the `console.log()` statements located?
• Does your code correctly implement the Luhn algorithm?

Tip: Cleaning up your indentation, and making it consistent will make your code much easier to read and debug. Making no edits to your code other than formatting using an online code formatter, it appears like this:

``````// Add your functions below:
const validateCred = valid1 => {
let valid1Test = []
valid1Test = valid1.slice()
valid1Test.pop()
valid1Test.reverse()
//Test
console.log(valid1Test)

for (let i = 0; i < valid1Test.length; i += 2) {
valid1Test[i] *= 2;
}
for (let i = 0; i < valid1Test.length; i++) {
if (valid1Test[i] > 9) {
valid1Test[i] -= 9
}
}

//Test
console.log(valid1Test)

let sumTotal = valid1Test.reduce((a, b) => a + b, 0)
valid1Test = sumTotal % 10
if (valid1Test === 0) {
return true;
} else {
return false;

//Test
console.log(sumTotal)
console.log(valid1Test)
}
}

//Test
validateCred(valid1)
``````

Much easier to tell what is what and where.

2 Likes

Okay so I managed to get everything to work to console now…
However I would love some advice on substituting the hard coded ‘8’ for a variable so that I can use this function for the other cards.

Here is my code below (hopefully formatted correctly).

``````//Copy, remove last digit and reverse valid1 CC number

const validateCred = (valid1) => {
let valid1Test = [];
valid1Test = valid1.slice();
valid1Test.pop();
valid1Test.reverse();

//Test to see if number is reversed and last digit removed
console.log(valid1Test);
//Double every second number and -9 if result is > 9
for (let i = 0; i < valid1Test.length; i += 2) {
valid1Test[i] *= 2;
}
for (let i = 0; i < valid1Test.length; i++) {
if (valid1Test[i] > 9) {
valid1Test[i] -= 9;
}
}

//Test to produce CC number and check loop worked
console.log(valid1Test);

//Sum the valid1Test array and add last digit of valid1 then check validity using Luhn

let sumTotal = valid1Test.reduce((a, b) => a + b, 0);

let num1 = sumTotal + 8; //How can I change the '8' to something like valid1 ?
if (num1 % 10 === 0) {
console.log("True");
} else {
console.log("False");
}
//Test to see if num1 (the total sum of valid1Test is valid)

console.log(num1);

};
//Test
validateCred(valid1);

``````

Really appreciate the feedback and apologies if I am making stupid mistakes this is my first week learning JS!

Cheers,
Jordan

1 Like

No need to apologize. First thing, you put a single backtick before and after your code. You need three on the line before your code, and three on the line after your code.

Consider the line of code where you removed the value. When you use the `pop()` method, it `returns` the value that was removed:

``````const a = [1,2,3]
const b = a.pop() //removes and returns the last element - that returned value is assigned to b
console.log(b) //3
``````