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
}
"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)
}
Comments
Leave a comment