Skip to content
Advertisement

Don’t understand why it only put the first int into array in javaScript

I would like to create a progress bar using Js. I’m following the online demo made by Cassidy Williams on September 13, 2018. Link: https://codepen.io/cassidoo/pen/wEXQaG

I added more steps into the steps div elements:

JavaScript

But when I click the third step the last step status is clicked. What is the problem with this error? Also, what is the solution to this error?

Advertisement

Answer

On line 6 of the JS code, the author have the following:

JavaScript

Replace by:

JavaScript

And on lines, 14, 18 and 21, do the same.

JavaScript

The HTML id is a string, so a cast is necessary. The conditions misbehave because a string comparison is used instead of a number comparison. By casting the string to a number, it is working as intended.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement