• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Solved. Thanks people.

HAL9000

Lifer
Hey people,

How do you write a JavaScript function to check if item is bigger an a but less than b?

Thanks in advance.

Neckarb
 
Code:
function test(val)
{
   return (val > a) ? ((val < b) ? true : false) : false;
}

This is obviously the best solution.

an alternative would be

Code:
function test(val)
{
   var output = (a - val) * (val - b) > 0;
   return output
}
 
how is that the best solution (implies it is better than Mr Chad's solution). when it detects val > a to be false it doesn't even go on with value < b, it is unnecessary to do all the low level optimization.

it's the same...
 
Last edited:
ok great thanks guys, the first solution works, but for some reason I can only get it to work if the values are not the results of prompts? I.e. If I assign the value it works fine, but if I have a prompt to assign the values to the variables it doesn't work?

Any ideas?

I.e. I'm trying to do this:
Code:
Result = value > bottom_range && value < top_range

after two prompts for bottom_range and top_range

Code:
bottom_range = prompt (); top_range = prompt ();
 
Last edited:
how is that the best solution (implies it is better than Mr Chad's solution). when it detects val > a to be false it doesn't even go on with value < b, it is unnecessary to do all the low level optimization.

it's the same...

I was being facetious.
 
Slight update, it does work with a prompt, but it displays the wrong results for example the result will show 12500 is Greater than 10, but less than 50,000

Similarly if I input 10 as the bottom and 1000 as the top no results will be displayed.

These problems only occur when prompting for the values?

Suggestions?
 
I'm not intimately familiar with JS but I would be checking for overflows and possible type mismatches as a cause for your problem now.
 
Slight update, it does work with a prompt, but it displays the wrong results for example the result will show 12500 is Greater than 10, but less than 50,000

Similarly if I input 10 as the bottom and 1000 as the top no results will be displayed.

These problems only occur when prompting for the values?

Suggestions?

Do a parseInt() on the values returned from the prompt when doing the compare. You'll want some validation to ensure the user enters a numeric value.
 
**content removed**

Keep your posts on topic, and please refrain from negative editorializing about the content of other people's threads. There are plenty of forums on this board where that passes, but this isn't one of them.

Markbnj
Programming moderator
 
Last edited by a moderator:
Back
Top