Credit Card Checker Challenge Project (JavaScript)

Hello everyone. With some effort and relief. I’ve been able to come to a program that works for the earlier part of the project.
I would be most grateful if anyone can help me figure out how I can calculate the total figure before a boolean is returned.
I haven’t been able to use console.log in the correct place perhaps.
I have been using console.log(validateCred(valid1))

// Add your functions below:

//credit card function declaration
const validateCred = card => {
total = 0
//let reversed = ;

//will be using the Free Formatter’s Luhn algorithm implementation
//create a reversed array for credit card number and keep last digit as will be counted as part of the determining final sum
for (let i = card.length - 1; i >= 0; i–) {
//reversed.push(arrayValue);
let arrayValue = card[i];
//want to iterate through every number in the array as will want to calculate odd i positioned numbers and last digit in addition to the sum of the even i positioned numbers (with the last digit included at the beginnning i =1) and subtracted by 9 if exceeds 9 after being doubled for even positioned i
if(i%2 === 0){
arrayValue *= 2;
if(arrayValue > 9){
arrayValue -=9;
}
};
total += arrayValue;
}

return total % 10 === 0;
}
//console.log(validateCred(valid1))
console.log(" ‘This should show true for valid cards:’ " + validateCred(valid4))
console.log(" ‘This should show false for invalid cards:’ " + validateCred(invalid4))

This was a lot of fun! I enjoyed it.

Hello guys :wave:. I am so glad :innocent: that I have completed my Credit Card Checker Project and I would like to share it with you if you don’t mind. Below is the link to my code. I hope my code can help someone who is stuck. Also if you have suggestion for my code, you are free to tell. Thanks! Credit Card Checker Project

I’ve figured it out. I have been using the debug terminal in VS to view the output and have just figured out that selecting new terminal has allowed my node command to work. in the terminal ie. node.fliename.js

I have been able to modify my code above properly now. All this time I’ve been working blind almost.

1 Like

Hey here is my solution:

Hi, here is my solution in last part (option) of Credit Card Checker.
Convert invalid card number to valid valid number.
(notice; I set function validateCred returns array => [valid or invalid, card number sum, first digit of numbers] )
Thanks for other challengers hint and tips, I could make my coding! yay!


//# 7-3 convert invalid numbers into valid numbers.

function convertIntoValid(arr){

arr.splice(arr.length-1, 1, 0);

console.log(arr)

let checkForModulo = parseInt(validateCred(arr)[1]);

let moduloNum = checkForModulo % 10;

let newLastDigit = 0;

if (moduloNum > 0 ) {

newLastDigit += (10 - moduloNum);

}

arr.splice(-1, 1, newLastDigit);

let newArr = arr;

console.log(newArr)

validateCred(newArr);

return newArr;

};


console.log(invalid5);

// [ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4 ]

convertIntoValid(invalid5);

//return converted array

// [ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 3 ],

// => valid

Hey all, here is my credit card checker solution, I welcome all feedback and would love to hear how it compares to yours!
Solution

HI, could you use backstrokes (top left of your keyboard behind 1) to surround your code please? It puts it in a code block, just need 3 on the line before and

3 on the line after

And it’ll look like above! A lot more readable!

1 Like

It took me almost a whole weekend to come up with this solution, I am still not sure if I got it right since my results differ from the solution posted on github. Please feel free to comment.

1 Like

I highly condensed the code utilizing ternary operations to make it more efficient as well. Having a background in python, having a single line to do a whole operation is always nice. May not be easy to read, but it works and is extremely condensed. Took more time to write it then it did to come up with it.

Solution (condensed)

2 Likes

Thanks for nice tips ! I didn’t know that!
I have a swedishkeybord …so using button for making a code block ( </> ).
Yeah, it looks much better than before!

//# 7-3 convert invalid numbers into valid numbers.

function convertIntoValid(arr){
arr.splice(arr.length-1, 1, 0);
console.log(arr)
let checkForModulo = parseInt(validateCred(arr)[1]);
let moduloNum = checkForModulo % 10;
let newLastDigit = 0;
if (moduloNum > 0 ) {
newLastDigit += (10 - moduloNum);
}

arr.splice(-1, 1, newLastDigit);
let newArr = arr;
console.log(newArr)
validateCred(newArr);
return newArr;
};

console.log(invalid5);
// [ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4 ]
convertIntoValid(invalid5);
//return converted array
// [ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 3 ],
// => valid 
1 Like
1 Like

Hi, here is my solution!
Can you give me some feedback about my solution?
thanks

1 Like

Hi all! Here’s my solution.

1 Like

Hello guys. Here is a link to a solution to this challenge. I am always impressed with how these codes work. :smile: :smiley: :thinking: … thanks

2 Likes

That’s so cool!!! :zap:

Wasn’t aware you could nest ternaries inside each other! :exploding_head:
Using a = 0 to do nothing!
And making the company digit the index, both checking the card first digit and if the company name was already added on the same line! VERY cool!

And actually, while it took me a little to go through that one condensed line in every function, I didn’t find it hard to read. Making it so short definitely makes following the logic very easy!

Had so much fun reading it and learned a lot!
Great work! :clap:t4:

1 Like

Here’ s mine!

Really enjoyed the last function from the Codecademy solution using switch and indexOf() to check if the company name was already added was really cool!

My solution

Learning a lot by reading code from other students too! Some really creative people in here!!! :exploding_head:

1 Like

Here goes mine including extra challenges in number 7:

https://github.com/ucha1110/credit-card-checker/blob/main/main.js

Thanks to everyone who shared code before me, I discovered other possible approaches reading other students code :slight_smile:

I got mine done, but for some reason am only getting Visa as an output?

1 Like

Here is my solution to the Credit Card Checker. I think it can probably be optimized a bit, but it works, and that’s what counts, right?

Credit Card Checker Solution

1 Like