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 161 lines 4.2 kB view raw
1// Test for-of loop functionality 2 3console.log("=== For-Of Loop Tests ===\n"); 4 5// Test 1: Basic for-of with array 6console.log("Test 1: Basic for-of with array"); 7const fruits = ["apple", "banana", "cherry"]; 8let collected = []; 9for (const fruit of fruits) { 10 collected.push(fruit); 11} 12console.log(" Collected:", collected); 13 14// Test 2: for-of with Object.keys() 15console.log("\nTest 2: for-of with Object.keys()"); 16const person = { name: "Alice", age: 30, city: "NYC" }; 17let keys = []; 18for (const key of Object.keys(person)) { 19 keys.push(key); 20} 21console.log(" Keys:", keys); 22 23// Test 3: for-of with Object.keys() accessing values 24console.log("\nTest 3: for-of accessing object values"); 25const scores = { math: 95, english: 88, science: 92 }; 26let total = 0; 27for (const key of Object.keys(scores)) { 28 console.log(" " + key + ":", scores[key]); 29 total = total + scores[key]; 30} 31console.log(" Total:", total); 32 33// Test 4: for-of with let (mutable variable) 34console.log("\nTest 4: for-of with let"); 35const numbers = [1, 2, 3, 4, 5]; 36let sum = 0; 37for (let num of numbers) { 38 num = num * 2; 39 sum = sum + num; 40} 41console.log(" Sum of doubled:", sum); 42 43// Test 5: for-of with string iteration 44console.log("\nTest 5: for-of with string"); 45const str = "hello"; 46let chars = []; 47for (const char of str) { 48 chars.push(char); 49} 50console.log(" Characters:", chars); 51 52// Test 6: Nested for-of loops 53console.log("\nTest 6: Nested for-of loops"); 54const matrix = [[1, 2], [3, 4], [5, 6]]; 55let flatSum = 0; 56for (const row of matrix) { 57 for (const val of row) { 58 flatSum = flatSum + val; 59 } 60} 61console.log(" Matrix sum:", flatSum); 62 63// Test 7: for-of with break 64console.log("\nTest 7: for-of with break"); 65const items = [10, 20, 30, 40, 50]; 66let breakSum = 0; 67for (const item of items) { 68 if (item > 25) { 69 console.log(" Breaking at:", item); 70 break; 71 } 72 breakSum = breakSum + item; 73} 74console.log(" Sum before break:", breakSum); 75 76// Test 8: for-of with continue 77console.log("\nTest 8: for-of with continue"); 78const mixed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 79let evenSum = 0; 80for (const n of mixed) { 81 if (n % 2 === 1) { 82 continue; 83 } 84 evenSum = evenSum + n; 85} 86console.log(" Sum of evens:", evenSum); 87 88// Test 9: for-of in function with return 89console.log("\nTest 9: for-of with return"); 90function findValue(arr, target) { 91 for (const val of arr) { 92 if (val === target) { 93 return true; 94 } 95 } 96 return false; 97} 98console.log(" Contains 30:", findValue([10, 20, 30, 40], 30)); 99console.log(" Contains 99:", findValue([10, 20, 30, 40], 99)); 100 101// Test 10: for-of with empty array 102console.log("\nTest 10: for-of with empty array"); 103const empty = []; 104let emptyCount = 0; 105for (const x of empty) { 106 emptyCount = emptyCount + 1; 107} 108console.log(" Iterations on empty:", emptyCount); 109 110// Test 11: for-of building new array 111console.log("\nTest 11: for-of building new array"); 112const source = [1, 2, 3, 4, 5]; 113const squared = []; 114for (const n of source) { 115 squared.push(n * n); 116} 117console.log(" Squared:", squared); 118 119// Test 12: for-of with complex objects in array 120console.log("\nTest 12: for-of with objects in array"); 121const users = [ 122 { id: 1, name: "Alice" }, 123 { id: 2, name: "Bob" }, 124 { id: 3, name: "Charlie" } 125]; 126let names = []; 127for (const user of users) { 128 names.push(user.name); 129} 130console.log(" Names:", names); 131 132// Test 13: for-of with Object.keys() building result object 133console.log("\nTest 13: for-of building result object"); 134const original = { a: 1, b: 2, c: 3 }; 135const doubled = {}; 136for (const key of Object.keys(original)) { 137 doubled[key] = original[key] * 2; 138} 139console.log(" Doubled object:", doubled); 140 141// Test 14: for-of scope isolation 142console.log("\nTest 14: for-of scope isolation"); 143const vals = [100, 200, 300]; 144const results = []; 145for (const v of vals) { 146 const computed = v / 10; 147 results.push(computed); 148} 149console.log(" Results:", results); 150 151// Test 15: Chained for-of operations 152console.log("\nTest 15: Chained for-of"); 153const data = { x: 10, y: 20, z: 30 }; 154const dataKeys = Object.keys(data); 155let product = 1; 156for (const k of dataKeys) { 157 product = product * data[k]; 158} 159console.log(" Product of values:", product); 160 161console.log("\n=== All for-of tests completed ===");