# validateCred function in Credit Card Checker exercise

The project I"m working on is here https://www.codecademy.com/practice/projects/credit-card-checker

Specifically I’m working on the validateCred function and I basically and producing the reverse outcome. In other words all the valid credit cards are coming back false and vice versa. Can anyone give me any advice on what is wrong with my code?

function validateCred(creditCard){
worksheet1 = creditCard
counter = worksheet1.pop()
reverseWorksheet = worksheet1.reverse()
for (i = 0; i < reverseWorksheet.length; i++){
if (reverseWorksheet.indexOf(reverseWorksheet[i]) %2 != 0){
oddIndex = reverseWorksheet[i] * 2
if (oddIndex > 9) {
counter += oddIndex - 9
} else {
counter += oddIndex
}
} else {
counter += reverseWorksheet[i]
}
}
if (counter % 10 === 0) {
return true
} else {
return false
}
}

Hello, @charleslonardo.

By removing the last digit with `pop()`, and then reversing the array, you are performing the doubling and subtracting 9 (if necessary) on the wrong digits. Try it on paper, and you’ll see. Also, in this line:

``````if (reverseWorksheet.indexOf(reverseWorksheet[i]) %2 != 0){
``````

you have some unnecessary code. You already know the value of the index of the element without the `indexOf()` method. What is the value of the index of an element accessed with `myArray[3]`? Would you need to use `myArray.indexOf(myArray[3])` to discover that the index is 3?

Hi Midlinder - very much appreciate your response! I saw in the Help section of the exercise that there is a way to use the reverse method to perform the validation check. My thought process was if I use the .pop() method I’m removing the check digit and adding that to the counter (because no action is performed on that. When I use the reverse function after the .pop() method my thought was I can start there at index 0 to start doubling and by using %2 I’d skip over the odd numbered indices. I get your point with knowing the value of the index when called specifically I was trying to figure out a method to use during iteration to identify the even index positions?

You just overthought it a bit. You are accessing the elements by index using `i`, so `i` is all you need. If `i` is even, the index is even.

Reversing the list is a good strategy, but if you remove the last element beforehand, you’ve removed the first element after reversing, so instead of performing the doubling on every other element starting with the second to last digit from the right (prior to reversing) you are starting with the third to last (prior to reversing). Simply omitting the `pop()` operation completely will give you the desired output. You’ll skip over the last digit, because its new index after `.reverse()` will be 0. It’s value will be added to your `counter` variable here:

along with all of the other digits with an even index.

Super helpful and thanks for the speedy reply. Obviously quite new to this but really enjoying the codecademy structure and the support as well. I’ll continue to play around with it. Have a great new year!

1 Like

I hope you have a great New Year as well! Good luck, and feel free to ask more questions. Happy Coding!

One last tip: for future posts, please review How do I format code in my posts?

Hi Midlinder!

Thanks again for your help on this. I’ve made some good progress except for the last step. The below code is iterating through the invalid credit cards but I can’t seem to isolate the digit at the zero index so it’s always giving “Company does not exist” for each array. I realize I haven’t added the code to avoid duplicates just yet I was focused on getting the array right then working on that bit. thank you in advance!

``````function validateCred(creditCard){
let counter = 0
for (let i = creditCard.length - 1; i >= 0; i--){
let currentCounter = creditCard[i]
if ((creditCard.length - 1 - i) % 2 != 0){
currentCounter = creditCard[i] * 2
if (currentCounter > 9) {
currentCounter -= 9
}
}
counter += currentCounter
}
if (counter % 10 === 0 ){
return true
} else {
return false
}
}

console.log(validateCred(valid1))

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

function idInvalidCardCompanies(myArray){
for (let i = 0; i < myArray.length; i++) {
let firstNum = myArray[0]
if (firstNum === 3) {
} else if (firstNum ===4) {
} else if (firstNum ===5){
} else if (firstNum ===6) {
} else {
}

}
}
``````

When you call `invalidCardCompanies` you are passing in an array containing the invalid card numbers, correct? If so, following your `for` statement:
`for (let i = 0; i < myArray.length; i++)` you have this line:
`let firstNum = myArray[0]`. Try adding a `console.log` statement right after that, and print the value of `firstNum`, and I think you’ll see the problem. Keep in mind that you want to iterate over all of the values in the array. If you use an index of `0`, you’ll only ever get the first element of the array. Also, each element of the array is an array containing the digits of the credit card number. Here’s an example which may be helpful:

``````const nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] //array containing arrays

for(let i = 0; i < nums.length; i++) {
console.log(nums[i][0]) //print the first digit of each nested array
}
``````

Output:

1
4
7

I used `[i]` as the index of the ‘parent’ array, so that I could access each element of the array. I then used `[0]` to access the first element of the nested arrays.

ahhhh - i didn’t know you could call two indexes like that. GOT IT- thank you!

1 Like

It’s not exactly two indices

If you have a value, you can index into it. When you index into a value what do you get? A value. You have a value. You can index into it.

So it’s one index. But you’re doing it again on the result. In particular, there’s nothing special letting you do this, it’s the same as how you can add three values like this:

``````1 + 2 + 3
``````

This is how expressions are evaluated. Start with the innermost operation. Evaluate it. Replace that operation with the result. Repeat with the innermost operation.

1 Like