Answer to Question #303194 in HTML/JavaScript Web Application for king

Question #303194

Date Type Report

given an array myArray write a JS program to find the count of number,object,string,boolean. data values in the array


sample input1

[1, '2', true,{'a':'b'}, false]

sample output1

{number: 1, object:2, string:1, boolean: 2}


"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().replace(/'/g, '"'));


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


 /* Write your code here */

}



1
Expert's answer
2022-03-05T11:46:58-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() {
    let myArray = JSON.parse(readLine().replace(/'/g, '"'));
    /* Please do not modify anything above this line */

    /* Write your code here */
    const data = {
        'number': 0,
        'object': 0,
        'string': 0,
        'boolean': 0
    }

    myArray.map(e => {
        if (typeof(e) === 'number') {
            data.number++;
        } else if (typeof(e) === 'object') {
            data.object++
        } else if (typeof(e) === 'string') {
            data.string++
        } else if (typeof(e) === 'boolean') {
            data.boolean++
        }
    })
    console.log(data)
}

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