Help for Crédit card checker

Hi everybody, can you help me for this exercise, i think an error existing in my code, but impossible to find it.
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];

function validateCred(array) {
var modulo = 10;
let noLastnumber = array.pop();//on isole le dernier element
//console.log(noLastnumber);

for (var i = array.length-1; i > 0 ; i-=2) {

array[i] *= 2;//On multiplie *2 les nb impairs
 console.log(array[3]);
if (array[i] > 9){
array[i] -= 9//On soustrait 9 les nb>9

} else {
array[i]
}
console.log(array[3]);
//console.log(array);
// var element = array[i];//inversion du sens de l’array sans le dernier element
console.log(array);
//return array
let initialValue = 0; //Ne pas oublier car le tableau inversé débute à -1 dans l’index de l’array
let sumTotale = array.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue + noLastnumber //addidtion de la somme de l’array + le premier chiffre retiré
);
console.log(sumTotale);
let moduloResult = sumTotale % modulo;
console.log(moduloResult);
if (moduloResult === 0) {
return true
} else {
return false
}
console.log(moduloResult);
}
}

console.log(validateCred(valid1));
console.log(validateCred(valid2));
console.log(validateCred(valid3));
console.log(validateCred(valid4));
console.log(validateCred(valid5));

Hey @montgomeryburns !

Do you mind providing more detail? What makes you think there’s an error? Is your code behaving in a way then what’s expected? IF so, how is it behaving and what is the expected outcome?

Screen shots may help.

Michael

Hi, this is the credit card checker project in javascript course. There the result returned should be “true” to all the console.log.

What is your code returning so far?

I don’t get a lot of chances to write/speak French so i’m going to take this opportunity to try!

Here is the exercise btw: https://www.codecademy.com/projects/practice/credit-card-checker

En lisant les instructions de l’exercice, je pense que ta fonction fait trop de choses en même temps.

Tout d’abord, il faut que tu multiplies le tableau comme le précisent les instructions, puis tu fais la somme des éléments du tableau.

Actuellement, ta fonction fait la somme du tableau à chaque itération de la “for loop”.

En plus, une autre façon de dépanner ta fonction est d’utiliser les “Snippets” de Google Chrome. Voici un lien :Sources panel overview - Chrome Developers

Thank you. I will try again.

Thank you very much. :slightly_smiling_face:

Another question. I had corrected my code with your helps and it work. But i have a problem because my arrays are modified (for example some numbers are doubled). that could be a problem in the next step of the project. How to avoid the array modification here?

My code :slightly_smiling_face:
function validateCred(array) {
var modulo = 10;

for (var i = array.length-2; i >= 0 ; i-=2) {
array[i]*=2;
if (array[i]>9){
array[i] -= 9;
}
}
//console.log(array);
let initialValue = 0; //Ne pas oublier car le tableau inversé débute à -1 dans l’index de l’array
let sumTotale = array.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue //addidtion de la somme de l’array + le premier chiffre retiré
);
//console.log(sumTotale);
let moduloResult = sumTotale % modulo;
console.log(array);
//console.log(moduloResult);
return moduloResult === 0

};

Try creating a tempArray and then use this nifty trick:

 const tempArray = JSON.parse(JSON.stringify(array));
1 Like

Just like @mdwiltfong mentioned, you could make a copy of the array and then modify the copy instead of the original.

function validateCred(card) {
  var array = Array.from(card); // makes a new array that is a copy of card
  var modulo = 10; 
2 Likes

It works, thank you very much.

1 Like

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