NickBomb Posted June 13, 2007 Share Posted June 13, 2007 Hi!I need help with an if thingie: var r=Math.random() if (r<=0.1) // 1 { monsternamn = "Råtta" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 10 monsterexp = 10 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.1 && r<=0.3) //2 { monsternamn = "Padda" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 7 monsterexp = 7 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.2 && r<=0.4) //3 { monsternamn = "Groda" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 5 monsterexp = 5 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.3 && r<=0.5) //4 { monsternamn = "Vägg" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 100 monsterexp = 100 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.4 && r<=0.6) //5 { monsternamn = "Gris" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 14 monsterexp = 14 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.5 && r<=0.7) //6 { monsternamn = "Noshörning" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 55 monsterexp = 55 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.6 && r<=0.8) //7 { monsternamn = "Älg" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 43 monsterexp = 43 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.7 && r<=0.9) //8 { monsternamn = "Fågel" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 10 monsterexp = 10 document.getElementById('textmonsterhp').value=monsterhp; } else if (r=>0.8) // 9 { monsternamn = "Mask" document.getElementById('textmonsternamn').value=monsternamn; monsterhp = 1 monsterexp = 1 document.getElementById('textmonsterhp').value=monsterhp; } I get syntax error in: else if (r=>0.1 && r<=0.3) //2 Please help, I don't get what's wrong... Link to comment Share on other sites More sharing options...
jesh Posted June 13, 2007 Share Posted June 13, 2007 Greater than or equal to is >= rather than =>. Link to comment Share on other sites More sharing options...
NickBomb Posted June 13, 2007 Author Share Posted June 13, 2007 Ok....But I don't get where to replace, when I changed it, it still gave the same error (syntax error).Sorry for such n00bieness, but I really don't get these => and == and === and >= and.... well operators in short Can you make the code correct? if you have time and if you want to... Link to comment Share on other sites More sharing options...
jesh Posted June 13, 2007 Share Posted June 13, 2007 I think you ought to read over the section of the javascript tutorial which deals with operators. It could be of great help:http://www.w3schools.com/js/js_operators.aspIn the meantime, you might be better served with something like this: if(r <= 0.1) // 1{}else if (r <= 0.2) // 2{}else if (r <= 0.3) // 3{}else if (r <= 0.4) // 4{}// and so on and so on... Or with this: var r = parseInt(Math.random() * 10)if(r == 1){}else if (r == 2){}else if (r == 3){}// and so on and so on. Link to comment Share on other sites More sharing options...
NickBomb Posted June 13, 2007 Author Share Posted June 13, 2007 Thank you VERY much!I did not know you could do that the randomizing just randomizes a integer, thank you very much!!How do I do that it randomizes i.e. a integer between 1 - 50? is it just to putvar r = parseInt(Math.random() * 50)or is it more advanced? Link to comment Share on other sites More sharing options...
jesh Posted June 13, 2007 Share Posted June 13, 2007 Math.random() returns a number between 0 and (almost) 1. Let's say you wanted to return a number between 1 and 10 instead. You could do it like this: var r = Math.random(); // number between 0 and 0.99999999r = r * 10; // number between 0 and 9.99999999r = Math.floor(r); // number between 0 and 9;r = r + 1; // number between 1 and 10; A more simplified version is: var r = Math.floor(Math.random() * 10) + 1; Link to comment Share on other sites More sharing options...
NickBomb Posted June 14, 2007 Author Share Posted June 14, 2007 Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.