Quick JavaScript Help

I need help writing javascript

In English here is what I need converted to java script, please

If the Feild "Term" is null or is <= 12 then = 1 else
If the Feild "Term" is >= 13 & <=24 then = 2 else
If the Feild "Term" is >= 25 & <=36 then = 3 else
If the Feild "Term" is >= 37 & <=48 then = 4 else
If the Feild "Term" is >= 49 & <=60 then = 5
MPDenverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eric CurziCommented:
Hello there. It looks like you're taking a "Term" value as input and changing it to another number using a set of conditions. Here it is a direct translation of it into JS:

if (Term === null || Term <= 12) { Term = 1 }
else if (Term >= 13 && Term <= 24) { Term = 2 }
else if (Term >= 25 && Term <= 36) { Term = 3 }
else if (Term >= 37 && Term <= 48) { Term = 4 }
else if (Term >= 49 && Term <= 60) { Term = 5 }

Open in new window


I see that you did not a specify a "catch-all" in your example, which could lead to trouble if the incoming value of Term is not of proper type or numerical range. You can add this to the end of the above example as a safeguard. Modify it as you see fit, depending on how you want to handing unexpected input.

else { Term = 1 }

Open in new window


Side note:
Are you sure that you want the first condition to check for null, by the way? There is nothing strictly wrong with this, but I am curious if it is intentional or otherwise.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
You can do this with a formula
Math.floor((i-1)/12+1))

Open in new window

Sample script below shows all values between 0 and 60 with corresponding output.

<script>

for(var i = 0; i <= 60; i++) test(i);


function test(i)
{
	console.log(i + ': ' + Math.floor((i-1)/12+1));
}
</script>

Open in new window

Output
0: 0
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 1
9: 1
10: 1
11: 1
12: 1
13: 2
14: 2
15: 2
16: 2
17: 2
18: 2
19: 2
20: 2
21: 2
22: 2
23: 2
24: 2
25: 3
26: 3
27: 3
28: 3
29: 3
30: 3
31: 3
32: 3
33: 3
34: 3
35: 3
36: 3
37: 4
38: 4
39: 4
40: 4
41: 4
42: 4
43: 4
44: 4
45: 4
46: 4
47: 4
48: 4
49: 5
50: 5
51: 5
52: 5
53: 5
54: 5
55: 5
56: 5
57: 5
58: 5
59: 5
60: 5

Open in new window

1
Julian HansenCommented:
For your case
var answer = Term ? Math.floor((Term-1)/12+1)) : 1;

Open in new window

0
MPDenverAuthor Commented:
Thanks Eric this was really helpful for my learning.
0
Julian HansenCommented:
If you want to go the verbose route then you don't need the lower bound. In other words
if (Term === null || Term <= 12) { Term = 1 }
// YOU CAN ONLY GET HERE IF Term > 12 SO NO
// NEED TO EXPRESSLY TEST FOR IT
else if (Term <= 24) { Term = 2 }
// SAME HERE - YOU ONLY GET HERE IF Term > 24
else if (Term <= 36) { Term = 3 }
// ETC
else if (Term <= 48) { Term = 4 }
else { Term = 5 }

Open in new window

The last statement should always be the default value (final else) - otherwise you open yourself to a situation where your input data might not match a condition and you end up with unexpected results.

So if 60 is the maximum value then don't use else if on the last rule - use else. If 60 is not the maximum value then add an else to the end with the default value you want Term to have if no other rules match.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.