Answer to Question #196680 in HTML/JavaScript Web Application for Sai

Question #196680

Objects with given Fruit


Given an array of objects


objectEntities in the prefilled code and fruit as an input, write a JS program to log the array of objects containing the given fruit.

Input

The input will be a single line containing a string


fruitOutput

The output should be the array of objects matching the given


fruit




Sample Input 1


apple




Sample Output 1


[


{ fruit: 'apple', vegetable: 'broccoli' },


{ fruit: 'apple', vegetable: 'cauliflower' }


]




Sample Input 2


orange




Sample Output 2


[ { fruit: 'orange', vegetable: 'mushrooms' } ]




i want code in between 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++];


}




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




function main() {


const fruit = readLine();


const objectEntities = [


{


fruit: "apple",


vegetable: "broccoli"


},


{


fruit: "kiwi",


vegetable: "broccoli"


},


{


fruit: "apple",


vegetable: "cauliflower"


},


{


fruit: "orange",


vegetable: "mushrooms"


},


];



/* Write your code here */




}


1
Expert's answer
2021-05-25T00:08:04-0400
"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++];
}
/* Please do not modify anything above this line */

function main() {
    const fruit = readLine();

    const objectEntities = [
        {
            fruit: "apple",
            vegetable: "broccoli"
        },
        {
            fruit: "kiwi",
            vegetable: "broccoli"
        },
        {
            fruit: "apple", 
            vegetable: "cauliflower"
        },
        {
            fruit: "orange", 
            vegetable: "mushrooms"
        },
    ];
 /* Write your code here */

    const res = objectEntities.filter( item => item.fruit == fruit )

    console.log(res);
}

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