Else token error caught off guard

I am currently learning about if/else statements on Codecademy and keep encountering an error that says, "SyntaxError: Unexpected token else". Can someone please assist me? Below is the code I am working on:

// Confirm if the user is ready to play

confirm("I am ready to play!");

var age = prompt("What is your age?");

if (age < 13) {
    console.log("You are allowed to play, but I take no responsibility");
} else {
    confirm("Play On!");
}

console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");

console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");

var userAnswer = prompt("Do you want to race Bieber on stage?");

if (userAnswer === "yes") {
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
} else {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

Answer №1

change the variable userAnswer to "yes"

simply delete the semicolon at the end of the if statement

if(userAnswer==="yes")

This change is important because in JavaScript (and many other languages), having a semicolon after the condition in an if statement like below:

if (condition); 
    statement; 

will be treated as a no-operation and will be optimized out, which means that the actual code executed would look like this:

if (condition) <nothing!>; 
    statement; 

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

Steps to include a personalized function in a Mongoose Model

One way to extend Mongoose is by adding methods to documents. Here's an example: const userSchema = new mongoose.Schema({ balance: Number }) userSchema.methods.withdrawBalance = function(amount){ const doc = this doc.balance = doc.balance - amou ...

Managing promises individually within a promise.all() operation

While there is plenty of information available on handling errors with promise.all() and using catch, my goal is to handle each promise resolution within promise.all(). This is because I am working on setting up a customized progress bar in the console, an ...

Adjust the size of the textarea to accommodate the number of lines of text

<script type="text/javascript"> function AutoGrowTextArea(textField) { if (textField.clientHeight < textField.scrollHeight) { textField.style.height = textField.scrollHeight + "px"; if (textField.clientHeight ...

Creating an AJAX request in Play 2.x by using a shared button

Although I have successfully utilized AJAX requests in the past using a form and adding return false to prevent page refresh, I recently encountered an issue when moving my JavaScript into a separate file. The @ commands now fail, making it difficult for m ...

Error encountered when attempting to establish a connection between socket.io and express: network error

Similar Question: socket.io: Failed to load resource I'm having trouble getting a simple express + socket.io hello world app to work. I keep receiving the following error: "NetworkError: 404 Not Found - http:// localhost:3002/socket.io/socke ...

Error: Router service provider not found in Angular 2 RC5!

Having trouble with using this.router.navigate. Here is the content of my app.module.ts file: import {NgModule, NgModuleMetadataType} from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; im ...

What is the CSS method for altering the color of a slider's runnable track upon selection?

Seeking assistance in changing the slider track color upon selection. Struggling to achieve the desired outcome of altering the color as it slides. CSS: /* Custom Styles */ .text-size-slider { line-height: 100%; font-size: 14px; position: relative ...

Tips for integrating NanoHTTPD with AJAX requests

I am currently working on setting up NanoHTTPD on an android device to respond to AJAX requests in a way that the requesting javascript can understand the response. Here is my implementation of the NanoHTTPD serve method: public NanoHTTPD.Response serve( ...

The amalgamation of geometries using BufferGeometryUtils results in variations from the original model

I have encountered an issue when attempting to merge GLB model geometries with three.js using BufferGeometryUtils.mergeBufferGeometries. The new merged geometries do not always align perfectly with the original model. Furthermore, some of the geometries e ...

Executing both Java and JavaScript code in the same form using JSP

Currently facing an issue where my form includes inputs for username and password, a captcha (<div>), and a submit button. Upon clicking the submit button, I want it to first check if the captcha is empty. If not, then proceed to call the Java code t ...

Looking for mouseover tooltips in three.js?

Currently, I am working on loading an object similar to this demo. My goal is to display a tooltip or infobox when hovering over a specific part of the object. For example, upon clicking the top button, I would like a tooltip to appear above the box. I hav ...

Can React components be saved in an array?

I am currently working on enhancing the code snippet provided below. This code is intended to iterate through elements of an array using keys to locate images within a lengthy SVG file directly embedded in the document under the identifier "SomelongUglySVG ...

What is the best way to pre-fetch data using axios in Vue?

My app requires offline functionality for drivers operating in remote areas with limited internet access. To address this, I aim to pre-download all necessary data using Axios requests when an internet connection is available. This way, the app can retriev ...

What could be the reason for my function failing to return true?

I have a function that can check if a script is loaded: function checkURLExistence(url){ $.ajax({ url: url, success: function(data){ //alert(url + ' exists'); console.log(url + ' exists'); return ...

What are the best practices for securely using JSON.stringify in JavaScript?

When I use JSON.stringify on a string that includes <script> tags, the script tags somehow escape and show up in the body of my document, causing unexpected results with the "injected" data. I'm puzzled by how they manage to escape, considering ...

What is the procedure for configuring custom request headers in Video.js?

Encountering this issue, I created a simple example using guidance from this documentation: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link href="https://vjs.zencdn.net/7.4.1/video-js.css" re ...

Exploring the process of using querySelector() to locate specific class and div IDs in JavaScript code

My objective is to make the id="myDropdownTop" trigger the corresponding drop-down menu. The problem lies in my script code, particularly the final line: ("div.dropdown-content").classList.toggle("show"); does not seem to be functioning correctly. <!D ...

Incorporating a scroll feature using Ionic React

Currently, I am attempting to include a button in my Ionic React project that will smoothly scroll to the top of the page. Below is a snippet of the code I have written thus far: ... function scrollToTop() { return document.getElementById("page")!.scr ...

Limiting the number of files that can be uploaded using ng-file-upload in Angular.js is a simple process

Can someone help me with limiting file selection using ng-file-upload in Angular.js? Here is my code: <input type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage" ng-model="file" ngf-pattern="' ...

Guide on toggling all el-collapse-items from the ElementPlus Vue3 Library at once using a single button

Is there a way to expand and collapse all el-collapse-items from the ElementPlus Vue3 Library using just one button in this code snippet? <template> <div class="demo-collapse"> <el-collapse v-model="activeName" accordion& ...