Credit Card Checker 7: changing

let a = "6011568518648431";

for(let i=0;i<a.length;i++){
a[i] = parseInt(a[i],10);
}
console.log(typeof a[0]); // output is string

in the seventh step, it says
‘To make it easier to test credit card numbers, create a function that accepts a string and converts it into an array of numbers like the initially provided arrays. (Check the hint for a helpful function)’

I don’t know why this code cannot change string to int.

https://www.codecademy.com/practice/projects/credit-card-checker

parsing to an integer is successful:

let a = "6011568518648431";

for(let i=0;i<a.length;i++){
  console.log(typeof parseInt(a[i],10))
  a[i] = parseInt(a[i],10);
}
console.log(typeof a[0]); // output is string

but then you update an element in the string, so JS cast the integer back to a string

when dealing which such bugs, break it down in steps: Does the casting to integer go well? In this case it does, so then the next thinking step is: what happens when i try an update an element in a string with a integer:

let a = "6011568518648431";

a[1] = 3

console.log(typeof a[1], a)

teaching yourself these steps (breaking down the problem, understanding every aspect of it) is more valuable then getting to the solution.

actually, this should lead to ever further questioning. How come you can an update a string with a number value? I will leave that for you to figure out (interesting topic to do some research on)

2 Likes

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.