How to create a function that will convert invalid numbers into valid numbers in “Credit Card Checker” project ?

You need to start off by making a function that accepts an array as a parameter.

`function abcde(arr) { }`

I mean, which algorithm will I use?

The Luhn algorithm.

On the wiki page for that algorithm is everything you need to make the validateCred function.

I think you understand wrong, In part 7, it say “Create a function that will convert invalid numbers into valid numbers.”.I don’t no how to create this function?

I wrote other 1,2,3,4,5,6 parts in project.

I did read wrong sorry about that.

Hello, @board9051179152.

Welcome to the forums.

Consider how you determined the number was invalid. You created a sum using the Luhn algorithm, and tested that sum to see if `sum % 10 == 0`

. That in mind, what would it take to get that expression to evaluate to `true`

with a given credit card number? For example, if we knew that `sum % 10`

returned a value of `3`

, how could we get the `sum`

to be either 3 less or 7 more?

Can I sent whole code to you for checking ? True or false?

Feel free to post your code here if you need help, or just want feedback. I’ll look at it when I can, and likely so will others. Please just remember to format your code. You can refer to this topic: How do I format code in my posts?

```
// All valid credit card numbers
const valid1 = [4,7,1,6,3,5,3,7,6,6,8,3,9,4,2,7];
const valid2 = [5,1,0,2,7,0,7,3,7,6,8,2,1,7,2,0];
const valid3 = [6,0,1,1,9,5,9,6,4,2,7,0,9,7,7,9];
const valid4 = [6,0,1,1,3,3,5,5,5,1,3,8,4,5,6,4,4,1,4];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];
let valid6 = [];
const inputStringToArr = str => {
for(let i=0;i<str.length;i++){
let a = parseInt(str[i],10);
valid6.push(a);
}
}
inputStringToArr("4556206302320585580");
batch.push(valid6);
// Convert invalid card to valid Card
const convInvToVal = (arr,remain) => {
let max=arr[0];// find max , because i want to subtract remain from maximum number;
for(let i=1;i<arr.length;i++){
if(max<arr[i]) max=arr[i];
}
arr[arr.indexOf(max)] -= remain;
validateCred(arr);
}
// Add your functions below:
const validateCred = array => {
const mutualArr = [];
for(let i=0;i<array.length;i++){
mutualArr[i]=array[i];
}
let sum=0;
for(let i=mutualArr.length-2;i>=0;i-=2){
mutualArr[i]*=2;
if(mutualArr[i]>9){
mutualArr[i]-=9;
}
}
for(let i=0;i<mutualArr.length;i++){
sum+=mutualArr[i];
}
if(sum%10==0) return true;
else {
let remain = sum%10;
convInvToVal(array,remain);
}
}
//console.log(validateCred(invalid1));
const invalidArr = [];
const findInvalidCards = bat => {
for(let i=0;i<batch.length;i++){
if(validateCred(batch[i])===false){
invalidArr.push(batch[i]);
}
}
}
findInvalidCards(batch);
console.log(invalidArr);
const invalidCompany = [];
const inInvalidCardCompanies = invalidCardArr =>{
for(let i=0;i<invalidCardArr.length;i++){
if(invalidCardArr[i][0]===3){
if(invalidCompany.indexOf('Amex (American Express)')=== (-1)) invalidCompany.push('Amex (American Express)');
}
else if(invalidCardArr[i][0]===4){
if(invalidCompany.indexOf('Visa')=== (-1)) invalidCompany.push('Visa');
}
else if(invalidCardArr[i][0]===5){
if(invalidCompany.indexOf('Mastercard')=== (-1)) invalidCompany.push('Mastercard');
}
else if(invalidCardArr[i][0]===6){
if(invalidCompany.indexOf('Discover')=== (-1)) invalidCompany.push('Discover');
}
else {
console.log('Company not found');
}
}
}
inInvalidCardCompanies(invalidArr);
console.log(invalidCompany);
```

I haven’t gone through your code completely to see if it produces desired output, but I do have a few thoughts on your strategy for converting an invalid card number to a valid card number.

Finding the highest (max) number in the card number, and then subtracting the predetermined amount to produce a valid number could be problematic for a couple of reasons. First, even though it isn’t the case with the example numbers provided, it is conceivable that you might come across a number where the first digit is the highest. Changing the first digit, changes the Card Company. Second, changing one of the digits that has to be doubled and then possibly have 9 subtracted from it won’t produce the desired result. For example, if the original digit was 9, doubling it to 18 then subtracting 9 results in 9. If 3 were the value you need to subtract, and you subtract 3 from 9, you have 6. Doubling 6 produces 12. Subtracting 9 leaves you with 3. If you had needed to lower the sum by 3 to get `sum % 10 == 0`

, you would still be off since the sum is actually reduced by 6.

I hadn’t yet added a function to my project to convert invalid numbers to valid, but did so last night. My approach was to split my `validateCred`

function into two functions, so that I could have a function that only creates the `sum`

according to the Luhn algorithm. That value gets returned, and in the `validateCred`

function I add the check digit to the returned sum, and return the result of the modulo operation (`true`

or `false`

). I also use the ‘summing’ function in my ‘convert’ function. After the sum of the invalid number is returned, I use the modulo operation to determine what the value of the check digit must be for the number to be valid, and assign that value to the end of the credit card number array in place of the original check digit.

I’m not saying you should use my approach, just passing along my train of thought.

Ok, thanks.When I try to add number in the end of array , It shows error.It is code:

```
const convInvToVal = (array,remain) => {
array.push(remain);
validateCred(array);
}
// Add your functions below:
const validateCred = array => {
const mutualArr = [];
for(let i=0;i<array.length;i++){
mutualArr[i]=array[i];
}
let sum=0;
for(let i=mutualArr.length-2;i>=0;i-=2){
mutualArr[i]*=2;
if(mutualArr[i]>9){
mutualArr[i]-=9;
}
}
for(let i=0;i<mutualArr.length;i++){
sum+=mutualArr[i];
}
if(sum%10==0) return true;
else {
let remain = sum%10;
remain=10 -remain;
convInvToVal(array,remain);
}
}
```

Error code:

```
/home/ccuser/workspace/credit-card-checker/main.js:39
const validateCred = array => {
^
RangeError: Maximum call stack size exceeded
at validateCred
```

Sorry for the late reply. You have two functions here, and each of them is calling the other, so it results in an endless loop.

Can you send me code which you wrote?

I couldn’t write this function which convert invalid card to valid card.

From my point of view at least, exercises are all about the process leading up to the result, not the result.

So, when you get stuck that means you’ve probably found some problem with that process. That’s a good thing, and it’s probably good to work through it to figure out how you would get yourself unstuck, and not circumvent it because you’ll probably encounter the same thing again later.

Do you know what is preventing you from creating such a function? Whatever that something is, it’s worth poking at. Maybe there’s some information that you’re missing, and then you’d move on to considering where that information can be found. Maybe you’re unsure about how to devise a strategy for your code to follow to get the result it should produce.

Do you know what your function needs to do? What does “convert” involve? Is it something you could do manually if given an invalid number? What steps do you carry out to do that? Could you write those steps as code?

Try to break things down into smaller problems/steps. Here for example you’re asking for what the whole thing can look like instead of zooming in on the particular thing(s) that you need to write it yourself. There may be many problems to solve, but start at the first one, solve it, and continue from there.

Thanks for replying.I thought more and solve it.

```
// All valid credit card numbers
const valid1 = [4,7,1,6,3,5,3,7,6,6,8,3,9,4,2,7];
const valid2 = [5,1,0,2,7,0,7,3,7,6,8,2,1,7,2,0];
const valid3 = [6,0,1,1,9,5,9,6,4,2,7,0,9,7,7,9];
const valid4 = [6,0,1,1,3,3,5,5,5,1,3,8,4,5,6,4,4,1,4];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];
let valid6 = [];
const inputStringToArr = str => {
for(let i=0;i<str.length;i++){
let a = parseInt(str[i],10);
valid6.push(a);
}
}
inputStringToArr("4556206302320585580");
batch.push(valid6);
// Convert invalid card to valid Card
const convInvToVal = (summ,array) => {
summ = 10 - (summ%10);
if(summ===10) array[array.length-1] = 0;
else array[array.length-1] = summ;
console.log(array);
}
// Add your functions below:
const validateCred = array => {
const mutualArr = [];
for(let i=0;i<array.length;i++){
mutualArr[i]=array[i];
}
let sum=0;
for(let i=mutualArr.length-2;i>=0;i-=2){
mutualArr[i]*=2;
if(mutualArr[i]>9){
mutualArr[i]-=9;
}
}
for(let i=0;i<mutualArr.length;i++){
sum+=mutualArr[i];
}
if(sum%10==0) return true;
else {
sum-=mutualArr[mutualArr.length-1];
convInvToVal(sum,array);
}
}
//console.log(validateCred(invalid1));
const invalidArr = [];
const findInvalidCards = bat => {
for(let i=0;i<batch.length;i++){
if(validateCred(batch[i])===false){
invalidArr.push(batch[i]);
}
}
}
findInvalidCards(batch);
//console.log(invalidArr);
const invalidCompany = [];
const inInvalidCardCompanies = invalidCardArr =>{
for(let i=0;i<invalidCardArr.length;i++){
if(invalidCardArr[i][0]===3){
if(invalidCompany.indexOf('Amex (American Express)')=== (-1)) invalidCompany.push('Amex (American Express)');
}
else if(invalidCardArr[i][0]===4){
if(invalidCompany.indexOf('Visa')=== (-1)) invalidCompany.push('Visa');
}
else if(invalidCardArr[i][0]===5){
if(invalidCompany.indexOf('Mastercard')=== (-1)) invalidCompany.push('Mastercard');
}
else if(invalidCardArr[i][0]===6){
if(invalidCompany.indexOf('Discover')=== (-1)) invalidCompany.push('Discover');
}
else {
console.log('Company not found');
}
}
}
inInvalidCardCompanies(invalidArr);
//console.log(invalidCompany);
```

wow , it gave me some insight into it …

is it something like that :

let x = 13;

let mdl = x % 10;

console.log(mdl);

x = x - mdl ;

console.log(x);

Something like that. You just have to choose one of the digits to change.