-4

I want the code to read the input value for Bz and give a greeting along with the answer from the equation. This is what I have this far. It's not giving a 'greeting' after the computation. Help?

  <html>
  <body>

<form id="function" onsubmit="return false" oninput="o.value = 3.21 + (9.63 * parseInt(Bz.valueAsNumber))">
    3.21+(9.63 *<input name="Bz" type="number" step="any">) =
   <output name="o" for="intial set Bz">0</output>&nbsp;nT

</form>

<div id="mys1"></div>

<script language="JavaScript">   
 function myMath() 
 {
    var Bz;
    var greeting; 
    if(Bz>-50)
    {
         if (Bz>-30)
         {
               greeting = "None"; 
         }
         else  
         {
               greeting = "Small ";
         }
         else 
         { 
               if (Bz> -80) 
               {
                     greeting = "Mild ";
               }
               else 
               {
                     greeting = "Large ";
               }
          }
     }
 document.getElementById("mys1").innerHTML= greeting;
 };  
</script>

</body>
</html>
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
maebee_me
  • 19
  • 5

2 Answers2

0

Where are you calling your function myMath() from? I guess it is not being fired.

Also, the way you wrote your ifs statements is pretty ugly. This would be much nicer:

if(Bz>-30)
{
    greeting = "Small";
}
else if(Bz>-50)
{
    greeting = "Mid ";
}
else if(Bz>-80)
{
    greeting = "Large";
}
else
{
    greeting = "None";
}

Beware that I wrote this trying to guess what greeting message you actually want. Notice that if the first condition is satisfied, the other conditions will not even be checked at all.

pablito.aven
  • 1,135
  • 1
  • 11
  • 29
  • I forgot one line of code after the equation:
    – maebee_me Jun 18 '15 at 17:54
  • 1
    Ok, then the problem is that your `if` are wrong. If you explain me what greeting you want in which case I would write the ifs for you. Also, are there any errors in your console? If there are you should add them to your post too – pablito.aven Jun 18 '15 at 18:09
  • There are no errors showing after I took it out of the format I had initially. For Bz > -80, I wanted it to greet Large. For Bz > -50, Mild. Bz > -30, Small. It was suppose to analysis the input and greet determining the range it fit best with. – maebee_me Jun 19 '15 at 12:39
  • @maebee_me Alright, I have fixed the code in the answer. Please check it out. If the value is any value bigger than -30, greeting is small. If it is between -50 and -30, the value is mid. If it is between -80 and -50, it is Large. If it is less than -80(Because it doesn't fit in any of the other cases), then the greeting is "None". I hope this helped you, please let me know if you still can't do it. – pablito.aven Jun 19 '15 at 13:47
  • regardless of the number, the only thing that shows is 'None'. – maebee_me Jun 19 '15 at 14:10
  • I added a new answer, please check it out and upvote me if it works. – pablito.aven Jun 19 '15 at 14:20
0

I found your problem. Bz value is not being loaded. Seting input name attribute will not store the value into a variable. That attribute is for form submission.

What you should do is:

function myMath() 
 {
    var Bz = $("input[name='Bz']").val();
    ...
    ...
    ...
 }
pablito.aven
  • 1,135
  • 1
  • 11
  • 29
  • '$' is jquery. I don't have that in my script. Is there any other way to do it without jquery? – maebee_me Jun 19 '15 at 14:50
  • So, can't you use JQuery for some reason? [Here](http://stackoverflow.com/questions/11563638/javascript-get-input-text-value) you can find how to target the desired input without JQuery. If you can assign an ID to it, it is just `var Bz = document.getElementById("your_input_id").value;` – pablito.aven Jun 19 '15 at 15:12
  • 1
    Got it! Thanks for the help. – maebee_me Jun 19 '15 at 15:33