Solved

Javascript If Then Return Value

Posted on 2013-10-29
6
207 Views
Last Modified: 2013-10-29
I am stuck on what I have a feeling is a pretty simple JS.

I have a button that when clicked does a calculation based on form fields and returns a numeric value.

I would like to add an additional field to evaluate the numeric value and return a description like "Superior" "Good" "Adequate" etc.

How do I write a separate formula to return the description by evaluating the calculated number field something like:  if score less than 75 then Poor else if score between 75 and 80 then Not Adequate so on and so forth.

Thank you in advance for any help on this.
0
Comment
Question by:mounty95
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39609561
Need some context for a proper answer but something like this

function getdescription(score){
     if(score<=100){getdescription="Superior"}
     if(score<=80){getdescription="Good"}
     if(score<=75){getdescription="Adequate"}
     return getdescription
}
0
 

Author Comment

by:mounty95
ID: 39609568
What do your mean context?  

I have a button that the user clicks after filling out a form.  The value is returned in a textbox with id="s".

I want to evaluate the value in the textbox and return in a separate textbox a descriptor for that calculation.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39609571
Context is giving us some code and not just saying I have a button.

Why evaluate the textbox, just work out the description (using code like above) when the button is pressed.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:mounty95
ID: 39609669
Here is the formula that returns a value:

function calc(thisform)
{
var rf=thisform.rf.value;
var ap=thisform.ap.value;
var ts=thisform.ts.value;
thisform.s.value=math.round(rf+ap+ts)

How would I (in the same code) evaluate so that the value in s and a description get returned?

Sorry to be so vague.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39609685
function calc(thisform)
{
var rf=thisform.rf.value;
var ap=thisform.ap.value;
var ts=thisform.ts.value;
totalscore=math.round(rf+ap+ts)
thisform.s.value=totalscore

// amend these as necessary
     if(totalscore<=100){description="Superior"}
     if(totalscore<=80){description="Good"}
     if(totalscore<=75){description="Adequate"}
// add a text box to the form and assign it an id and change it below.
document.getElementById("mytextbox").value=description
}
0
 

Author Closing Comment

by:mounty95
ID: 39609714
Thank you for your help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now