Answer to Question #205968 in HTML/JavaScript Web Application for Praveen

Question #205968

Squares of Array Items

Given an array

myArray, write a JS program to get the squares of each item in the given myArray.Input

  • The input will be a single line containing an array myArray
  • myArray consists of numbers and arrays

Output

  • The output should be an array containing the square of each item

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 ] ]




1
Expert's answer
2021-06-11T17:02:31-0400
    "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));


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS