Credit card checker- questions about modulo

Hello everyone! I am STILL working on the credit card checker and I would like some help assuring my code.
On step 3 of the credit card checker, on item #4 it reads :
“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.”
I get that modulo is the remainder of number divided by 10, but the first three numbers have a remainder of 3, 3 , 0 (73 /10 = 7.3, 60/ 10 = 6.0). Which would make the first two numbers false. when I run my code it is showing that all numbers are valid in the console and I am not sure if this was something in the challenge they meant to do, or I am completely wrong :frowning: I am performing the challenge step by step and checking as I go ( writing things down and doing my own calculations to make sure I am on the right path). Just an fyi I started on step 4 but backtracked so please ignore const findInvalidCards(), that will be a question for another time: Thank you!

// 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];



let sum = 0;
let newArray = [];

const validateCred = array => {
  let newArray = Array.from(array).map(modifyIfIndexIsOdd) // copy the array, goes over each array
  
  newArray.reverse(); // reverses the new array

   newArray.reduce((total, current) => {
     return total + current; // sums the totals
   });

   if (sum % 10 === 0) {
    return 'Valid';
  } else {
    return 'Invalid';
  }
}

//Iterates through the numbers and checks the numbers
function modifyIfIndexIsOdd(input, index) {
    if ((index % 2) == 1) {
      let x = input * 2;
      if (x > 9) {
        x = x - 9;
      }
      return x;
    }
    else {
      return input;
    }
}

// const findInvalidCards() {
//    let validCardNums = [];
//    let inValidCardNums = [];
// }

  for (i = 0; i < batch.length; i++) {
     console.log(validateCred(batch[i]) );
  }

You didn’t declare the variable sum in your function.

It should be here:

  const sum = newArray.reduce((total, current) => {

In the version of the code you have, the modifications (multiplying stuff at odd index by 2 and maybe subtracting 9) should be done using the reversed array.
So the .map should be later in your code.

const validateCred = array => {
  let newArray = Array.from(array) // copy the array
  
  newArray.reverse(); // reverses the new array
  let modifiedArray = newArray.map(modifyIfIndexIsOdd);  // changes appropriate digits

  const modifiedArray = newArray.reduce((total, current) => {
     return total + current; // sums each number
  });

  if (sum % 10 === 0) {
    return 'Valid';
  } else {
    return 'Invalid';
  }
}

Also, I’m not sure whether the project instructions what you to return 'Valid' or 'Invalid',
or whether it should be true or false.

Hey @janbazant1107978602, The project instructions are to instruct wither true or false, I was adding something different is all. With your suggestions my code worked? I am still trying to get the math on this project ( which is easy, it is just I am still a bit confused here and there). I would like to ask you a question on step 4. I am trying to check ( as I said earlier) and for some reason when I console.log my function I get something like “function invalidNums” I know this is a beginner question but how can I call my function to check my answer. Any other time I have been doing this it works fine, but I feel like I am missing something.

let sum = 0;
let newArray = [];

const validateCred = array => {
  let newArray = Array.from(array) // copy the array
  
  newArray.reverse(); // reverses the new array
  let modifiedArray = newArray.map(modifyIfIndexIsOdd);  // changes appropriate digits

  const sum = newArray.reduce((total, current) => {
     return total + current; // sums each number
  });


  if (sum % 10 === 0) {
    return true;
  } else {
    return false;
  }
}

//Iterates through the numbers and checks the numbers
function modifyIfIndexIsOdd(input, index) {
    if ((index % 2) == 1) {
      let x = input * 2;
      if (x > 9) {
        x = x - 9;
      }
      return x;
    }
    else {
      return input;
    }
}

const findInvalidCards = (nestArray) =>{
  let invalidNums = [];
  for( let i = 0; i < nestArray.length; i++){
    if(!validateCred(nestArray[i])){
      invalidNums.push(nestArray[i])
    }
  }
  return invalidNum
}





  // for (i = 0; i < batch.length; i++) {
  //    console.log(validateCred(batch[i]) );
  // }

I also wanted to throw a huge thank you for helping me, I appreciate it!

Yes, there’s mistakes in my code on this line

  const modifiedArray = newArray.reduce((total, current) => {

which should be

  const sum = modifiedArray.reduce((total, current) => {

corrcted:

const validateCred = array => {
  let newArray = Array.from(array) // copy the array
  
  newArray.reverse(); // reverses the new array
  let modifiedArray = newArray.map(modifyIfIndexIsOdd);  // changes appropriate digits

  const sum = modifiedArray.reduce((total, current) => {
     return total + current; // sums each number
  });

  if (sum % 10 === 0) {
    return 'Valid';
  } else {
    return 'Invalid';
  }
}

You can check by doing:

console.log(validateCred(invalid1));  // should be false
console.log(validateCred(invalid2)); // should be false

and so on.
And

console.log(validateCred(valid1));  // should be true
console.log(validateCred(valid2));  // should be true

and so on.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.