Issue
i've been trying to combine 6 digits as one string but I check the length of the combinedPin, it's 54 although I haven't input anything yet
here's the piece of my code
combinedPin = "" + this.pinCode1 + this.pinCode2 + this.pinCode3 + this.pinCode4 + this.pinCode5 + this.pinCode6;
combinedPinInt = parseInt(combinedPin);
console.log("pincode: " + combinedPinInt + " length: " + combinedPin.length + " typeof: " + typeof combinedPin);
what is happening here? where did the 54 come from?
Solution
The issue here seems to be related to how JavaScript is interpreting the variables this.pinCode1
through this.pinCode6
. JavaScript might be converting undefined variables to "undefined" strings when you are trying to concatenate them. That would increase the length of your combinedPin
string.
Here's a potential example of what could be happening:
pinCode1 = undefined
pinCode2 = undefined
pinCode3 = undefined
pinCode4 = undefined
pinCode5 = undefined
pinCode6 = undefined
combinedPin = "" + this.pinCode1 + this.pinCode2 + this.pinCode3 + this.pinCode4 + this.pinCode5 + this.pinCode6;
console.log(combinedPin.length) // Prints: 54
Each "undefined
" string has 9 characters and you are concatenating 6 of them, which results in a total of 54 characters.
To solve this problem, you need to ensure that pinCode1
through pinCode6
are correctly defined and hold the values you expect before the concatenation operation.
If you could show more of the code, I might be able to help you with a potential fix.
It could also be something else, but it's quite coincidental that it logs exactly 54. That's why I suspect that all the values are undefined.
Answered By - Y. Gherbi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.