GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript-Fundamentals/020-while-loop.js at main · luiz158/JavaScript-Fundamentals Expand file tree/ Copy path020-while-loop.jsMore file actions 98 lines (72 loc) · 2.14 KB / Copy path020-while-loop.jsFile metadata and controls98 lines (72 loc) · 2.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 // 1: WHAT IS A WHILE LOOP? // while (condition) { // // code to be executed // } // let count = 1; // while (count <= 5) { // console.log("Count is: " + count); // count++; // } // Count is: 1 // Count is: 2 // Count is: 3 // Count is: 4 // Count is: 5 // 2: WHILE VS FOR LOOP // let password = ""; // while (password !== "javascript123") { // password = prompt("Enter the password:"); // } // console.log("Access granted!"); // 3: COMMON PATTERNS & EXAMPLES // Example 1: Reading through data until a condition is met // let numbers = [3, 7, 2, 9, 1, 5]; // let index = 0; // while (index < numbers.length && numbers[index] !== 9) { // console.log("Current number: " + numbers[index]); // index++; // } // console.log("Found 9 at index: " + index); // Example 2: Countdown timer // let seconds = 5; // while (seconds > 0) { // console.log("Time remaining: " + seconds + " seconds"); // seconds--; // } // console.log("Time's up!"); // Example 3: Processing until a flag changes // let dataProcessed = false; // let attempts = 0; // while (!dataProcessed && attempts < 10) { // console.log("Processing attempt: " + (attempts + 1)); // // Simulate some processing // if (Math.random() > 0.7) { // dataProcessed = true; // console.log("Data processed successfully!"); // } // attempts++; // } // if (!dataProcessed) { // console.log("Failed to process data after 10 attempts"); // } // 4: COMMON PITFALLS // let i = 1; // while (i <= 5) { // console.log(i); // // Oops! Forgot to increment i // } // let x = 0; // while (x = 5) { // Should be x !== 5 or x < 5 // console.log(x); // x++; // } // 5: PRACTICAL EXERCISE let randomNumber = 0; let attempts = 0; while (randomNumber !== 7) { randomNumber = Math.floor(Math.random() * 10) + 1; attempts++; console.log("Attempt " + attempts + ": Generated " + randomNumber); } console.log("Finally got 7 after " + attempts + " attempts!"); // 6: CONCLUSION |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |