MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 197 lines 4.7 kB view raw
1// Test all loop types: for, while, do-while 2console.log('=== Loop Tests ==='); 3 4// Test 1: Basic while loop 5console.log('\nTest 1: Basic while loop'); 6let i = 0; 7let sum = 0; 8while (i < 5) { 9 sum = sum + i; 10 i = i + 1; 11} 12console.log('While loop sum (0-4): ' + sum); 13 14// Test 2: While loop with break 15console.log('\nTest 2: While loop with break'); 16let count = 0; 17while (count < 10) { 18 if (count === 5) { 19 break; 20 } 21 count = count + 1; 22} 23console.log('While with break, count: ' + count); 24 25// Test 3: While loop with continue 26console.log('\nTest 3: While loop with continue'); 27let j = 0; 28let evenSum = 0; 29while (j < 10) { 30 j = j + 1; 31 if (j % 2 === 1) { 32 continue; 33 } 34 evenSum = evenSum + j; 35} 36console.log('Even sum (2,4,6,8,10): ' + evenSum); 37 38// Test 4: Basic do-while loop 39console.log('\nTest 4: Basic do-while loop'); 40let k = 0; 41let result = 0; 42do { 43 result = result + k; 44 k = k + 1; 45} while (k < 5); 46console.log('Do-while sum (0-4): ' + result); 47 48// Test 5: Do-while executes at least once 49console.log('\nTest 5: Do-while executes at least once'); 50let executed = false; 51do { 52 executed = true; 53 console.log('Do-while executed even when condition is false'); 54} while (false); 55 56// Test 6: Do-while with break 57console.log('\nTest 6: Do-while with break'); 58let n = 0; 59do { 60 n = n + 1; 61 if (n === 3) { 62 break; 63 } 64} while (n < 10); 65console.log('Do-while with break, n: ' + n); 66 67// Test 7: Do-while with continue 68console.log('\nTest 7: Do-while with continue'); 69let m = 0; 70let oddSum = 0; 71do { 72 m = m + 1; 73 if (m % 2 === 0) { 74 continue; 75 } 76 oddSum = oddSum + m; 77} while (m < 9); 78console.log('Odd sum (1,3,5,7,9): ' + oddSum); 79 80// Test 8: For loop basic 81console.log('\nTest 8: For loop basic'); 82let forSum = 0; 83for (let x = 0; x < 5; x = x + 1) { 84 forSum = forSum + x; 85} 86console.log('For loop sum (0-4): ' + forSum); 87 88// Test 9: For loop with break 89console.log('\nTest 9: For loop with break'); 90let breakCount = 0; 91for (let y = 0; y < 10; y = y + 1) { 92 if (y === 5) { 93 break; 94 } 95 breakCount = breakCount + 1; 96} 97console.log('For loop with break, count: ' + breakCount); 98 99// Test 10: For loop with continue 100console.log('\nTest 10: For loop with continue'); 101let skipSum = 0; 102for (let z = 0; z < 10; z = z + 1) { 103 if (z % 2 === 0) { 104 continue; 105 } 106 skipSum = skipSum + z; 107} 108console.log('For loop skip evens (1+3+5+7+9): ' + skipSum); 109 110// Test 11: Nested while loops 111console.log('\nTest 11: Nested while loops'); 112let outer = 0; 113let nestedSum = 0; 114while (outer < 3) { 115 let inner = 0; 116 while (inner < 3) { 117 nestedSum = nestedSum + 1; 118 inner = inner + 1; 119 } 120 outer = outer + 1; 121} 122console.log('Nested while loops, count: ' + nestedSum); 123 124// Test 12: Nested for loops 125console.log('\nTest 12: Nested for loops'); 126let product = 0; 127for (let a = 1; a <= 3; a = a + 1) { 128 for (let b = 1; b <= 3; b = b + 1) { 129 product = a * b; 130 } 131} 132console.log('Last nested for product: ' + product); 133 134// Test 13: While with complex condition 135console.log('\nTest 13: While with complex condition'); 136let p = 0; 137let q = 10; 138while (p < 5 && q > 5) { 139 p = p + 1; 140 q = q - 1; 141} 142console.log('Complex while, p: ' + p + ', q: ' + q); 143 144// Test 14: For loop with let declaration 145console.log('\nTest 14: For loop with let'); 146for (let temp = 0; temp < 3; temp = temp + 1) { 147 console.log('For loop iteration: ' + temp); 148} 149 150// Test 15: While loop with single statement 151console.log('\nTest 15: While with single statement'); 152let single = 0; 153while (single < 3) single = single + 1; 154console.log('Single statement while: ' + single); 155 156// Test 16: Do-while with single statement (in block) 157console.log('\nTest 16: Do-while with single statement'); 158let singleDo = 0; 159do { singleDo = singleDo + 1; } while (singleDo < 3); 160console.log('Single statement do-while: ' + singleDo); 161 162// Test 17: Empty while loop 163console.log('\nTest 17: Empty while loop'); 164let empty = 5; 165while (empty < 5) { 166 console.log('Should not print'); 167} 168console.log('Empty while executed: no output above'); 169 170// Test 18: For loop with const 171console.log('\nTest 18: For loop counting down'); 172let countdown = 0; 173for (let val = 5; val > 0; val = val - 1) { 174 countdown = countdown + val; 175} 176console.log('Countdown sum (5+4+3+2+1): ' + countdown); 177 178// Test 19: While loop with array 179console.log('\nTest 19: While loop with array'); 180const arr = [10, 20, 30]; 181let idx = 0; 182let arrSum = 0; 183while (idx < arr.length) { 184 arrSum = arrSum + arr[idx]; 185 idx = idx + 1; 186} 187console.log('Array sum via while: ' + arrSum); 188 189// Test 20: Do-while minimal 190console.log('\nTest 20: Do-while minimal'); 191let minimal = 0; 192do { 193 minimal = minimal + 1; 194} while (minimal < 1); 195console.log('Minimal do-while: ' + minimal); 196 197console.log('\n=== All loop tests completed ===');