The reason behind choosing this particular topic? As a student at Flatiron School we go through many challenges and many time you come across a challenge that requires you to google. I still have a lot to improve on my googling skills. See here comes in the choice of my topic. I just could not find the answer to this very simple question. I tried so many ways and even though you can find some answer they just seemed way to difficult.
In that particular challenge you had to add a vote based on the value of your input field to your total vote count.
<h4>Total Votes: <span id="vote-count">Character's Votes</span></h4>; <form id="votes-form">; <input type="text" placeholder="Enter Votes" id="votes" name="votes" />; <input type="submit" value="Add Votes" />; </form>;
It is nothing too complicated right? Well the answer was not so straight forward. Since the input is a string it will not be able to cumulate your votes.
This is when parseInt() comes useful.
let count = 0; count = parseInt(input.value) + count; characterVote.textContent = count;
This function will return an integer from the string argument and from now it will be able to cumulate your total count.
Hope this was useful! Feel free to reach out if you have or had the same struggle with parseInt() and strings :D.
Also here is a link to more information about parseInt():
MDN web docs_:
Top comments (2)
In some cases it might be preferable to use
parseInt(value). For instance, when used with an empty string,
Or maybe you have a mix of integers and floats in your input strings,
Numberwill return the appropriate type, whereas
parseIntwill only return the whole digits up to the decimal point and drop the remainder without any rounding.
Good to know!