Square at Alternate Indices
Given an array
Sample Input 1
[ 1, 2, 3, 4, 5 ]
Sample Output 1
[ 1, 2, 9, 4, 25 ]
Sample Input 2
[ 2, 4 ]
Sample Output 2
[ 4, 4 ]
let myArray = [];
function squared(array) {
for (int i = 0; i < array.length; i++) {
if (!array.includes(array[i] * array[i]))
array[i] *= array[i];
}
}
squared(myArray);
console.log(myArray);
Comments
Leave a comment