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 :sweat_smile:: 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];

// Add your functions below:

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 :thinking:) 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!