0

Can someone advise how I can get my final variable to display as a number (the value of my function variable) rather than NaN?

<form id="computeroi">
                <fieldset>
                    <div class="col-1 pblue"> <p class="float-left"> 1. How many monthly visitors does your website get per month? </p>  <input type="number" value="0" id="monthlyvisitors" class="width-50" onchange="computeroi()"> </div>
                    <div class="col-1 pblue"> <p class="float-left"> 2. How many of those visitors are from a mobile device? </p>  <input type="number" value="0" id="mobilevisitors" class="width-50" onchange="computeroi()"> </div>
                    <div class="col-1 pblue"> <p class="float-left"> 3. What is your average deal worth ? </p>  <input type="number" value="0" id="dealworth" class="width-50" onchange="computeroi()"> </div>
                    <div class="col-1 pblue"> <input type="submit" value="Submit" class="width-25" id="submit" width="25"> </div>
                    <div class="col-1 "> <h1 id="newdealw"> </h1> </div>
                </fieldset>
            </form>

function computeroi() {
var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

var newdeals = (mobilevisitors / monthlyvisitors); 
var newdealw = (newdeals * dealworth);

document.getElementById('newdealw').innerHTML = newdealw;

}

2
  • 3
    Your code declares the variables monthlyv and mobilet, and then it proceeds to use mobilevisitors and monthlyvisitors in the math. Those variables are not defined, so you get NaN. Commented Dec 8, 2015 at 14:42
  • the error is here var newdeals = (mobilet / monthlyv); var newdealw = (newdeals * dealw); Commented Dec 8, 2015 at 14:48

4 Answers 4

1

mobilevisitors,monthlyvisitors,dealworth isn't declared or defined anywhere in your method .

Rather your assign value of ids monthlyvisitors,mobilevisitors,dealworth into monthlyv ,mobilet ,dealw

var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

Try like this

var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

var newdeals = ( +mobilet / +monthlyv ); 
var newdealw = (newdeals * +dealw );

N:b: + sign before variable will consider it as a number.

Sign up to request clarification or add additional context in comments.

1 Comment

Great responses guys solved it right away, silly oversight on my part! var newdeals = (3 / 100 (* monthlyv )); I need the math to do the above actually but that won't work
0
var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

var newdeals = (mobilevisitors / monthlyvisitors); 
var newdealw = (newdeals * dealworth);

there are no mobilevisitors ,monthlyvisitors variables in your code.

also use parseInt() or parseFloat() accordingly before performing mathematical operations to be safe.

Comments

0

there are no mobilevisitors ,monthlyvisitors variables in your code. juste change the last two lines like this:

var newdeals = (mobilet / monthlyv); 
var newdealw = (newdeals * dealw);

Comments

0

You have not declared variable mobilevisitors or this one monthlyvisitors so you cannot get value. You should use the variable monthlyv and mobilet into which you are getting the value of these two thing (mobilevisitors and monthlyvisitors)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.