JS对象和函数更正

时间:2015-06-04 00:26:04

标签: javascript html

我有一个家庭作业来纠正一些阻止执行功能的Javascript代码。我已经纠正了我能找到的一切,甚至用JSFiddle和JSHint来确定。所以没有留下语法错误,但必须存在逻辑错误,因为没有任何工作。我很欣赏我的代码,我确定这是一个我只是忽略的小问题。谢谢!

(这里一定有一些混乱的代码,我试图改变多少事情,为此道歉)。

首先是非常基本的HTML:

<body>
<p id="message"></p>
<p id="movies"></p>
<p id="object"></p>
</body>

然后JS:

// Define variables
var output = document.getElementById("message");
var BR = "<br>";
var makeBreakfast;
var mood;
var energyLevel;
var doWork;
var powerOn;
var command;
var duration;
var quitWork;

// Define robot object
var robot = {
material: "titanium",
mood: "happy",
energyLevel: 100,
powerOn: false,
command: "Sweeping the floor",
duration: 10,
favoriteMovies: ["2001: A Space Odyssey", "The Terminator", "I, Robot",        "WALL-E", "Short Circuit", "Forbidden Planet"]
};


makeBreakfast = function (newMood) {
mood = newMood;
if (mood === "Happy") {
    output.innerHTML += "Here are your waffles and milk, master." + BR;
} else {
    output.innerHTML += "Here is your burnt toast and lukewarm water,    master." + BR;
    energyLevel = 50;
}
};
doWork = function () {
if (!powerOn) {
    powerOn = true;
    output.innerHtml += "My current task is: " + command + ". Duration: " +     duration + " minutes." + BR;
}
};
quitWork = function () {
if (powerOn) {
    energyLevel = 90;
    powerOn = false;
    command = "Taking a nap";
}
};

// Make robot do housework
doWork();
quitWork();

完整作业如下: 1)更正代码,并使doWork和quitWork正确执行。 2)调用makeBreakfast参数并使用字符串“mad”作为参数。 3)添加一个新方法并调用它来显示一个字符串(离开这部分,现在没用) 4)列出所有机器人最喜欢的电影,每个都在新的一行。 5)使用命名索引语法将名为language的新属性添加到robot对象。使用您选择的语言对其进行初始化。 6)使用命名索引语法,遍历对象并列出新行上的每个属性和值对。在新段落中显示结果。

我真的只想要#1的帮助,但如果看到完整的任务有帮助,就是这样。谢谢大家!

1 个答案:

答案 0 :(得分:1)

需要把它作为一个对象,告诉可怜的笨蛋让你吃早餐!

Full Difference

// Define variables
var output = document.getElementById("message");
var movies = document.getElementById("movies");
// Define variables
var output = document.getElementById("message");
var movies = document.getElementById("movies");
var object = document.getElementById("object");
var BR = "<br>";

// Define robot object
var robot = {
    material: "titanium",
    mood: "Happy",
    energyLevel: 100,
    powerOn: false,
    command: "Sweeping the floor",
    duration: 10,
    favoriteMovies: ["2001: A Space Odyssey", "The Terminator", "I, Robot", "WALL-E", "Short Circuit", "Forbidden Planet"],
    
    makeBreakfast: function (newMood) {
        if(newMood) this.mood = newMood;
        this.command = "Making breakfast";
        this.doWork();
        if (this.mood === "Happy") {
            output.innerHTML += "Here are your waffles and milk, master." + BR;
        } else {
            output.innerHTML += "Here is your burnt toast and lukewarm water,    master." + BR;
            this.energyLevel = 50;
        }
    },
    doWork: function () {
        if(!this.powerOn) {
            this.powerOn = true;
            output.innerHTML += "My current task is: " + this.command + ". Duration: " + this.duration + " minutes." + BR;
        }
    },
    quitWork: function () {
        if (this.powerOn) {
            this.energyLevel = 90;
            this.powerOn = false;
            this.command = "Taking a nap";
            output.innerHTML += "My current task is: " + this.command + ". Duration: " + this.duration + " minutes." + BR;
        }
    }
};

// Make robot do housework
robot.doWork();
robot.quitWork();
robot.makeBreakfast();
robot.quitWork();
robot.makeBreakfast("Mad");
robot.quitWork();
<body>
    <p id="message"></p>
    <p id="movies"></p>
    <p id="object"></p>
</body>