Squares of Array Items
Given an array
Sample Input 1
[[1, 2], [3, 4], [5, 6]]
Sample Output 1
[ [ 1, 4 ], [ 9, 16 ], [ 25, 36 ] ]
Sample Input 2
[12, [24], 36, [48, 60]]
Sample Output 2
[ 144, [ 576 ], 1296, [ 2304, 3600 ] ]
"use strict";
process.stdin.resume();
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++];
}
function main() {
const myArray = JSON.parse(readLine());
const deep_Map=(input,callback)=>input.map(entry=>entry.map?deep_Map(entry,callback):callback(entry))
squaresArray=deep_Map(myArray,x=>x**2)
console.log(JSON.stringify(squaresArray));
}
Comments
Leave a comment