Credit card checker

const validateCred = (parM) => {
for(let i = 0; i < parM.length; i++){
if(parM[i] % 2 !== 0){

   console.log(parM[i] * 2);
  
}
}

};

how do I log my new array with the doubled odd numbers. kindly help.

Hello Martin,

First before running the for loop you will have to make a copy of the card because you cannot change the original card. I used the slice method for this.

You are on a good path with the modulo checker. And to double the value you can do something like this. copyofCard[i] = copyofCard[i] * 2;

Thank you so much janneslohmeijer! You have saved me a lot of time googling for a question I don’t even understand!! Be blessed! I really do appreciate.

const validateCred = (parM) => {
  
  let newArr = parM.slice(0);
  y = newArr.pop();
  newArr.reverse();
  for(let i = 0; i < newArr.length; i++){
    if((newArr.indexOf(newArr[i])+1) % 2 !== 0){
    
        newArr[i] = newArr[i]*2;
      if(newArr[i] > 9){
        newArr[i] = newArr[i] - 9;
      }
      
    }
    
    
    
    }
  console.log(newArr)
  const total = newArr.reduce((acc,currVal) => {
      return acc + currVal;
    })
    console.log(total + y );
    
  };

output is

[ 0, 8, 3, 1, 0, 8, 0, 9, 5, 7, 3, 9, 6, 1, 8 ]
76

the second last digit changes to 1 instead of remaining 5. Why is this? Where is the bug in my code. Those are valid card numbers and output should be 80. Instead my program is minusing 4 from the second last digit. Help is highly appreciated.

Hello, @martinnyagah51575630.

I formatted the code in your post. Please review this post for future reference: How do I format code in my posts?
This will allow others to easily copy/paste your code in their own environment, so they can test it. I’m having a look now :slightly_smiling_face:
You may be able to find the issue yourself by adding a few more console.log() statements to see what is going on.

This is the root of your problem. Think about what the value of each of these things is for each value of i (specifically when i reaches 13). You don’t need this level of complexity. This is the equivalent of using a person’s address to go to their home, and ask them for their address, but with a glitch. You then look them up by name, and accidentally go to the wrong house (address) belonging to someone else with the same name that appeared first in the phone book.

Hint:
if(i % 2 == 0) //this is all you need

Hi midlindner,

My code finally works. But I don’t understand why the hint you gave me surprisingly works. Would you mind explaining further?

if(i % 2 == 0)    vs   if((newArr.indexOf(newArr[i])+1) % 2 !== 0)
1 Like

Consider this more simple example:

const nums = [1, 2, 1, 2]

for(let i = 0; i < nums.length; i++) {
  console.log(`i = ${i}`)
  console.log(`nums[i] = ${nums[i]}`)
  console.log(`nums.indexOf(nums[i]) = ${nums.indexOf(nums[i])}\n`)
}

Output:

i = 0
nums[i] = 1
nums.indexOf(nums[i]) = 0

i = 1
nums[i] = 2
nums.indexOf(nums[i]) = 1

i = 2
nums[i] = 1
nums.indexOf(nums[i]) = 0

i = 3
nums[i] = 2
nums.indexOf(nums[i])= 1

Notice when i is 0 and when i is 1 that code similar to your original code nums.indexOf(nums[i]) is the same as i. However, when i is 2 and when i is 3 something different happens. Let’s break it down a little. When i is 2:

nums = [1, 2, 1, 2]
indexes: 0, 1, 2, 3
nums[2] is the second occurrence of the value 1
so, nums.indexOf(1) returns 0
Why? Because the indexOf() method returns the index of the first occurrence of the argument supplied.

The value of i in the for loop is all we need to access each value in the array of credit card number digits. Since you reverse the array, and we need to perform the double/subtract 9 if greater than 9 operation on every other digit beginning with the first (index 0) digit, the code in my hint accomplishes the task.