Credit Card Checker

Hi all,
I am currently doing the Credit Card Checker task.
I am at the initial step, where I need to check the validity of the code, by multiplying every-other number by 2, without the first one.

I first want to mulptily every other number by two. If the number is greater than 9 to subtract 9 from it and return the sum of those values.
This task poses a challange fro me because if I have a if statement in a loop, or a loop inside of a loop, I get confused very quickly what is executed when.

This is my code so far:

//Store my values in those two variables:
let doubleNumberSum = 0;
let singleNumberSum = 0;
// Create a function to check the card number:
const validateCred = arr => {
  // As specified in the brief, I reverse the array:
  arr.reverse();
  // Check if I have done it:
  console.log(arr);
  // Create a loop to go trough every other number wihtout the first one aka arr.length-1
  for (let i = 0; i < arr.length - 1; i += 2) {
    // I multiply each number by two:
    arr[i] * 2;
    // If the number is greater than 9, 9 is subtracted from it.
    if (arr[i] > 9) {
      return arr[i] - 9;
    }
    // I sum all the numbers(that are multiplied by 2)
    doubleNumberSum += arr[i];
    console.log(doubleNumberSum);
  }
};

From the console I get:


So the function does the addition of numbers, but doesn’t do the multiplication by two.

Hey tsetsko, how are you doing?

In your code you have this part:

  // Check if I have done it:
  console.log(arr);

From the image you posted is visible what is the array reversed.

Now, if I remember correctly in the exercise it’s asked, in a very confusing way, to ignore the check digit, which is the last value of the normal array, or the first value in case you reversed it.

This is important, because depending on what strategy you follow your for loop is structured differently. In your case if you must ignore the first digit, you should not start the for loop from i=0.

Also, I believe it was asked to do not modify the original array, so you should implement a way to create a copy.

Finally, if you use the return keyword means that you want to exit the function whenever the return is reached, whatever the other code or loops the current state of the function is in. In my opinion you should remove it and change it with something else, I can’t go in details because it really depends from your strategy. However, as an example:
Let’s say you’d use a classic map to copy the original array to copy, you can simply say:

    if (arrCopy[i] > 9) { //arrCopy[i] is greater than 9
      arrCopy[i] -= 9; //subtract 9 to the current value of arrCopy[i]
    }

Hopefully this is clear enough to solve your question. :slight_smile:

Hi,
Thank you for the reply.
I am trying to solve the problem first. After that I will worry about not mutating the original array.
So I changed my for look to account for not taking the first reversed number and also work till the end.
The thing I don’t understand is why my arr[i] doesn’t double? For some reason arr[i] * 2 doesn’t do anything and therefore the if statement does’t initiate because there isn’t a number greater than 9.

// Add your functions below:
//Store my values in those two variables:
let doubleNumberSum = 0;
let singleNumberSum = 0;
// Create a function to check the card number:
const validateCred = arr => {
  // As specified in the brief, I reverse the array:
  arr.reverse();
  // Check if I have done it:
  console.log(arr);
  // Create a loop to go trough every other number wihtout the first one aka arr.length-1
  for (let i = 1; i < arr.length; i += 2) {
    // I multiply each number by two:
    arr[i] * 2;
    console.log(arr[i]);
    // If the number is greater than 9, 9 is subtracted from it.
    if (arr[i] > 9) {
      arr[i] - 9;
    }
    // I sum all the numbers(that are multiplied by 2)
    doubleNumberSum += arr[i];
    //console.log(doubleNumberSum);
  }
};

Hey, note that I forgot to write the word “not”, in my previous post:

you should not start the for loop from i=0 .

But I see you understood that anyway, good. :wink:

I am trying to solve the problem first. After that I will worry about not mutating the original array.

Alright.

The thing I don’t understand is why my arr[i] doesn’t double?

Very simple. You’re not doubling it. If you notice in my example to subtract 9 I used the operator -= not just -. The same for the multiplication operator *, you’re missing an equal sign = to assign the new value to the variable.

You can also not use this form if you find it confusing and simply write it in the extended way:

myVar =  myVar + 1;

Cheers,

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