Credit card checker function problem

I have been working on the credit card checker project and I’m having a problem with the Id invalid card companies function.

I have decided to create variables for each credit of the card companies that are stored as booleans. Then I created a loop that runs through the first digit of each of the card numbers in my nested array of false card numbers and if for example ‘3’ shows up we will change the value of amex to true.

Then the function logs a string to the console for every card company that is stored as has the boolean value of true.

When I try to run my function it always comes up as undefined. There is obviously something fundamental that I am missing here, but I can’t figure it out.

I am going to re write the code and use a different method to solve the problem. But I think it would be helpful to know why this function isn’t behaving how I anticipated it would.

here is my code:
https://gist.github.com/8b6f7206c1e3524d54b695af1c462785

I would be grateful for any help

I think the issue is you are logging another console.log.

Change your console.logs in the function to return statements and that should solve that issue (at least on the surface).

Welcome to the forums :slight_smile:

1 Like

Thanks for the help!

I’ve replaced the return statements for console logs and I’m still getting undefined. But it’s good to know that I shouldn’t put console logs in a function that I am going to call inside another console log

One down!

Well, back on the theme of returns, if a function invocation is logged and there is no return… I think it also returns undefined. So to me that means that your conditionals are not being met to begin with. You might want to put some log statements before them like:

console.log(amex === true);

to see if they’re being triggered… and work backwards from there.

3 Likes

Awesome idea using booleans! There are 2 reasons why it isn’t working. The first is with your for loop:

for (let p; p < arr.lenth; p++){

p isn’t initialized so it takes on an undefined value. Example:

  let test;
  console.log(test); // undefined

so when it gets to the comparison p < arr.lenth you are asking if undefined < arr.lenth, which is false so the for loop never runs. Also, is lenth a property? Typos happen. No shame in that haha!

The second reason is in your switch statements:

case 3 : 
return amex = true;
break;

When you use return the function is immediately exited and the value is returned. Sometimes this is handy in switch statements, but here it is stopping your function before it loops through all the elements in the array – the first case to match will stop the function and your if statements at the bottom are never reached.

Correct those things and your idea is ready to rock! Hope that helps, and welcome to the forums!

Bonus info - The break statement is not necessary if you plan on returning a value from a switch. The break statement stops the rest of the cases from running once a match is found, if it is left out then the rest of the code in the switch statement will run. Example:

switch (1) {
  case 1 :
    console.log('a');
    break;
  case 2 :
    console.log('b');
    break;
}
// Logs:
// a

Now leaving out the first break

switch (1) {
  case 1 :
    console.log('a');
    //break;
  case 2 :
    console.log('b');
    break;
  case 3 :
    console.log('c');
    break;
}
// Logs:
// a
// b

// a break statement is reached, so 'c' is not printed.

Freaky. But if you’re returning a value then the rest of the code doesn’t matter - as soon as something is returned the function is exited. So this is acceptable:

switch (1) {
  case 1 :
    return 'a';
  case 2 :
    return 'b';
}
// 'a' would be returned if this were in a function.

That is correct. A function will always return undefined unless the programmer returns their own value.

2 Likes

Yes, good point, I should make a habit of checking that each bit of my code works before building the rest on top of it!

That in itself is a great takeaway. Especially important when doing anything with increased complexity.

Remember if you’re running js on a browser you can use the console to interactively test these things out. I find it pretty useful.

1 Like

Whoops! I feel a tad dopey for not noticing that I didn’t assign a value to my iterator.

Good point about switch functions. I remember the javascript course on switch functions mentioning that the the break stops you once a condition is met. I suppose I made a fundamental error in using a switch statement, like this which kind of works more like an if/or statement which is precisely what I *don’t * need for this task.

But if I understand you correctly, the switch statement will act like a series of consecutive ‘if’ statements if I remove the breaks (but if I use console logs rather than return statements I’ll end up returning everything else on the list once a condition has been met).

So I’ll try again. But thanks a lot! I feel like I understand a lot more now that you have helped me understand my mistakes

That’s not correct. The original condition in the switch statement is evaluated once - only once. The first case that matches is where control is passed to. From there every executable line of code is executed in order including break or return. If you leave those out, every executable line of code from the first matching case down will be executed:

const color = 'blue';

switch (color) {
  case 'blue':
    console.log('The color is blue.');
  case ' red':
    console.log('The color is red.');
  case 'yellow':
    console.log('The color is yellow.');
  default:
    console.log('I do not know what the color is.');
}

console.log('This line is outside the switch.');

Output:

The color is blue.
The color is red.
The color is yellow.
I do no know what the color is.
This line is outside the switch.

Versus:

const color = 'blue';

switch (color) {
  case 'blue':
    console.log('The color is blue.');
  case ' red':
    console.log('The color is red.');
    break; //This will pass control to whatever comes after the switch's closing }
  case 'yellow':
    console.log('The color is yellow.');
  default:
    console.log('I do not know what the color is.');
}

console.log('This line is outside the switch.');

Output:

The color is blue.
The color is red.
This line is outside the switch.

2 Likes

Thanks everyone for your help. I completed the project and tested my functions, they all seem to work as intended. I ended up using a series of ‘if’ statements in place of the switch statement.

Here is my solution code if anyone is interested:

https://gist.github.com/2c2e6187e792ef8fc0789af4d593959e

1 Like