I don’t understand this project. I mean are valid arrays really valid? because each time I log an array called with validateCred
function, I get invalid and my function is:
const reduce = (accumateValue, currentValue) => accumateValue + currentValue;
// Add your functions below:
const validateCred = arr => {
for (let i= arr.length - 2; i >= 0; i--) {
arr[i] = arr[i] * 2
if (arr[i] > 9 ) {
arr[i] -= 9;
};
}; // i loop end
const array2 =arr.reduce(reduce);
const remainder = array2 % 10;
if (remainder == 0) {
return "valid"
} else {
return "invalid"
}
return remainder
}; // function end
Please help.
Hello, @joewilliam0901679842.
You’ve got a few issues with your validateCred
function. First of all, you are mutating the original array. You can use the slice()
method to make a copy before you apply the Luhn algorithm. Second, you’re implementation of the Luhn algorithm is off. Your code is doubling every element of the array from right to left starting with the second to last element. You should only be doubling every other element. Lastly, you need to decide what it is you want to return. Only one return
will be executed. remainder
will either be 0 or something else, so your function will return either valid
or invalid
. This line: return remainder
is unreachable, and will never be executed. (I believe the project asks for the return value to be either true
for valid or false
for invalid.)
1 Like
But how to double only the digits that we want to double and not every element?
You have a few options to accomplish that, but the most simple might be to consider this part of your for
loop: i--
. How could you change that to decrement by 2 instead of only 1?
simply by changing i--
to i -=2
1 Like
Thank you! But I have one more question: can I create and use a new empty array let’s say var array = [ ]
so that i don’t mutate the original arrays? sorry I am still learning these things.
1 Like
To make a copy of an array, use slice()
. For example:
const myArray = [1, 2, 3, 4, 5, 6];
const myArrayCopy = myArray.slice();
//now we can mutate the copy without affecting the original array
I’m not sure how you would make a copy of the original array by creating an empty array first. I suppose you could iterate through the original array, and add them in one at a time, but there’s no need since slice()
will make a copy for us.
1 Like
Ok I tested my function validateCred
on all the given arrays and it does the trick but when i return array
, it says: unreachable code detected but why? and the code is working correctly without mistakes and this is the code:
const reduce = (accumateValue, currentValue) => accumateValue + currentValue;
// Add your functions below:
const validateCred = arr => {
const array = arr.slice();
for (let i= array.length - 2; i >= 0; i -= 2) {
array[i] = array[i] * 2
if (array[i] > 9 ) {
array[i] -= 9;
};
}; // i loop end
const array2 = array.reduce(reduce);
const remainder = array2 % 10;
if (remainder == 0) {
return "valid"
} else {
return "invalid"
}
return array; unreachable code detected
}; // function end
Once a return
is executed, the function is done. return
hands a value and control flow back to the line of code that called the function. One of the return
statements in your if
condition is going to be executed, so the return array;
line will never be reached.
There is no need to return the mutated array. The function should only return true
if the array is valid or false
if the array is an invalid credit card number. If the card number is invalid, then you would push that array (the original un-mutated one) into the array of invalid card numbers.
1 Like
is there a bug in this code or what? I mean if I log findInvalidCards(batch)
to console, it says undefined
but when logging arrayForInvadCards
to the console, I get the right solution as I expected and as what the task wants. but why it says undefined
in the first logging?
var arrayForInvalidCards = []
// Task 4
function findInvalidCards(arrayForInvalid) {
for (let i = 0; i < arrayForInvalid.length; i++ ) { // The main loop
validateCred(arrayForInvalid[i]);
if (validateCred(arrayForInvalid[i]) === "invalid") {
arrayForInvalidCards.push(arrayForInvalid[i])
}
}; // The main loop
}; // function end
console.log(findInvalidCards(batch)); // logs undefined
console.log(arrayForInvalidCards) // logs the expected value
Can you share your entire code? I need to see what is in arrayForInvalid
. I don’t understand why you need arrayForInvalidCards
and arrayForInvalid
. You start with an array named batch
that has all of the individual card number arrays. The task is to iterate through the batch array supplying each card number array as the argument to the validateCred()
function. If the number is invalid, push that array into a new array for the invalid card numbers.