Answer to Question #296076 in HTML/JavaScript Web Application for chethan

Question #296076

Flattening & case conversion

the given code to get output using array methods flat(),map() and string methods toLowerCase() and toUpperCase().


input

the first line input is an array nestedArray

the second line input is a number depth


output

the output containing an array with words having even lengths in lowercase and odd length in uppercase


input1

['Inspector', ['ANKLE',['HIKE']],'pawn']

output2

['INSPECTOR','ANKLE', 'hike',' pawn']


function readLine() {

 return inputString[currentLine++];

}


function main() {

 const nestedArray = JSON.parse(readLine().replace(/'/g, '"'));

 const depth = JSON.parse(readLine());

  

/* Please do not modify anything above this line */

  

 // Write your code here

  

}


1
Expert's answer
2022-02-12T01:17:26-0500
"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() {


 const nestedArray = JSON.parse(readLine().replace(/'/g, '"'));


 const depth = JSON.parse(readLine());


// Write your code here
 var newArray = nestedArray.flat(depth).map(changeCase => {
   if ((changeCase.length % 2) == 0) {
     return changeCase.toLowerCase()
   }
   else {
     return changeCase.toUpperCase()
   }
 })
 console.log(newArray)
  
  }

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