mortalc Posted May 24, 2010 Share Posted May 24, 2010 How would I right the paramaters for an if function as 'if x is an integer'? Link to comment Share on other sites More sharing options...
boen_robot Posted May 24, 2010 Share Posted May 24, 2010 There's no integer in JavaScript. There's only (floating point) numbers.You can check the type with the typeof() operator, like: if (typeof(x) == "number") Link to comment Share on other sites More sharing options...
chibineku Posted May 24, 2010 Share Posted May 24, 2010 You could also use the modulo:if(x%1 == 0) //no remainder when divided by one Link to comment Share on other sites More sharing options...
mortalc Posted May 24, 2010 Author Share Posted May 24, 2010 if (typeof(x) == "number") So what would I use? 'if (typeof(x) == "integer)?if(x%1 == 0) //no remainder when divided by oneSurely the remainder will always be zero?Yes, I used this code, and both X anY were written:<html><body><script type="text/javascript">var x=123;var y=123.4;if(x%1 == 0){document.write (x);}if(y%1 == 0);document.write ("<br />");{document.write (y);}</script></body></html> Link to comment Share on other sites More sharing options...
mortalc Posted May 24, 2010 Author Share Posted May 24, 2010 I've just had an Idea. Would this worK? if (x == Math.floor(x)) Link to comment Share on other sites More sharing options...
ShadowMage Posted May 24, 2010 Share Posted May 24, 2010 Nevermind. Floating points and integers are all numbers. Link to comment Share on other sites More sharing options...
jeffman Posted May 24, 2010 Share Posted May 24, 2010 This is incorrect: if(y%1 == 0);document.write ("<br />");{document.write (y);} 1. A semicolon after your if statement terminates the if-block. You have NO block before the semicolon, so the two document.write statements will execute no matter what value the if statement returns.2. But cannot simply remove the semicolon, since only the document.write ("<br />"); statement follows the conditional. It is not enclosed in braces, so the if block would terminate after executing that statement, and the second statement would execute in every case. (It is good practice to enclose ALL result blocks in braces, even if it is just one statement. You'll never make an error if you get into that habit.)Try this: if(y%1 == 0){ document.write ("<br />"); document.write (y);} Link to comment Share on other sites More sharing options...
jeffman Posted May 24, 2010 Share Posted May 24, 2010 I think boen was suggesting proof-of-concept, not code that should be used. Nevermind. Floating points and integers are all numbers. Link to comment Share on other sites More sharing options...
boen_robot Posted May 24, 2010 Share Posted May 24, 2010 I've just had an Idea. Would this worK? if (x == Math.floor(x)) Add another "=", and you should be all set, i.e. if (x === Math.floor(x)) Otherwise, there's one edge case you won't cover - an integer written as a string. Link to comment Share on other sites More sharing options...
mortalc Posted May 24, 2010 Author Share Posted May 24, 2010 Ok, thanks!Just two more things (complitely unrelated):1. How can I get the value inputted in a prompt box to be assigned to a variable (it must be a number)?2. Is window.confirm a confirm box? and How can I do something if it returns false? (i.e. if (return=false)) Link to comment Share on other sites More sharing options...
ShadowMage Posted May 24, 2010 Share Posted May 24, 2010 1. How can I get the value inputted in a prompt box to be assigned to a variable (it must be a number)?2. Is window.confirm a confirm box? and How can I do something if it returns false? (i.e. if (return=false))1. document.getElementById("element").value2. To get the result of a confirm dialog (yes, window.confirm is a confirmation box) you assign it to a variable then test the variable:var result = window.confirm("Message");if (result) { //Same as if (result == true)//Do something if OK} else {//Do something if Cancel}or if you only care about the user clicking Cancelif (!result) { //Same as if (result == false)//Do something} Link to comment Share on other sites More sharing options...
mortalc Posted May 25, 2010 Author Share Posted May 25, 2010 1. Could you right that out in full please? Basically, I want the user to input a maximum value for a. Lat's say this is called A. //output of prompt box = Avar a = Math.floor(Math.random()*A + 1); Basically that would randomize a value for a between 1 and A.2. Thanks! Link to comment Share on other sites More sharing options...
thescientist Posted May 25, 2010 Share Posted May 25, 2010 1. Could you right that out in full please? Basically, I want the user to input a maximum value for a. Lat's say this is called A.//output of prompt box = Avar a = Math.floor(Math.random()*A + 1); Basically that would randomize a value for a between 1 and A.2. Thanks! you should look that one up, its pretty easy. usually in the beginning of most tutorials...like the one's here on W3schools.http://www.w3schools.com/js/js_popup.asp Link to comment Share on other sites More sharing options...
ShadowMage Posted May 25, 2010 Share Posted May 25, 2010 1. Could you right that out in full please? Basically, I want the user to input a maximum value for a. Lat's say this is called A.//output of prompt box = Avar a = Math.floor(Math.random()*A + 1); Basically that would randomize a value for a between 1 and A. I think I misread your question. I thought you wanted to get the value of an input element.Here's the link to the window.prompt() method:http://w3schools.com/jsref/met_win_prompt.aspThat should get you going. Link to comment Share on other sites More sharing options...
mortalc Posted May 25, 2010 Author Share Posted May 25, 2010 Ok, I think I'm getting it....How can I get it the input to be a number rather than a string? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 25, 2010 Share Posted May 25, 2010 You can use parseInt and parseFloat to convert to an integer or float. Make sure to check the return value, if the function returns NaN then they didn't input a number or it couldn't be converted. The isNaN function can test for that. e.g.: var x = parseInt(input, 10);if (isNaN(x)){ alert(x + ' is not a number.');} Link to comment Share on other sites More sharing options...
mortalc Posted May 25, 2010 Author Share Posted May 25, 2010 What does the 10 do?and could I put it in a while loop (!typeof(x) === "number")?And also, if I'm putting several parameters in a while loop (e.g. while x is NaN and is not an integer [i know how to do these now]), would I seperate them with a ; or a comma?? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 25, 2010 Share Posted May 25, 2010 The 10 specifies the base to use for conversion. If you leave it out it will guess the base and may give unexpected results for some numbers. If you use parseInt on "08" for example, and don't give a base, it's not going to return 8 as the integer. and could I put it in a while loop (!typeof(x) === "number")?You can test any expression in a while loop.would I seperate them with a ; or a comma??You would use a logical operator like AND or OR. Link to comment Share on other sites More sharing options...
ShadowMage Posted May 25, 2010 Share Posted May 25, 2010 You can use parseInt and parseFloat to convert to an integer or float. Make sure to check the return value, if the function returns NaN then they didn't input a number or it couldn't be converted. The isNaN function can test for that.Why not just use casting? (I think that's what its called) Eg. var num = Number(input);You'd still have to check for NaN though.You would use a logical operator like AND or OR....or you could use && and || Link to comment Share on other sites More sharing options...
justsomeguy Posted May 25, 2010 Share Posted May 25, 2010 Why not just use casting? (I think that's what its called) Eg. var num = Number(input);You'd still have to check for NaN though.Right, parseInt simplifies the process. This is specifically what parseInt is for. There may be other ways to do it, but I've seen several bugs in people's systems where they convert a value and never bother to check that it actually converted to a number. So leaving out the validation isn't really a good idea. Link to comment Share on other sites More sharing options...
mortalc Posted May 25, 2010 Author Share Posted May 25, 2010 how would I fit parseInt into the Prompt?Ed:Or is the 'input' bit a prompt box? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 25, 2010 Share Posted May 25, 2010 You don't. You use the prompt to get the input, then you check the value. The prompt box just gets a value, it doesn't do anything else. Link to comment Share on other sites More sharing options...
mortalc Posted May 25, 2010 Author Share Posted May 25, 2010 Ok. What I meant was, how can I get the input of the prompt box to be the 'input' in parseInt()? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 25, 2010 Share Posted May 25, 2010 The return value of the prompt function will be the value they typed. Link to comment Share on other sites More sharing options...
mortalc Posted May 25, 2010 Author Share Posted May 25, 2010 So will the 'input' in parseInt() automatically be the input of the Prompt Box? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.