2016-05-12 199 views
-1

我被困在一个作业问题上。我如何使我的代码工作?我不知道为什么我的代码不起作用

var myObj = { 
    gift: "pony", 
    pet: "kitten", 
    bed: "sleigh" 
}; 

function checkObj(checkProp) { 
    // Your Code Here 
    if (myObj.hasOwnProperty(checkObj) === true) { 
return myObj[checkObj]; 
} else return "Not Found"; 
} 
myObj.hasOwnProperty(""); 
// Test your code by modifying these values 
checkObj("gift"); 
+4

措辞不好的问题。代码应该做什么?你期待什么结果?你得到什么结果?你试图做些什么来调试?这项工作的结果是什么? – lurker

回答

3

检查你的变量名

var myObj = { 
    gift: "pony", 
    pet: "kitten", 
    bed: "sleigh" 
}; 

function checkObj(checkProp) { 
    // Your Code Here  vv HERE vv 
    if (myObj.hasOwnProperty(checkProp) === true) { 
return myObj[checkProp]; // <= and here 
} else return "Not Found"; 
} 
myObj.hasOwnProperty(""); 
// Test your code by modifying these values 
checkObj("gift"); 
1

你的变量名是错误的,你对你的if/else语句中缺少括号。

试试这个:

var myObj = { 
    gift: "pony", 
    pet: "kitten", 
    bed: "sleigh" 
}; 

function checkObj(checkProp) { 
    if (myObj.hasOwnProperty(checkProp) === true) { //checkProp, not checkObj 
    return myObj[checkProp]; //checkProp, not checkObj 
    } else { // you were missing this opening brace 
    return "Not Found"; 
    } 
} 

myObj.hasOwnProperty(""); 
alert(checkObj("gift")); 

this working fiddle

相关问题