Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

length of string is 54 although there is no input

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading