squares of Array items
input
output
input1
[[1,2],[3,4],[5,6]]
output1
[ [ 1,4],[9,16],[25,36]]
input2
[12,[24],36,[48,60]]
output2
[144,[576],1296,[2304,3600]]
"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++];
}
function main() {
let myArray = JSON.parse(readLine())
/* Write your code here and log the output */
}
"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++];
}
function main() {
let myArray = JSON.parse(readLine())
/* Write your code here and log the output */
let res = [];
myArray.forEach(item => {
if (typeof item === 'number') {
res.push(item ** 2);
} else {
let arr = [];
if (item.length < 2) {
arr.push(item ** 2);
} else {
item.forEach(n => arr.push(n ** 2));
}
res.push(arr);
}
})
console.log(res);
}
Comments
Leave a comment