Fire Dragon Posted February 19, 2006 Share Posted February 19, 2006 Hi!I create this kind code,but I want know one thing before I continue developing it.I want that code decreases one value,depend on what random value comes. function decrease(){var value= Math.random()if (value<0.5){value2=value2 -10;}else{value2=value2 -20;}} I have two questions.First,can you see any errors?Second question is about Math.random code.Like you see,this is function.If I create example button,and Math.random creates example value 0.42536.Than,I press again button.Question is,do math.random create new value?Example 0.84763.Thanks for help.I write this code to paper,and I can't test it yet,because it is part of huge code.I can't see any mistakes in it,but you can't never be sure... Thanks. Link to comment Share on other sites More sharing options...
scott100 Posted February 19, 2006 Share Posted February 19, 2006 Math.random() always creates a new number, you can check this by putting it's result in an alert message. Also, if you put it within a Math.ceil() you get whole numbers ie 1 & 6 not 0.666 & 5.32656 <head><script>var value2=0;function decrease(){ var value=Math.ceil(Math.random()*10); alert("Random number is "+value); if (value<5) { value2=value2-10; document.getElementById('val2').value=value2; } else { value2=value2-20; document.getElementById('val2').value=value2; }}</script></head><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="0" id="val2" /></p></body> Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 20, 2006 Author Share Posted February 20, 2006 Math.random() always creates a new number, you can check this by putting it's result in an alert message. Also, if you put it within a Math.ceil() you get whole numbers ie 1 & 6 not 0.666 & 5.32656<script>var value2=0;function decrease(){ var value=Math.ceil(Math.random()*10); alert("Random number is "+value); if (value<5) { value2=value2-10; document.getElementById('val2').value=value2; } else { value2=value2-20; document.getElementById('val2').value=value2; }}</script><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="0" id="val2" /></p></body> <{POST_SNAPBACK}> I can't get it work.I tried modify and only copy & paste,but when I press button,I can't see any alerts,or field's value won't change.What I did wrong? Link to comment Share on other sites More sharing options...
scott100 Posted February 20, 2006 Share Posted February 20, 2006 I can't get it work.I tried modify and only copy & paste,but when I press button,I can't see any alerts,or field's value won't change.What I did wrong?<{POST_SNAPBACK}> What exactly did you "modify"? I don't know what's wrong unless you show me what changes you have made. Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 20, 2006 Author Share Posted February 20, 2006 What exactly did you "modify"? I don't know what's wrong unless you show me what changes you have made. <{POST_SNAPBACK}> I only change name of values.In this time.However,I copied exactly same code to my notepad, saved and tested,but NOTHING happened.I can check it again. Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 20, 2006 Author Share Posted February 20, 2006 Sorry It was funniest mistake what I ever made I write accidentally <script><head> I'm very sorry! Link to comment Share on other sites More sharing options...
scott100 Posted February 20, 2006 Share Posted February 20, 2006 It's ok, these things happen!! Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 20, 2006 Author Share Posted February 20, 2006 It's ok, these things happen!! <{POST_SNAPBACK}> Yeah,you are right By the way,I tried set that if value is 0 or less,code directes you to new URL,but I can't get it work right now.I tried if else statement,but I don't know why it wont work.Here is my code: else if (value<=0) { window.location("http://www.google.com") } I put it between if and else statements,but nothing happens when value is equal or under 0.What can be wrong?I also tried create own function,what checks value when user presses button,but it won't work. Link to comment Share on other sites More sharing options...
scott100 Posted February 20, 2006 Share Posted February 20, 2006 By the way,I tried set that if value is 0 or less,code directes you to new URL,but I can't get it work right now.<{POST_SNAPBACK}> That won't work because if you look at this line of code var value=Math.ceil(Math.random()*10); i used Math.ceil which rounds a number up to the nearest integer, it will never be 0.change it to Math.floor like so:var value=Math.floor(Math.random()*10); your if statement will now work for a 0. Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 20, 2006 Author Share Posted February 20, 2006 Like usual,I can't get it work right now Here is my code: <script>var value2=100;function decrease(){ var value=Math.floor(Math.random()*10); alert("Random number is "+value); if (value<5) { value2=value2-10; document.getElementById('val2').value=value2; } else if (value2<=0) { window.location("http://www.google.com") document.getElementById('val2').value=value2; } else { value2=value2-20; document.getElementById('val2').value=value2; }}</script><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="100" id="val2" /></p></body> What is wrong now?I tried,but it doesn't activate window.location,even valu2 is 0 or less. Link to comment Share on other sites More sharing options...
scott100 Posted February 20, 2006 Share Posted February 20, 2006 window.location("http://www.google.com") window.location="http://www.google.com" if (value<5) else if (value2<=0) if (value<5&&value>1) else if (value2==0) <script>var value2=100;function decrease(){var value=Math.floor(Math.random()*10);alert("Random number is "+value); if (value<5&&value>1) { value2=value2-10; document.getElementById('val2').value=value2; } else if (value2==0) { window.location="http://www.google.com"; document.getElementById('val2').value=value2; } else { value2=value2-20; document.getElementById('val2').value=value2; }}</script><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="100" id="val2" /></p></body> Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 21, 2006 Author Share Posted February 21, 2006 Thanks.It finally works like I want. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now