JavaScript Objects: Where am I going astray?

I encountered an issue while working on a coding exercise. Whenever I attempt to submit my code, I receive the following error message:

SyntaxError: Unexpected string

var movieObj = {
"Toy Story 2": "Great story. Mean prospector.",
"Finding Nemo": "Cool animation, and funny turtles."
"The Lion King": "Great songs."
};

var getReview = function (movie) {
    if (movie in movieObj) {
        return movieObj[movie]
    } else {
        return "I don't know!"
    }
};

getReview("Toy Story 2") //expected = "Great story. Mean prospector."
getReview("Toy Story") //expected = " don't know!"

Can you help me figure out what's going wrong?

Answer №1

The missing comma for the second item in the movieObj object is causing the issue. Update the second line to include the necessary comma:

"Finding Nemo": "Cool animation, and funny turtles.", 
/* Don't forget the comma */
This change should resolve the problem.

Answer №2

You overlooked adding a comma following "... turtles." Remember, object properties are typically separated by commas.

Answer №3

Your movieObj was missing a comma for the movie "Finding Nemo". Additionally, there were some semi-colons ; missing.

var movieObj = {
    "Toy Story 2": "Great story. Mean prospector.",
    "Finding Nemo": "Cool animation, and funny turtles.",
    "The Lion King": "Great songs."
};

var getReview = function (movie) {
    if (movie in movieObj) {
        alert(movieObj[movie]);
    } else {
        alert("I don't know!");
    }
};

getReview("Toy Story 2"); //expected outcome: "Great story. Mean prospector."
getReview("Toy Story"); //expected outcome: "I don't know!"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The looping function in JavaScript iterates 20 times successfully before unexpectedly producing NaN

run = 0 function retrieveItemPrice(id){ $.get('/items/' + id + '/privatesaleslist', function(data){ var htmlData = $(data); var lowestPrice = parseInt($('.currency-robux', htmlData).eq(0).text().replace(',& ...

Issue with event handling in react-leaflet polygon component

Curious question, my experience involves dynamically rendering Polygons with the help of react-leaflet. The polygons are displaying as expected. However, whatever configurations I apply to eventHandlers seem to have no effect. const highlightFeature = ( ...

Revamping the website to become a Progressive Web App

I am in the process of transforming my website into a Progressive Web App (PWA) and have made some updates to my code to facilitate this: index.html <script> if('serviceWorker' in navigator) { navigator.serviceWorker.registe ...

Steps to open specifically the WhatsApp application upon clicking a hyperlink, image, or button

I need a code for my HTML website that will open the WhatsApp application when a user clicks on a link, image, or button while viewing the site on a mobile device. Only the WhatsApp application should be opened when a user interacts with a link on my webs ...

The order of execution is not maintained for $.getJSON() calls within the $.each() loop

As I iterate through an HTML table, I am making a $.getJSON() request based on the data in each row. My goal is to retrieve the data from that $.getJSON call and update the corresponding row with it. Unfortunately, when I run my code, it seems to be execu ...

Tips for modifying fullcalendar's renderEvents handler to include custom code

Currently, I am working with fullcalendar 1.6.3 in conjunction with Drupal 7 (which is why I have to stick with version 1.6.3 for now). Every time the calendar view changes through ajax requests - whether moving forward or backward in time or switching bet ...

Item template with dynamic content for pagination directive

My current implementation includes a paginator directive that displays items from an array: .directive('paginator', function() { restrict: 'A', template: '<div ng-repeat="item in items">' ...

Alignment problem with the text on the left

Utilizing remodal.js from the repository found at https://github.com/VodkaBears/Remodal to develop a modal interface: http://jsfiddle.net/j4wnov5z/. In essence, I am trying to align certain elements to the left, but encountering difficulties. For instanc ...

Implementing mouse hover functionality for fieldset in EXTJS

I am looking to enhance the following code snippet by adding a mouse hover event that changes the background color of the fieldset item and displays an image next to it. Can someone assist me with this modification? var mainGroup = { ...

The script fails to execute upon the loading of the view

I am facing an issue with a simple JavaScript code that aims to target an element within an angular view: var buttons = document.querySelectorAll('.msg-type i'); console.log(buttons.length); When I run this code, it incorrectly prints out 0 on ...

Develop unique web components and distribute them across various frameworks

Currently, I am working on two projects that utilize Angular and React. I have noticed that certain components are duplicated in both projects. To streamline this process, I am considering creating a company library where I can develop custom components on ...

Experimenting with TypeScript code using namespaces through jest (ts-jest) testing framework

Whenever I attempt to test TypeScript code: namespace MainNamespace { export class MainClass { public sum(a: number, b: number) : number { return a + b; } } } The test scenario is as follows: describe("main test", () ...

choose the checkbox in the first column using jQuery

I need assistance with selecting only the checkboxes in the first column of a simple table. Currently, my code is selecting all checkboxes in the table instead. How can I modify it to achieve my desired outcome? Html <script src="https://ajax.googleap ...

How can you activate or deactivate Bootstrap Checkboxes using buttons in Angular 9?

I've been working on developing a page in Angular, although I'm still getting the hang of it. Despite spending several hours and going through numerous threads trying to find a solution, I haven't been able to address my specific issue. Jus ...

Troubleshooting the Checkbox Oncheck Functionality

Before checking out the following code snippet, I have a requirement. Whenever a specific checkbox (identified by id cfc1) is clicked, it should not show as checked. I have implemented the onCheck function for this purpose, but I'm struggling to fig ...

What is the best way to delete markers from a leaflet map?

I need to remove markers from my map. I am looking to create a function that will specifically clear a marker based on its ID. I am utilizing Leaflet for the map implementation. Here is my function: public clearMarkers(): void { for (var id in this. ...

Strategies for effectively handling errors in the requestAnimationFrame function

I'm currently facing issues with the animate() function, as it tends to crash my browser and cause my computer to heat up when errors occur. I attempted to use a try/catch handler to handle these errors but it did not work as expected. animate(){ ...

Is there a way to circumvent the eg header along with its contents and HTML code using JavaScript?

I have a script in JavaScript that is designed to replace the content of data-src attribute within each img tag with src. However, I am facing an issue where this script interferes with the functionality of a slider located in the header section. The sli ...

Evaluating TypeError in CoffeeScript using Jasmine with Backbone.js

Currently, I am following the PeepCode video tutorial on Backbone.js, but I am rewriting all the code in CoffeeScript instead of plain JavaScript. Everything is going well so far, except when I attempt to run Jasmine tests on the code, I encounter some Ty ...

The jQuery Code in My Updated HTML Is Not Running Properly

Currently, I am working on a dynamic insert form for my website. In order to achieve this, I am creating a button to append a new textbox that is supposed to be using the Select2 plugin. However, when I use the jQuery append() function, the appended code d ...