JavaScript: Testing Objects for Properties | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

JavaScript: Testing Objects for Properties

checkObj("gift") should return "pony". checkObj("pet") should return "kitten". checkObj("house") should return "Not Found". I tried several methods as you can see no way! HELP var myObj = { gift: "pony", pet: "kitten", bed: "sleigh" }; function checkObj(checkProp) { myObj.hasOwnProperty(myObj[]); myObj.hasOwnProperty("kitten"); myObj.hasOwnProperty(checkObj); return checkObj; }

10th Oct 2018, 12:43 PM
Dzoyem Barry
Dzoyem Barry - avatar
2 Answers
+ 5
11th Oct 2018, 1:12 PM
John Wells
John Wells - avatar
0
function checkObj(obj, checkprob) { if (obj.hasOwnProperty(checkprob)) { return obj[checkprob]; } else { return "Not Found" } } console.log(checkObj({ gift: "pony", pet: "kitten", bed: "sleigh" }, "Amir"))
12th Jan 2022, 11:09 AM
M.Salem AL-najjar