Hello and welcome to our community! Is this your first visit?
AdSlot7
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Check for range 0-99

1. ## Check for range 0-99

Hi guys, I was wondering how I can check if the numbers for each type of candy is between 0-99? Any help would be greatly appreciated.
Code:
```function totalCost(){
//declare variables
var snicker = parseInt(document.getElementById("snicker").value);

var twix = parseInt(document.getElementById("twix").value);
var chips = parseInt(document.getElementById("chips").value);
var salesTax = 0.05;
var total = 0;

if (!isNaN(snicker)) {
total += (snicker * .59);
}

if (!isNaN(twix)) {
total += (twix * .49);
}

if (!isNaN(chips)) {
total += (chips * .39);
}

total += (salesTax * total);

if (total != 0) {
alert("The total is " + total);
return false;
} else {
alert("Invalid total cost, please make a selection");
}
}```

2. AdSlot6
3. You can just use less than and greater than symbols. Then, you won't even need to use `!isNaN(...)` because NaN isn't bigger or smaller than anything and will always return false.

Also, I made parseInt use base-10 for you. Just in-case any weird stuff happens.

Code:
```function inRange(n) {
// easily change the min/max values here
var min = 1;
var max = 99;

return n >= min && n <= max;
}

function totalCost() {
//declare variables
var snicker = parseInt(document.getElementById("snicker").value, 10);
var twix = parseInt(document.getElementById("twix").value, 10);
var chips = parseInt(document.getElementById("chips").value, 10);
var salesTax = 0.05;
var total = 0;

if ( inRange(snicker) ) {
total += snicker * .59;
}

if ( inRange(twix) ) {
total += twix * .49;
}

if ( inRange(chips) ) {
total += chips * .39;
}

total += salesTax * total;

if (total > 0) {
alert("The total is " + total);
return false;
} else {
alert("Invalid total cost, please make a selection");
}
}```

4. you don't need javascript to do that anymore

Code:
` <input id=twix min=0 max=99 type=number />`

this is a more semantic solution and allows the visually impaired to become aware of the input constraints instead of hiding the rules in javascript.

it goes without saying that it's a far simpler approach to implement and customize...

you can drop in an HTML5 shim if you need to support legacy browsers.

5. Hi Thank You both for the suggestions. They both work great. Just curious if I wanted to place an alert message in each one. For an example right now if the user enters valid numbers for twix and snickers but not chips it still gives the correct amount but ignores chips. Can I change it so it gives an error message? If it too complicated, I am fine leaving it the way it is. Thanks.

6. You can add "required" to it to stick with rnd_me's modern ways:

Code:
`<input ... required>`
The same things rnd_me said about support for older browsers goes here, too. Keep in mind that any real validation (if this is more than just an exercise) should be done server-side, never client-side. Client-side validation is only for a fast feedback to improve the user experience, but not safe at all since it can be tricked with almost no effort.

7. Code:
```if ( inRange(snicker) ) {
total += snicker * .59;
}
else {
alert ("You must enter a value from 0 to 99 for snickers");
return false;
}```
But alerts are considered to be obsolete. In practice you should use DOM methods to display a message to your user.

8. Originally Posted by Philip M
In practice you should use DOM methods to display a message to your user.
`alert` is a DOM method, just saying, lol. You may want to re-phrase that.

9. Originally Posted by joesimmons
`alert` is a DOM method, just saying, lol. You may want to re-phrase that.
Hmm. alert() is a method of the Javascript window object.

innerHTML is a property of the HTML DOM element object. That is what I was indicating.

I am aware that the innerHTML property is not actually part of the official DOM specification. Despite this, it is supported in all major browsers, and has had widespread use across the web for many years. DOM, which stands for Document Object Model, is the hierarchy used to access and manipulate HTML objects from within JavaScript.

10. Originally Posted by Philip M
Hmm. alert() is a method of the Javascript window object.
It's not actually a part of JavaScript, though, just like setTimeout, prompt, etc.

If you run it in an environment with no context, those methods won't work because they don't exist.

For example... run this in the latest Firefox's location bar:
Code:
`javascript: alert(5);`
It won't work... `uncaught exception: ReferenceError: alert is not defined`
It's because the browser provides that method. JavaScript run in the location bar in Firefox runs with no context (no alert, no DOM, etc).

11. Originally Posted by joesimmons
It's not actually a part of JavaScript, though, just like setTimeout, prompt, etc.

If you run it in an environment with no context, those methods won't work because they don't exist.

For example... run this in the latest Firefox's location bar:
Code:
`javascript: alert(5);`
It won't work... `uncaught exception: ReferenceError: alert is not defined`
It's because the browser provides that method. JavaScript run in the location bar in Firefox runs with no context (no alert, no DOM, etc).
When I type javascript:alert(5) into IE's address bar I get an alert with text 5. Same in Chrome. I don't see how it can not be a part of Javascript.

12. Originally Posted by Philip M
When I type javascript:alert(5) into IE's address bar I get an alert with text 5. Same in Chrome. I don't see how it can not be a part of Javascript.
no, your other latest firefox; you're not paying attention...

i think what joesimmons is driving at is that alert, escape, atob, and even window, etc are defined by the DOM/Browser/Host, not by ECMAScript itself...

13. Originally Posted by Philip M
When I type javascript:alert(5) into IE's address bar I get an alert with text 5. Same in Chrome. I don't see how it can not be a part of Javascript.
The browser provides those functions. They are not in the language itself.

I told you to do it in Firefox location bar because they run the code with no context (aka pure JavaScript/ECMAScript).

It works in IE because IE provides those functions for script you run in the location bar.

I feel like you're not reading what I'm typing, maybe?

14. Originally Posted by joesimmons
The browser provides those functions. They are not in the language itself.

I told you to do it in Firefox location bar because they run the code with no context (aka pure JavaScript/ECMAScript).

It works in IE because IE provides those functions for script you run in the location bar.

I feel like you're not reading what I'm typing, maybe?
Seems to me another pointless argument. I am not a fan of Firefox. Suggest you drop it now, and rather than picking flies prefer to focus on helping the people who ask the questions.

15. It's not even an argument. I'm trying to HELP you understand... It has nothing to do with Firefox and how much you like it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
AdSlot4
AdSlot5