var out='';
var car = {type:"Fiat", model:500, color:"white"};
// BUG #2 -- argument should be obj here not car as you use obj as variable inside
var invert = function (obj) {
var new_obj = {};
for (var prop in obj) {
if(obj.hasOwnProperty(prop)) {
var value = obj[prop];
new_obj[value] = prop;
}
}
// BUG #3 need to return the new_obj
return new_obj;
};
// BUG #1 -- need to pass car to invert() here!
for (var value in invert(car)) {
out += value + ' ';
}
document.getElementById("demo").innerHTML = out;
2条答案
按热度按时间oalqel3c1#
你有几个小毛病。我把你的小提琴修好了:
JSF中间文件:http://jsfiddle.net/o1dcaw8j/2/
pwuypxnk2#
最短的两个解决方案Vanilla JavaScript