2016-05-12 233 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

相關問題