Objects with given Fruit
Given an array of objects
The input will be a single line containing a string
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 */
}
"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);
}
Comments
Leave a comment