Solved

Javascript If Then Return Value

Posted on 2013-10-29
6
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

739 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