Credit Card Checker Challenge Project (JavaScript)

I found the problem, wasn’t in that function but in the previous one:
function validateCred(arr){

let sum = 0;

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

if ((arr.length - i)%2 === 0 ){

  arr[i] *=2;

  if(arr[i] > 9){

    arr[i] -= 9;

  } 

}sum += arr[i];
}
if (sum %10 === 0){
return true;
}
return false;
}

The function by itself was working but I was mutating the output on the findInvalidCard func because I was mutating the original array on the line arr[i] *=2… I needed to assign that to new variable… by the way problem solved.

Thanks a lot.

function validateCred(arr){

let sum = 0;

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

let currV = arr[i];

if ((arr.length - i)%2 === 0 ){

  

  currV *=2;

  if(currV > 9){

    currV -= 9;

  } 

  

}sum += currV;

}

return sum % 10 === 0;

}

function findInvalidCards(nArr){

let junkIndex = ;

const junkArr = batch;

for (let i = 0; i < nArr.length; i++){

if (validateCred(nArr[i])){

  junkIndex.push(i);

} 

}for(let k = junkArr.length; k >= 0; k--){

  for (let x = 0; x < junkIndex.length; x++){

    if(k === junkIndex[x]){

      junkArr.splice(junkIndex[x],1);

    }

  }

  

}return junkArr;

}

Thanks for making the last 6 months so interesting! For some reason, I couldn’t get “for” loops to work on the last 2 functions (findInvalidCards() and invalidCardCompanies()), so I’m now very well acquainted with .forEach()! slight_smile: What an elegant solution!

Hey guys, here’s a link to my solution.


Here’s my code any tips to improve it would be helpful

Have a look at my solution to the problem!

1 Like

https://github.com/dimitrifox/creditCardChecker.git


This is my solution to this interesting challenge

Hello community.
Here’s my code. I aimed for readability. Pretty much the same as codecademy’s solution.
Thanks!

1 Like