Credit Card Checker Troubles

Hello!

I am new to programming and Javascript, so I apologize if I’m asking something completely obvious here.

The first part of the Credit Card Checker project asks you to create a function that determines if a credit card is valid or invalid using the Luhn algorithm . Here is what I came up with:

// Add your functions below:

const validateCred = creditCard => {
  let reversedArray = creditCard.reverse();
  let newArray = [];
  for (let i = 1; i < reversedArray; i = i + 2) {
   let value = i * 2;   
    if (value > 9) {
      newArray.push(value - 9);
    } else {
      newArray.push(value);
    }
   let sum = newArray.reduce((acc, item) => {
     return acc + item}, 0);
    if (sum % 10 === 0) {
    console.log('Creditcard is valid')
    } else {
    console.log('Creditcard is invalid')
    }
   }  
  }

When I try to log the function with one of the given arrays it comes back as undefined. Thanks in advance for any assistance!

link to the project: https://www.codecademy.com/practice/projects/credit-card-checker

If you make a simpler function, say, one that adds two numbers together, how would you get the result back from that?
You’d for example want to be able to do:

add(3, add(4, 5)) to obtain 12

Also, I suggest adjusting your indentation so that it matches your braces, otherwise it will be visually misleading. Usually text editors help you keep it this way at all times.

Thanks for the reply!

Are you referring to return? I tried adding it like such earlier in my troubleshoot, I’ve just been adding and removing code trying to make it work:

 return.console.log('Creditcard is valid')
    } else {
    return.console.log('Creditcard is invalid')

The above still returns undefined. I also tried putting it before:

 if (value > 9) {
     return newArray.push(value - 9);
    } else {
     return newArray.push(value);
    }

I assume I’m using return in the wrong place?

When you say:

What exactly does that look like? I had a feeling I messed up the structure and indentation of it all as I was adding and deleting code trying to make it work.

Less of this.
What does return do, and, how do you therefore need to leverage it to get what you want?
What does array.push return, and is that something your function wants to return? Probably not.
You seem to “try” that but does it make any sense to do, is it something you argued was the right thing? My guess is this was arbitrary without much plan. You need to stick to whatever the plan is, and make the program do that and nothing else. Your plan is your reference to what needs doing.

More likely, you want your function to compute some value that you can return to the caller.

When you open a brace, increase indentation by 2, when you close one, decrease by 2.

Getting anywhere with that?

if you consider the function that adds two numbers, its whole purpose is to return its result, that is how it presents the result. where would you get the result if it wasn’t returned to the caller?
your function is not so different. it takes some input and is supposed to produce some result, namely whether or not the provided input is a valid card number

Thanks for the advice.

I figured it out. When I got back from dinner I took a look at the function chapter in the Introduction to JavaScript course (paying attention to return). I’ve only been at this a few days and I guess its easy to read over material quickly without actually understanding how it works.