Credit Card Checker - step 7

Hey Guys!
I wonder if there’s anyone trying to figure out step 7 - Create a function that will convert invalid numbers into valid numbers? Here’s my code but unfortunately I’m getting an error. Can anyone help me with this?

//7
function covertIntoValid (invalidCard) {
  let checkForModulo = 0;
  let lastElement = arr[arr.length - 1];
  let newArray = arr.slice(0, -1);
  let modulo = (checkForModulo + lastElement) % 10

  newArray.reverse();

  for (i = 0; i < arr.length; i += 2) {
    newArray[i] *= 2;
    if (newArray[i] > 9) {
      newArray[i] -= 9;
    }
    checkForModulo += newArray[i];
  }

  for (j = 1; j < newArray.length; j += 2) {
    checkForModulo += newArray[j];
  }

  if (!modulo === 0) {
    let newLastDigit = (10 - modulo);
    return arr.splice(-1, 1, newLastDigit)
  }

return arr

}

What error are you getting? Errors are helpful in locating where the issue lies.

/home/ccuser/workspace/credit-card-checker/main.js:128
convertIntoValid(invalid1);
^

ReferenceError: convertIntoValid is not defined
at Object. (/home/ccuser/workspace/credit-card-checker/main.js:128:1)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

And what’s the name of the function that you’ve declared?

Thanks, I skipped “n” in convert word but it’s not the case. So now the code looks like this:

const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];

//7
function convertIntoValid (invalidCard) {
  let checkForModulo = 0;
  let lastElement = arr[arr.length - 1];
  let newArray = arr.slice(0, -1);
  let modulo = (checkForModulo + lastElement) % 10

  newArray.reverse();

  for (i = 0; i < arr.length; i += 2) {
    newArray[i] *= 2;
    if (newArray[i] > 9) {
      newArray[i] -= 9;
    }
    checkForModulo += newArray[i];
  }

  for (j = 1; j < newArray.length; j += 2) {
    checkForModulo += newArray[j];
  }

  if (!modulo === 0) {
    let newLastDigit = (10 - modulo);
    return arr.splice(-1, 1, newLastDigit)
  }

return arr
}

convertIntoValid(invalid1);

and that’s the output:

/home/ccuser/workspace/credit-card-checker/main.js:99
let lastElement = arr[arr.length - 1];
^

ReferenceError: arr is not defined
at convertIntoValid (/home/ccuser/workspace/credit-card-checker/main.js:99:21)
at Object. (/home/ccuser/workspace/credit-card-checker/main.js:128:1)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Your error tells you everything, your new error, what is arr?

let lastElement = arr[arr.length - 1];

arr needs defining

Okay, I rewrote the code but the output is super weird:

//7
function convertIntoValid (arr) {
  let checkForModulo = 0;
  let lastElement = arr[arr.length - 1];
  let newArray = arr.slice(0, -1);

  newArray.reverse();

  for (i = 0; i < arr.length; i += 2) {
    newArray[i] *= 2;
    if (newArray[i] > 9) {
      newArray[i] -= 9;
    }
    checkForModulo += newArray[i];
  }

  for (j = 1; j < newArray.length; j += 2) {
    checkForModulo += newArray[j];
  }

  let moduloNum = (checkForModulo + lastElement) % 10;
  let newLastDigit = (10 - moduloNum);
  let convertedArr = arr.splice(-1, 1, newLastDigit);

return convertedArr

}

console.log(invalid5);

console.log(convertIntoValid(invalid5));

The console prints:
[ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4 ]
[ 4 ]

and I swear I have absolutely no idea why.

Splice modifies the original array and returns an array of the items replaced.

So your convertedArr variable is storing the replaced value and then you’re returning just that one size array.

Thanks! Finally I figured it out! Here’s the solution:

//7
function convertIntoValid (arr) {
  let checkForModulo = 0;
  let lastElement = arr[arr.length - 1];
  let newArray = arr.slice(0, -1);

  newArray.reverse();

  for (i = 0; i < arr.length; i += 2) {
    newArray[i] *= 2;
    if (newArray[i] > 9) {
      newArray[i] -= 9;
    }
    checkForModulo += newArray[i];
  }

  for (j = 1; j < newArray.length; j += 2) {
    checkForModulo += newArray[j];
  }

  let moduloNum = checkForModulo % 10;
  let newLastDigit = 0;

  if (moduloNum > 0) {
    newLastDigit =+ (10 - moduloNum)
  };

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

return arr

}

console.log(invalid5);

console.log(convertIntoValid(invalid5));