JavaScript Project

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

// Add your functions below:

function validateCred(arr){
for (let i = 15; i = 0; i–){
if (i % 2 === 0) {
arr[i] = arr[i] *2;{
return arr[i];
if (arr[i] > 9){
arr[i] = arr[i] - 9
return arr[i];
} else {
arr[i] = arr[i]
return arr[i];
}
}
} else {
arr[i] = arr[i]
return arr[i];
}
}
let sum = 0;
for (let i = 0; i < arr.length; i++){
sum += arr[i];
}
if (sum % 10 === 0 ){
return true;
} else {
return false;
}

  }

console.log(validateCred(valid1));

https://www.codecademy.com/practice/projects/credit-card-checker

The above is my code and a link to the assignment I’m working on.
My code is running, but it is printing out false, so I know it is not performing the beginning if statements correctly; how do I get these to run correctly? My code should be printing true, but it’s printing false (for the first array listed) because it’s not manipulating the values in the array.

For reference,

  1. Starting from the farthest digit to the right, AKA the check digit, iterate to the left.
  2. 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.
  3. Sum up all the digits in the credit card number.
  4. 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.

This is what my program is supposed to do.

A number of issues but I’ll give you the first an see if you can work through and figure out the rest.
Your for is wrong. i = 0 in the middle section should not be an assignment, it should be a condition to check if the loop should keep running like i >= 0. Because of this your for loop isn’t even being run and because there is no code to return a Boolean outside of it, your function always returns undefined.

Yes, but I want the last index to be checked to be 0; according to the assignment I have to check every value from the right to the left. How would I set it up to decrement from the 15th to the 0th index?

for(let i = 15; i >= 0; i–)

1 Like

Should I get rid of the return arr[i]'s?

Yes, they stop your loop from going over the whole array.

1 Like

Omg it’s running now, and outputing the right value; I’m so happy :slight_smile:

Thank you!

Just as a little side, your code is a lot more complex than it need to be. The first two conditions can be achieved with this code:

function validateCred(arr) {
    for (let i = arr.length - 2; i >= 0; i -= 2){
        arr[i] = (arr[i] * 2) % 9
    }
}

First we don’t need to do anything to the odd number indexes, so let’s just skip them. So start on 14 and decrement by 2 indexes each time. The arr.length - 2 could have been 14 but just did that because the rule didn’t actually explicitly say the length was always 16 digits.

Next for every other digit we will times it by 2 and rather check if it is over 9 and then subtract 9, we will take the mod 9 of it which will do the same thing.

Thanks!

I figured there was something simpler, but I wanted to see how I did writing it on my own.

That’s fine, when you are trying to figure something out you will write code that is more complex than required. Once you’ve figured it out you will often see you’re doing things needlessly as the route needed is clearer in your head.

I just thought I’d share that because there was quite a simple solution and it can help to think in a different way when you see them for the future.

Definitely!

I would prefer that.

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

// Add your functions below:

function validateCred(arr){
for (let i = 15; i >= 0; i–){
if (i % 2 === 0) {
arr[i] = arr[i] *2;{
if (arr[i] > 9){
arr[i] = arr[i] - 9
} else {
arr[i] = arr[i]
}
}
} else {
arr[i] = arr[i];
}
}

let sum = 0;
for (let i = 0; i < arr.length; i++){
sum += arr[i];
}
if (sum % 10 === 0 ){
return true;
} else {
return false;
}
}

console.log(validateCred(valid1));

let batch2 = ;

function findInvalidCards(batch){
for (let i = 0; i < batch.length; i++){
return validateCred(arr);
if (return === false){
batch2.push(batch[i]);
}

}
}

Here is my new code. It runs up until let batch2 = ; …

The instructions are " Create another function, findInvalidCards() that has one parameter for a nested array of credit card numbers. The role of findInvalidCards() is to check through the nested array for which numbers are invalid, and return another nested array of invalid cards."

I’m not sure how to pull the value from the previous code into my new code which will run through the nested array batch

Ok, so one issue you will face is that your validateCred function currently changes the array it is passed in, meaning when you try return it from your findInvalidCards method you will not be returning the original card number.

You should declare batch2 in the findInvalidCards function and return it. Also in findInvalidCards you are returning in your for loop again, so it will only do the first item

function findInvalidCards(batch){
let batch2 = ;
return validateCred(arr);

for (let i = 0; i < batch.length; i++){
if (return === false){
batch2.push(batch[i]);
}

}
}

okay like this? Hmm it’s not running at all though.

Yes to the batch2.
No to the return statement.
You still need validateCred in your for loop but instead of returning the result of it store it and the check if it === false. After to for loop return batch2

function findInvalidCards(batch){
let batch2 = ;
let issue = return validateCred(arr);

for (let i = 0; i < batch.length; i++){
if (value === false){
batch2.push(batch[i]);
}

} return batch2;
}

how do I set return validateCred(arr) to a value?

function findInvalidCards(batch){
    let batch2 = [];

    for (let i = 0; i < batch.length; i++){
        let isValidCred = validateCred(batch[i])
        if (!isValidCred){
            batch2.push(batch[i]);
        }
    }
    return batch2;
}

My bad description, I’m on my phone. By set I mean assign.
So we create batch2 to store all the invalid cards in.
We loop over the passed in cards in batch.
For each card in batch we use validateCreds and store the value in isValidCred.
We check if isValidCred is false.
If it is false we put the value in batch2.
Finally after looping over everything in batch we return batch2.

Technically we don’t have to use isValidCred and could just call validateCred directly in the if statement. But this is clearer to follow and explain.