JavaScript Javascript input returns NaN or undefiend javascript input return key,javascript input return event,javascript input

I'm making a tip calculator and I would like the tip amount to display for the user to see. The problem I'm having is the output showing up as 'NaN' or 'undefined'. I've tried making changes to my code but I keep getting the same result.

function calculateTip() {
  var billInput = document.getElementById('bill');
  var tipPercentage = document.getElementById('tip');
  var tipPercentageCalc = (tipPercentage / 100);
  var tipAmount = (bill * tipPercentageCalc).toFixed(2);
  tipAmount = tipAmount.toString();
  document.getElementById('display_text').innerHTML = 'Tip = $', +tipAmount;

};
<div id='calculate'>
  <p>Bill: $<input id="bill" type="number" name="bill" placeholder="Enter bill amount" onchange="calculateTip()"></p>
  <p>Tip: %<input id="tip" type="number" name="tip" placeholder="15%" onchange="calculateTip()"></p>
  <input type="button" name="submit" onclick="calculateTip();">
</div>
<div id="display">
  <h4 id="display_text"></h4>
</div>
Answer:1

You forgot to get the value of your fields. Because without the property .value, it returns HTMLObject.

function calculateTip() {
    var billInput = parseFloat(document.getElementById('bill').value);
    var tipPercentage = parseFloat(document.getElementById('tip').value);
    var tipPercentageCalc = (tipPercentage / 100);
    var tipAmount = (bill * tipPercentageCalc).toFixed(2);
    tipAmount = tipAmount.toString();
    document.getElementById('display_text').innerHTML = 'Tip = $', + tipAmount;

};
Answer:2

You are reading billInput and tipPercentage as HTML element objects instead of the text the user types into them, which will be their .value properties.

Answer:3
function calculateTip() {
// get the VALUE property of the textbox elements
// parseInt will turn them into numbers if they're not already.
// if they are not numbers you cannot use them in math.

  var billInput = parseInt(document.getElementById('bill').value);
  var tipPercentage = parseInt(document.getElementById('tip').value);
  var tipPercentageCalc = (tipPercentage / 100);
  var tipAmount = (billInput * tipPercentageCalc);

// isNaN stands for "is Not a Number"
// this checks if tipAmount is not a number
// if it is not we simply set it to the number 0
if (isNaN(tipAmount)) {
    tipAmount = 0;
  }

  // when you concatenate a number to a string you do not need to turn it into a string.
  // it will automatically be converted
  document.getElementById('display_text').innerHTML = 'Tip = $' + tipAmount;

};

function calculateTip() {
// get the VALUE property of the textbox elements
// parseInt will turn them into numbers if they're not already.
// if they are not numbers you cannot use them in math.

  var billInput = parseInt(document.getElementById('bill').value);
  var tipPercentage = parseInt(document.getElementById('tip').value);
  var tipPercentageCalc = (tipPercentage / 100);
  var tipAmount = (billInput * tipPercentageCalc);

// isNaN stands for "is Not a Number"
// this checks if tipAmount is not a number
// if it is not we simply set it to the number 0
if (isNaN(tipAmount)) {
    tipAmount = 0;
  }
  
  // when you concatenate a number to a string you do not need to turn it into a string.
  // it will automatically be converted
  document.getElementById('display_text').innerHTML = 'Tip = $' + tipAmount;

};
<div id='calculate'>
  <p>Bill: $<input id="bill" type="number" name="bill" placeholder="Enter bill amount" onchange="calculateTip()"></p>
  <p>Tip: %<input id="tip" type="number" name="tip" placeholder="15%" onchange="calculateTip()"></p>
  <input type="button" name="submit" onclick="calculateTip();">
</div>
<div id="display">
  <h4 id="display_text"></h4>
</div>
Answer:4

I'm going through an array to apply different classes based on a state that is set externally. This is how I do it now, but I feel like i'm repeating myself a lot. Is there a DRYer way to do it? The ...

I'm going through an array to apply different classes based on a state that is set externally. This is how I do it now, but I feel like i'm repeating myself a lot. Is there a DRYer way to do it? The ...

  1. conditionally applying class
  2. conditionally apply class angular
  3. conditionally apply class react
  4. conditionally apply class angular 4
  5. conditionally apply class angular 7
  6. conditionally apply class vue
  7. conditionally apply class angularjs
  8. conditionally apply class in angular2
  9. conditionally apply class css

<div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Act</label> <input ...

<div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Act</label> <input ...

  1. jquery dynamically add rows form
  2. dynamically add rows to form

I've 1000's of users and user data looks like the below. Some users have the devices as [{some data}] (array), some users have devices as {some data}, and some users have devices as empty []. I ...

I've 1000's of users and user data looks like the below. Some users have the devices as [{some data}] (array), some users have devices as {some data}, and some users have devices as empty []. I ...

I want to make a function that takes an array as an argument, creates a random number from 1 - 10, and runs until it creates a number that is not in the array, and returns it. Here is jsfiddle ...

I want to make a function that takes an array as an argument, creates a random number from 1 - 10, and runs until it creates a number that is not in the array, and returns it. Here is jsfiddle ...