MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1// Test 'in' operator and for-in loops
2console.log('=== In Operator and For-In Tests ===');
3
4// Test 1: Basic 'in' operator with object
5console.log('\nTest 1: Basic in operator');
6const obj1 = { name: 'test', value: 42, flag: true };
7console.log("'name' in obj1: " + ('name' in obj1));
8console.log("'value' in obj1: " + ('value' in obj1));
9console.log("'missing' in obj1: " + ('missing' in obj1));
10
11// Test 2: 'in' operator with arrays
12console.log('\nTest 2: In operator with arrays');
13const arr = [10, 20, 30];
14console.log("'0' in arr: " + ('0' in arr));
15console.log("'1' in arr: " + ('1' in arr));
16console.log("'5' in arr: " + ('5' in arr));
17console.log("'length' in arr: " + ('length' in arr));
18
19// Test 3: Basic for-in loop with object
20console.log('\nTest 3: For-in loop with object');
21const obj2 = { a: 1, b: 2, c: 3 };
22let keys = '';
23for (let key in obj2) {
24 keys = keys + key + ',';
25}
26console.log('Keys: ' + keys);
27
28// Test 4: For-in loop with const
29console.log('\nTest 4: For-in with const');
30const obj3 = { x: 10, y: 20 };
31for (const prop in obj3) {
32 console.log('Property: ' + prop + ' = ' + obj3[prop]);
33}
34
35// Test 5: For-in loop accumulating values
36console.log('\nTest 5: For-in accumulating values');
37const obj4 = { first: 5, second: 10, third: 15 };
38let sum = 0;
39for (let k in obj4) {
40 sum = sum + obj4[k];
41}
42console.log('Sum of values: ' + sum);
43
44// Test 6: For-in with break
45console.log('\nTest 6: For-in with break');
46const obj5 = { a: 1, b: 2, c: 3, d: 4 };
47let count = 0;
48for (let key in obj5) {
49 count = count + 1;
50 if (count === 2) {
51 break;
52 }
53 console.log('Key: ' + key);
54}
55console.log('Stopped at count: ' + count);
56
57// Test 7: For-in with continue
58console.log('\nTest 7: For-in with continue');
59const obj6 = { a: 1, b: 2, c: 3, d: 4 };
60for (let key in obj6) {
61 if (key === 'b' || key === 'd') {
62 continue;
63 }
64 console.log('Processing: ' + key);
65}
66
67// Test 8: For-in with array
68console.log('\nTest 8: For-in with array');
69const arr2 = ['first', 'second', 'third'];
70for (let idx in arr2) {
71 console.log('Index ' + idx + ': ' + arr2[idx]);
72}
73
74// Test 9: Nested for-in loops
75console.log('\nTest 9: Nested for-in loops');
76const outer = { a: { x: 1, y: 2 }, b: { x: 3, y: 4 } };
77for (let key1 in outer) {
78 for (let key2 in outer[key1]) {
79 console.log(key1 + '.' + key2 + ' = ' + outer[key1][key2]);
80 }
81}
82
83// Test 10: In operator with nested properties
84console.log('\nTest 10: In operator with nested object');
85const nested = { outer: { inner: 'value' } };
86console.log("'outer' in nested: " + ('outer' in nested));
87console.log("'inner' in nested: " + ('inner' in nested));
88
89// Test 11: For-in counting properties
90console.log('\nTest 11: Counting properties with for-in');
91const obj7 = { prop1: 'a', prop2: 'b', prop3: 'c', prop4: 'd' };
92let propCount = 0;
93for (let p in obj7) {
94 propCount = propCount + 1;
95}
96console.log('Property count: ' + propCount);
97
98// Test 12: For-in with empty object
99console.log('\nTest 12: For-in with empty object');
100const empty = {};
101let ranOnce = false;
102for (let k in empty) {
103 ranOnce = true;
104}
105console.log('Loop ran: ' + ranOnce);
106
107// Test 13: In operator with different types
108console.log('\nTest 13: In operator checks');
109const testObj = { num: 42, str: 'hello', bool: true };
110console.log("'num' in testObj: " + ('num' in testObj));
111console.log("'str' in testObj: " + ('str' in testObj));
112console.log("'bool' in testObj: " + ('bool' in testObj));
113
114// Test 14: For-in modifying external variable
115console.log('\nTest 14: For-in with external variable');
116const data = { a: 10, b: 20, c: 30 };
117let total = 0;
118for (let key in data) {
119 total = total + data[key];
120}
121console.log('Total: ' + total);
122
123// Test 15: For-in with conditional logic
124console.log('\nTest 15: For-in with conditional');
125const items = { item1: 5, item2: 15, item3: 25, item4: 35 };
126let filtered = 0;
127for (let name in items) {
128 if (items[name] > 10) {
129 filtered = filtered + items[name];
130 }
131}
132console.log('Filtered sum (>10): ' + filtered);
133
134console.log('\n=== All in operator tests completed ===');