Skip to main content
nivek1802
Participating Frequently
November 2, 2021
Answered

Needing script to return a value of 1,2 or 3 when in a certain range

  • November 2, 2021
  • 2 replies
  • 1066 views

Hello, 

 

My company is doing performance evaluations and I used radio buttons to add up the score based of 15 questions with a score of 1-3 on each question. I got the total working but now they would like a 1,2 or 3 returned if it is within a range. 1-15 will equal 1, 16-30 will equal 2 and 31-45 will equal 3. 

 

I am not familiar with coding and the closest thing i know is some excel formulas. From my research into this online i have the below formula but it keeps coming up with an error that i cannot get around.

 

Any help would be greatly appreciated. 

 

Thank you

 

var v1 = this.getField("x") . value;
if (v1 <= 0) {
event.value = ">>>>>";
else if (v1 >= 1 && v1<= 15) {
event.value = "1";
}
else if (v1 >= 16 && v1<= 30) {
event value = "2";
}
else if (v1 >= 31 && <= 45) {
event value = "3";
}

This topic has been closed for replies.
Correct answer Nesa Nurani

You have few errors in your code.

1. Missing curly brackets in first condition.

2. In last two condition missing dot at event value (it's event.value).

3. Missing variable name in last condition, it should be (v1 >= 31 && v1 <= 45).

In your case you don't even need curly brackets.

Try this:

var v1 = this.getField("x").value;
if(v1 <= 0)
event.value = ">>>>>";
else if (v1 >= 1 && v1<= 15)
event.value = "1";
else if (v1 >= 16 && v1<= 30)
event.value = "2";
else if (v1 >= 31 && v1 <= 45)
event.value = "3";

2 replies

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
November 6, 2021

You have few errors in your code.

1. Missing curly brackets in first condition.

2. In last two condition missing dot at event value (it's event.value).

3. Missing variable name in last condition, it should be (v1 >= 31 && v1 <= 45).

In your case you don't even need curly brackets.

Try this:

var v1 = this.getField("x").value;
if(v1 <= 0)
event.value = ">>>>>";
else if (v1 >= 1 && v1<= 15)
event.value = "1";
else if (v1 >= 16 && v1<= 30)
event.value = "2";
else if (v1 >= 31 && v1 <= 45)
event.value = "3";

nivek1802
nivek1802Author
Participating Frequently
November 10, 2021

Nesa, 

 

This worked, thank you so much for your help. You are amazing. 

 

Thank you

Community Expert
November 3, 2021

Hi @nivek1802,

You could use the following function to get your range, pass in the variable that holds your total to the function as argument and you shall get the result.

function getRange(total)
{
	if(isNaN(total))
	{
		alert("Please input number")
		return;
	}
	if(total >= 1 && total <= 15)
		return 1
	else if(total <=30)
		return 2
	else if(total <= 45)
		return 3
}

//Pas the variable containing total in place of 30
var range  = getRange(30)
alert(range)

-Manan

nivek1802
nivek1802Author
Participating Frequently
November 3, 2021

Manan, 

 

Thank you for the revised formula, when i go to the text box where i want the result to appear, I choose Properties, go to the Calculate tab and enter the formula in the Custom Calculation section, nothing happens. I am unsure if i am putting the formula in the right area. 

 

Thank you again for your help.

BarlaeDC
Community Expert
Community Expert
November 4, 2021

HI,

 

How are you calling the code? if it is in the calculate field you would need to change a field to trigger the code, does anything appear in the console when you have run the code?