These are chat archives for FreeCodeCamp/Help

18th
Mar 2019
nsonhouse
@nsonhouse
Mar 18 01:55 UTC
Hello all. I need some help with a bug in my code. Any takers?
<body>

<div id="guess">
        <label>Enter a number</label>
        <input type="number" id="guessNum">
        <button id="btn">CLICK</button>
</div>

<div id="output">
    <!-- output here -->
</div>

    <script>
        let randNum = Math.floor(Math.random() * 10) + 1;
        document.getElementById("btn").addEventListener("click", isCorrect);

        function isCorrect() {        
            let gn = document.getElementById("guessNum").value;

            if(gn === randNum){
                document.getElementById("output").innerHTML = "Good Work!";
                alert(gn +" "+ randNum);
            }
            else{
                document.getElementById("output").innerHTML = "No Match! The number is: " + randNum;
                alert(gn +" "+ randNum);
            }
        }
    </script>
</body>
nsonhouse
@nsonhouse
Mar 18 02:02 UTC
The value from the input box never matches the random number. I have determined what the random number is from the console, and still never get a match.
Christopher McCormack
@cmccormack
Mar 18 02:50 UTC
@nsonhouse the input is a string, you should either convert to a number or use loose equality == instead of ===
Christopher McCormack
@cmccormack
Mar 18 02:55 UTC
@nsonhouse also let randNum = Math.floor(Math.random() * 10) + 1; can be written let randNum = Math.ceil(Math.random() * 10);
nsonhouse
@nsonhouse
Mar 18 02:56 UTC
@cmccormack So input type="number" is a string?
Christopher McCormack
@cmccormack
Mar 18 03:01 UTC
@nsonhouse the value is, yes
nsonhouse
@nsonhouse
Mar 18 03:02 UTC
@cmccormack OK. I thought the input type forces the data type in the field?
Like text, number, date, email, etc...
Christopher McCormack
@cmccormack
Mar 18 03:04 UTC
that only affects what you see in the input on the page, but as far as getting information from an html element via JavaScript they are all strings
nsonhouse
@nsonhouse
Mar 18 03:14 UTC
@cmccormack WOW!!! Good stuff man!!! Thank you!!!
henryking123
@henryking123
Mar 18 04:58 UTC
Hello, can I ask here for help for a website I'm developing?
It's about flexbox.
zootechdrum
@zootechdrum
Mar 18 07:26 UTC
hello quick question
x = [1,2,3,4,5]

for x[-2] in x:
  print(x[-2])
gives me
1
2
3
3
5
what is x[-2]
Momozor
@momozor
Mar 18 19:50 UTC
hi
May Kittens Devour Your Soul
@diomed
Mar 18 20:21 UTC
@henryking123 dont ask if you can ask. just ask