Programs & Programming notes | PDF
Audio Quick Review (5:46)
<html> <body> <script type="text/javascript"> <!-- ----PROGRAM BEGINS------- --> // lines beginning with // are comments // write text in the web page document // <BR> means new line document.write("GUESSING GAME<BR>") // store number in variable called sekrit var sekrit = 631 // prompt function knows // how to get input from user // whatever it gets, store in variable guess var guess = prompt("guess the number", "0") // loop repeats indented lines inside { } // as long as guess is not equal to sekrit while (guess != sekrit) { // if guess is too big, // say so and ask again if(guess > sekrit) { guess = prompt(guess +" too high.", "0") // otherwise, must be too small, // so say so and ask again } else { guess = prompt(guess +" too low.", "0") } } // since the loop ended, // they must have guessed right document.write("YOU WIN!!!") <!-- ----PROGRAM ENDS--------- --> </script> </body> </html>
All lines beginning with // are explanatory comments, and are not run as part of the program. (Everything between PROGRAM BEGINS and PROGRAM ENDS is the program, everything outside is there for the web page, not part of the program.)
The W3Schools.com's Tryit Editor or JS.do allow you to run javascript programs and see their result. Open one of these links (it will be easier if you do this in a new tab or window). Copy and paste the program above into the box on the left side of the linked page, replacing what was there before. Then click the Submit/Run button, and the program's output will show up in the box on the right. You can make changes in the box on the left and run the program again with changes. (The worst that can happen is that the program gets stuck asking for guesses forever and have to close the browser tab to get out of it.)