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.
The input will be a single line containing a string
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
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);
Comments
Leave a comment