Hi, everyone! I’m in trouble I just started learning and I’m trying to solve simple code with first array I don’t know what the sum is working wrong. Please leave comments here.
why is
sum += numbers
inside an else
block?
Ohh. I think it is wrong. I’ll review. Thank you.
Please what is the wrong? I checked one array, it is ok. but replaced with batch, got error, I added two different Codebyte. I cannot imagine what do I do this error, please help a little, I would appreciate.
Because batch
is an array containing arrays you need to iterate through batch
and then call ValidateCreditCard()
on each item in the batch
array.
Hope that helps!
Thank you for response, how to iterate the batch? Should I change arr to batch as argument, right?
Maybe something like:
for (let i = 0; i < batch.length; i++) {
validateCreditCard(batch[i]);
}
Hi, everyone! Hope I completed the project, compared with https://discuss.codecademy.com/t/credit-card-checker-challenge-project-javascript/462375?u=artashes85, and corrected some errors, but there are little different results, please review if there are errors. Thanks!
Looks good overall.
I think that your stuff gives the correct result for arrays that have an even length, but not for arrays an odd length.
(Your does the doubling of the digit if the index is even, which matches “double every other digit going backward starting with the next-to-last digit” in the algorithm.)
You could fix that by checking whether the difference between the length and the index is even, instead of checking if the index is even.
On the 38 line, I changed to if(arr.length - 1 === 0) or if(i%2 != 0), it didn’t work correctly, should been true and false, didn’t print empty array, that is the wrong, I stressed? Please, write your example for this 38 line. I don’t imagine how odds works, if there is material or simple example, it will be helped me to understand.
One approach is: to check whether the difference between the length and the index is even
if ((arr.length - i) % 2 === 0) {
other approach
A different way to accomplish that would be to have a variable that counts from the end of the array.
const validateCred = (arr) => {
let sum = 0;
let j = 0; // variable to count from the end
for(let i = arr.length - 1; i >= 0; i--) {
let currNumber = arr[i];
if (j % 2 != 0) {
currNumber *= 2;
if(currNumber > 9) {
currNumber -= 9;
}
}
sum += currNumber;
j++;
}
if(sum % 10 === 0) {
return true
} else {
return false
}
};
Thanks you. Works. Can you explain ((arr.length - i) % 2 === 0) how it works? It will help me understand logically.
Correct? I imagine like
16-0=16 % 2 === 0
16-1=15 % 2 === 0
16-2=14 % 2 === 0
16-3=13 % 2 === 0
16-4=12 % 2 === 0
…
Hi Artashes,
I completed the project in a very similar way, thank you for sharing this . It’s always nice to confirm that I am on the right track.
Mike
Hi, nice. Please share the code with us
No problem, here is my completed solution.
Finally finished, spent the entire day debugging and writing out comments.
Check it out here: projects/codecademy-assignments/JS_assignments/projects/credit-card-checker-starter/main.js at main · kiddulu916/projects · GitHub
Did both bonus challenges also, bonus challenge 1 starts on line 146 and bonus challenge 2 starts on line 194.
Let me know what yall think, how it can be improved. Added way more comments then I usually do but trying to get in the habit of it more. Let me know if its to many, lol.