tinfanide Posted June 26, 2011 Share Posted June 26, 2011 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>JS: Incremental Counter</title><script>var y = 0;function add(){ var num = document.getElementById('num'); var n = document.getElementById('n'); var cats = ["Dino", "Jarque", "Marble", "Pineapple"]; num.value = cats[y]; y++; } </script></head><body><input id="num" type="text" /><div id="n" style="display:inline;"></div><br /><input onclick="add()" type="button" value="Click!" /></body></html> How could I make it stop before it shows "undefined" clicking the button?I've tried to use if and break, but dreamweaver said it was wrong codes.Thank you. Link to comment Share on other sites More sharing options...
samerhannoun Posted June 26, 2011 Share Posted June 26, 2011 its not a good programming practice, but try GOTO var pastures = getPastures();var i, pastureLen = pastures.length;pastureLoop:for (i = 0; i < pastureLen; i++){ var pasture = pastures; var cows = pasture.getCows(); var j, numCows = cows.length; for (j = 0; j < numCows; j++) { var cow = cows[j]; if (cow.isEating()) { continue pastureLoop; } } // No cows were eating, so fire the callback for pasture pasture.executeCallback(); // or whatever}source: http://blog.stchur.com/2007/03/09/the-java...goto-statement/ Link to comment Share on other sites More sharing options...
birbal Posted June 26, 2011 Share Posted June 26, 2011 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS: Incremental Counter</title> <script> var y = 0; function add(){ var num = document.getElementById('num'); var n = document.getElementById('n'); var cats = ["Dino", "Jarque", "Marble", "Pineapple"]; num.value = cats[y]; y++; } </script> </head> <body> <input id="num" type="text" /> <div id="n" style="display:inline;"></div> <br /> <input onclick="add()" type="button" value="Click!" /> </body> </html> How could I make it stop before it shows "undefined" clicking the button?I've tried to use if and break, but dreamweaver said it was wrong codes.Thank you. Where is your loop? i cant see any...You can use for loop or for..in loop to loopthrough the all array element.You can use array length to determine till which index the loop should go on , so that no undefined could occure Link to comment Share on other sites More sharing options...
jeffman Posted June 26, 2011 Share Posted June 26, 2011 It's not a true loop, but the idea is similar. Every time the button clicks, the text advances to the next array element. After 4, there are no elements.What yu need is a way to compare the value of y against the length of the array. Keep in mind that the array starts at 0. Try adding this to the BOTTOM of your function, after y++ : if (y >= cats.length) { y = 0;} Link to comment Share on other sites More sharing options...
tinfanide Posted June 26, 2011 Author Share Posted June 26, 2011 It's not a true loop, but the idea is similar. Every time the button clicks, the text advances to the next array element. After 4, there are no elements.What yu need is a way to compare the value of y against the length of the array. Keep in mind that the array starts at 0. Try adding this to the BOTTOM of your function, after y++ :if (y >= cats.length) { y = 0;} Exactly what I wanted...Your description saves me a lot of words and I realised it's as simple as the code is above. Link to comment Share on other sites More sharing options...
tinfanide Posted June 26, 2011 Author Share Posted June 26, 2011 Where is your loop? i cant see any...You can use for loop or for..in loop to loopthrough the all array element.You can use array length to determine till which index the loop should go on , so that no undefined could occureNo, it's not a true loop. In that sense I thought it should be. Best explained in Deirdre's Dad's answer. Link to comment Share on other sites More sharing options...
tinfanide Posted June 26, 2011 Author Share Posted June 26, 2011 its not a good programming practice, but try GOTO var pastures = getPastures();var i, pastureLen = pastures.length;pastureLoop:for (i = 0; i < pastureLen; i++){ var pasture = pastures; var cows = pasture.getCows(); var j, numCows = cows.length; for (j = 0; j < numCows; j++) { var cow = cows[j]; if (cow.isEating()) { continue pastureLoop; } } // No cows were eating, so fire the callback for pasture pasture.executeCallback(); // or whatever}source: http://blog.stchur.com/2007/03/09/the-java...goto-statement/ Can ya please explain a little bit about the codes above?I just have no idea of what's going on... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.