Credit Card Checker

Hi all,

I am working on the credit card checker project: https://www.codecademy.com/practice/projects/credit-card-checker

I have managed to get the result needed for step 3, validateCred()

My question is, at the bottom of the function, when I call the function and pass int he argument, nothing gets printed to the console. I have to wrap console.log around the call. Why does the code not print 0 (the expected result) to the console without console.log?

I know I have a lot of refactoring to do but I at least got to the expected result, next steps are to refactor. Code below:

const validateCred = (arr) => {
  arr.pop()
  arr.reverse();
  let newArr = []
  for (let i = 0; i < arr.length; i++) {
   if (i % 2 === 0 ) {
     arr[i] = arr[i] * 2;
     if (arr[i] > 10) {
       arr[i] = arr[i] - 9;
     }
   }
    newArr.push(arr[i]); 
  }
  
  let value = 0;
  for (let i =0; i < newArr.length; i++) {
    value += newArr[i];
  }
  
  let finalArr = value + 8 
  finalArr = finalArr % 10
  if (finalArr === 0) {
    return true;
  } else {
    return false;
  }
};



console.log(validateCred(valid1)); //Works
validateCred(valid1);  //Does not print true, print nothing```

Well the reason it is not logging anything is because you are not telling it to do so :stuck_out_tongue:

1 Like

Why are you expecting to print to the console without console.log()? Also you are mutating the original array with reverse(), you want to make a copy first than you can use reverse()

2 Likes

If you want the validateCred function to log anything you have to include it inside the function using console.log.

You could modify the last part of your function like so:

let verdict 
if (finalArr === 0) {
    verdict = true;
} else {
    verdict = false;
}
console.log(verdict); // this prints in the console
return verdict; // returns the value of verdict, you will need this in the next part of the assignment
1 Like

Thank you for the time and help!

Hi there,
I didn’t want to create a new topic so I am using this one. I hope it’s fine for everybody.

I also have some trouble with this exercise, I am on the task 4 to create findInvalidCard().
You will find my code below.

let validateCred = arr => {
  let sum;
  let sumArr = 0;
  for (i=arr.length-1 ; i>=0; i--){
    if(i % 2 === 0){
      sum=arr[i]*2;
      if (sum > 9){
        sum = sum - 9;
        sumArr = sumArr + sum;
      }else{
        sumArr = sumArr + sum;
      }
    }else{
      sumArr = sumArr + arr[i];
    }
  }
  console.log('The sum of the of all the digit is: ' + sumArr);
  if(sumArr % 10 === 0){
    console.log('This credit card is valid');
    return true;
  }else{
    console.log('This credit card is invalid');
    return false;
  }
}
validateCred(valid1);

let invalideCard;
let findInvalidCards = batch =>{
  invalideCard=[];
  for(i=0; i< batch.length; i++){
    validateCred(batch[i]);
    if (validateCred(batch[i])=false){
      invalideCard.push(batch[i]);
    }
  }
}
findInvalidCards(batch);
console.log(invalideCard);

The function, validateCred is working well when I am running it alone. I tested it on the different valid and invalid card numbers, and I got for all of the them the good answer.

But when I am adding the findInvalidCard one, I am getting this on the console:

The sum of the of all the digit is: 80
This credit card is valid
The sum of the of all the digit is: 80
This credit card is valid
/home/ccuser/workspace/credit-card-checker/main.js:30
for (i=arr.length-1 ; i>=0; i--){
            ^

TypeError: Cannot read property 'length' of undefined

I am a bit confused because it looks like that the first valid1 card number is working for both of the function but when arrived the card number valid2, it doesn’t work. And I can’t figure why.
I don’t understand why validateCred function is working fine but when I nested in findInvalidCard, it doesn’t work anymore.
Is anybody has feedbacks or suggestions please?
Thank you so much for your time and help.
Clement

If you don’t use some sort of declaration in your for loop statement then the variable will be global. That will cause a lot of problems because you have two different for loops using the same i variable

Thanks :slight_smile: ! I changed the second i with j and I don’t have this error again.
Thank you for your help!

That’s not really the lesson you should take from this though. Not using a variable declaration is bad syntax and will cause problems down the road. You should be doing let i = 0 inside the for loop statement as that will keep the scope of i to the for loop block.