const matrix = [[1, 2], [3, 4]]; let result = ''; for (let i = 0; i < matrix.length; i++) { for (let j = 0; j < matrix[i].length; j++) { result += matrix[i][j] + '-'; } } console.log(result);
The outer loop goes through each row, and the inner loop goes through each element in that row. So the elements are added in row-major order: 1, 2, then 3, 4.
const arr = [10, 20, 30, 40, 50]; let output = ''; for (let i = arr.length - 1; i >= 0; i -= 2) { output += arr[i] + ','; } console.log(output);
The loop starts at index 4 (value 50), then goes to index 2 (value 30), then index 0 (value 10), adding each to the output string.
const nums = [1, 2, 3, 4]; for (let i = 0; i <= nums.length; i++) { console.log(nums[i].toString()); }
The loop runs while i is less than or equal to nums.length, but nums.length is 4 and valid indices are 0 to 3. Accessing nums[4] is undefined and causes a runtime error.
The output shows elements traversed in a zigzag pattern: first row left to right (1,2), second row right to left (4,3), then third row left to right (5,6).
const arr = [1, 2, 2, 3, 4, 4, 4, 5]; const uniqueSet = new Set<number>(); for (const num of arr) { uniqueSet.add(num); } console.log(uniqueSet.size);
The array has the numbers 1, 2, 3, 4, 5. Even though some repeat, the set keeps only unique values, so size is 5.
