JavaScript CreditCard Checker Challenge Project help

I am currently working on the creditcard checker challenge in JavaScript. Right now I am just focusing on step 3, where I need to create a function in order to just validate whether it is a valid card number or not. I am as far as returning the new numbers after multiplying by two then subtracting 9 IF the new value is greater than 9. (At this point of typing this up I realized it is only supposed to double every other digit, so now that is what I am struggling with). Here is my current code:

function validateCred(arr) {
    let newArr = arr.slice(-1);
    let newVal = 0;
    for (i = arr.length - 2; i >= 0; i--) {
        
        newVal = arr[i] * 2;
        
        if (newVal > 9) {
          let subNine = newVal - 9;
          newArr.unshift(subNine); 
        } 
        else {
          newArr.unshift(newVal);
        }
        
    }
    return newArr;
}

const valid1 = [4, 5, 3, 9, 6, 8, 9, 8, 8, 7, 7, 0, 5, 7, 9, 8]

console.log(validateCred(valid1));

In order to only double every other digit, I feel like I need to make another for loop within the current on, but I am not sure at what point to put it. Am I right about the for loop or will I need to do something completely different. I used the numbers here as a test array: cc validator v2@2x

One loop will be enough. Determine the length to decide which element to start from, first or second, then every other element after that.

[ :, ., :, ., :, ., :, . ]  // length 8; even
  ^     ^     ^     ^

[ ., :, ., :, ., :, . ]     // length 7, odd
     ^     ^     ^

When the length is even, start from the first element; when it is odd, start from the second.

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