I'm having problems understanding why I keep getting NaN as a return output

Viewed 45

const myFunction = function() {
   let fah;
   fah = document.getElementById('fah').value;

   let celcius;
   celcius = ((fah - 32) * 5/9);

  document.getElementById('cel').value = celcius;
};
<body>
  <main>
    <h1 class="header">Fahrenhiet to celcius</h1>
    <div class="wrapper">
      <form class="form">
        <div class="fah">
          <label for="fah" class="fahrenhiet" id="fah">&#176F</label>
          <input type="text" id="fah" class="input-fah" value="">
        </div>
        <div class="span">
          <span class="equals"> &#61 </span>
        </div>
        <div class="cel">
          <input type="text" id="cel" class="input-cel" value="">
          <label for="cel" class="celcius">&#176C</label>
        </div>
        <div class="bttn-group">
          <button class="bttn bttn-sumbit" type="" onclick="myFunction(); event.preventDefault();">Submit</button> 
          <input class="bttn bttn-reset" type="reset" value="Reset">
        </div>
      </form>
    </div>
  </main>
  <script src="script.js"></script>
</body>

I came back to this code after a week and it was working perfect before. Now all of a sudden every time I run it I keep getting NaN as my output. I've tried everything I can think of even using parseInt(), but after toying around I realize that my fah variable is not being treated as a number as far as I can tell. I tried deleting the document.getElementById('fah').value; and used a random number and it seemed to work. I'm at a loss. I'm still learning javascript so I'm sure it's something I haven't learned yet.

1 Answers

You had a duplicate ID in your HTML which is not valid and caused your code to pick up the value of the label instead of the input field.

You should also parseInt the user input to make sure it's a number.

const myFunction = function() {
   let fah;
   fah = parseInt(document.getElementById('fah').value);
// CHANGE ---^

   let celcius;
   celcius = ((fah - 32) * 5/9);

  document.getElementById('cel').value = celcius;
};
<body>
  <main>
    <h1 class="header">Fahrenhiet to celcius</h1>
    <div class="wrapper">
      <form class="form">
        <div class="fah">
          <label for="fah" class="fahrenhiet" id="fah-label">&#176F</label>
<!-- CHANGE ---------------------------------------^ -->
          <input type="text" id="fah" class="input-fah" value="">
        </div>
        <div class="span">
          <span class="equals"> &#61 </span>
        </div>
        <div class="cel">
          <input type="text" id="cel" class="input-cel" value="">
          <label for="cel" class="celcius">&#176C</label>
        </div>
        <div class="bttn-group">
          <button class="bttn bttn-sumbit" type="" onclick="myFunction(); event.preventDefault();">Submit</button> 
          <input class="bttn bttn-reset" type="reset" value="Reset">
        </div>
      </form>
    </div>
  </main>
  <script src="script.js"></script>
</body>

Related