Answer to Question #207168 in HTML/JavaScript Web Application for jayanth

Question #207168

Trekking Kit

Given an object

trekkingKit in the prefilled code and item item as an input, write a JS program to add the method isKitContains to the constructor function Trekking using the prototype. The method

isKitContains checks whether the trekkingKit contains the given item.

Quick Tip

The Object.getOwnPropertyNames() method gives an array containing the properties of the object.

Input

The input will be a single line containing a string

itemOutput

The output should be a single line containing a boolean value

Sample Input 1

ruckSackBag

Sample Output 1

true

Sample Input 2

surfboard

Sample Output 2

false




1
Expert's answer
2021-06-15T07:08:37-0400
const givenItem = ['ruck', 'sack', 'bag']


function Trekking(item) {
    this.item = item;
}


const trekkingKit = {
    ruckSackBag: givenItem,
}
Trekking.prototype.isKitContains = function(obj) {
    return Object.getOwnPropertyNames(obj).some( i => i === this.item )
}


const trekking = new Trekking('ruckSackBag');
const trekking1 = new Trekking('surfboard');
const restTrekking = trekking.isKitContains(trekkingKit);
const restTrekking1 = trekking1.isKitContains(trekkingKit);


console.log(restTrekking);
console.log(restTrekking1);

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