# Trying Luhn Algorithm

Hello, Im trying to implement Luhn algorithm in Credit card checker project, but I dont know how to
return an array that is created by .map()

``````const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
`function validateCred(array) {
emptyArray = [];
everyOther = [];
modified = [];
for (let i=array.length-1; i > 0; i--) {
emptyArray.push(array[i]);
}

for (let j=1; j < emptyArray.length; j += 2) {
everyOther.push(emptyArray[j]);
}
everyOther.map(function multiply(s){
let multiplied = s*2;
if (multiplied > 9){
return multiplied - 9;
}else{
return multiplied;
}
})
return [emptyArray, everyOther];
}

console.log(validateCred(valid5))``Preformatted text`
``````
1 Like

Does this example help? If youâ€™re having trouble making sure itâ€™s an array that youâ€™re returning, just make sure by logging before you return it

``````const test_array = [1, 2, 3, 4];
const test_map = test_array.map(x => x * 4);
console.log(test_map); // [4, 8, 12, 16]
return test_map;
``````

Yeah, assign it to a variable.

a variable thatâ€™s an array?

yeah, a variable thatâ€™s an array

when you assign it. It becomes array

correctâ€¦ maybe I misunderstood your question : I was just going by this part of the post:

so youâ€™re having trouble returning that array? if you format (</> button) the code itâ€™ll be easier to interpret whatâ€™s happening.

I just clicked on â€śpreformatted textâ€ť

You have to highlight it and then press </>

Ok, it seems like youâ€™re calling `everyOther.map` and it returns something, but that something isnâ€™t being stored (for it to work with the rest of the `validateCred()` function) . That might be the issue.

Fixed it

``````const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

function validateCred(array) {

emptyArray = [];

everyOther = [];

for (let i=array.length-1; i > 0; i--) {

emptyArray.push(array[i]);

}

for (let j=1; j < emptyArray.length; j += 2) {

everyOther.push(emptyArray[j]);

}

modified = everyOther.map(function multiply(s){

let multiplied = s*2;

if (multiplied > 9){

return multiplied - 9;

}else{

return multiplied;

}

})

return [emptyArray, everyOther, modified];

}

console.log(validateCred(valid5))
``````

by assigning to variable â€śmodifiedâ€ť

1 Like

Great!

I personally find it useful to just play around and experiment with single elements of these bigger functions to see how they behave (bending and breaking ) before applying them on a bigger scale.

If Iâ€™m ever doing anything large, I really only consider tools that I know very stronglyâ€¦ So the preparatory experimentation I mentioned sets a great foundation for dealing with complex structures and concepts.

Thatâ€™s also why I gave a simplified example. If something doesnâ€™t work in a big app Iâ€™m writing, I first try to abstract it in the simplest terms possible. Itâ€™s not unusual that Iâ€™ll learn something unexpected about something I thought I knew fairly well!