JS object and function fixes

I have a homework assignment to fix the Javascript code that prevents functions from executing. I fixed everything I could find, even using JSFiddle and JSHint to be sure. So there are no syntax errors left, but there must be a logic error because nothing works. I'd love to have a quick look at my code, I'm sure this is a minor issue that I am simply ignoring. Thank!

(There must be some messy code here with how many things I've tried to change, apologize for that).

The simplest HTML first:

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

      

Then 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();

      

Full assignment: 1) Correct the code and make doWork and quitWork executable. 2) Call the makeBreakfast argument and use the "mad" line as an argument. 3) Add a new method and call it to display the line (leaving this part not useful right now) 4) List of all favorite robot movies, each on a new line. 5) Use named indexing syntax to add a new property called language to the robot object. Initialize it in the language of your choice. 6) Using named indexing syntax, walking through the object and listing each property and value pair on a new line. Display the results in a new paragraph.

I really only need C # 1 help, but if you see the full assignment, that's it. Thanks everyone!

+3


source to share


1 answer


You need to make it an object and tell the poor shmak that you have breakfast!

( Complete 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>
      

Run codeHide result


+1


source







All Articles