skip to Main Content

The question:

Define a setter function named wonAward() that takes a parameter for the name of an award and pushes the award name onto the end of the awards array.

My code:

    let movie = { 
       title: "Interstellar",
       director: "Christopher Nolan",
       composer: "Hans Zimmer",
       budget: 165000000,
       boxOffice: 675100000,
       awards: [],   
   };

   /* Your solution goes here */
   set wonAward(nameAward) {
       this.awards.push(nameAward);
      }
   };

The Error:

Error: (line 14) Uncaught SyntaxError: Unexpected identifier
‘wonAward’. The code editor above may provide more details for this
error

.

Note: Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often don’t recognize the problem until reaching a later line.

I have been working on this problem for 3 hours and it is a small question in a huge homework assignment. I am changing my major.

To solve this problem, I already laid it all out earlier.

2

Answers


  1. Your wonAward setter is defined outside the movie object, and the last couple of chars ( };) of your code shouldn’t be there:

    let movie = { 
        title: "Interstellar",
        director: "Christopher Nolan",
        composer: "Hans Zimmer",
        budget: 165000000,
        boxOffice: 675100000,
        awards: [],   
        set wonAward(nameAward) {
            this.awards.push(nameAward);
        }
    };
    

    You can then use your setter like so: movie.wonAward = "Razzie";.

    Login or Signup to reply.
  2. It seems like you’re trying to define the function wonAward as a setter function inside the movie object. To achieve this, you need to use the Object.defineProperty method or use ES6 shorthand for defining setter functions. Here is the corrected version of your code:

    let movie = {
        title: "Interstellar",
        director: "Christopher Nolan",
        composer: "Hans Zimmer",
        budget: 165000000,
        boxOffice: 675100000,
        awards: [],
        set wonAward(nameAward) {
          this.awards.push(nameAward);
        },
      };
      // Add some awards
      movie.wonAward = "Best Visual Effects";
      movie.wonAward = "Best Sound Editing";
    
      // Checking the awards array
      console.log(movie.awards);
    

    In this code snippet, I’ve used Object.defineProperty to define the wonAward property as a setter on the movie object. You can then use this setter function to push the name of the award onto the awards array. Check if this works for you and if you still have any errors share them here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search