1 Input
[ 1, 2, 3, 4, 5 ]
1 Output
[ 1, 2, 9, 4, 25 ]
2 Input
[ 2, 4 ]
2 Output
[ 4, 4 ]
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString.trim().split("\n").map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/*not modify above this line */
function main() {
const myArray = JSON.parse(readLine());
/* Write your code here */
let array = [1,2,3,4,5];
function square(arr){
let newArr = [];
for(var i = 0 ; i < arr.length; i++){
newArr.push(arr[i++]);
}
let mappedArr = arr.map((num) =>{
for(let i = 0; i < newArr.length; i++){
if(num === newArr[i]){
num = num**2;
}
}
return num;
});
console.log(mappedArr);
}
square(array);
Comments
Leave a comment