コード: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i < array.length; i++) { 03 if (array[i] === 4) { 04 array.splice(i, 1); 05 i--; 06 } 07 } 実行後の配列の値は何ですか?
正解:B
Initial array: [1, 2, 3, 4, 4, 5, 4, 4] The loop removes every value equal to 4: * array.splice(i, 1) removes the element at index i and shifts all later elements left. * i-- ensures the next index is checked correctly after the shift, preventing skipping elements. Step-by-step removal: * Remove the first 4 # array becomes [1,2,3,4,5,4,4] * Remove next 4 # [1,2,3,5,4,4] * Remove next 4 # [1,2,3,5,4] * Remove last 4 # [1,2,3,5] Final result: [1,2,3,5] Option B is correct. JavaScript Knowledge References (text-only) * splice(index, deleteCount) removes elements and shifts remaining items. * Adjusting the loop index when deleting prevents skipping items. * Arrays update length dynamically after splice().