Make sure that the If statement is set up to notify the user of the number they have entered

I am in the process of creating a basic JavaScript program that compares two numbers provided by the user. The expected behavior is to display "x is larger" if the first number is greater than the second, and "y is larger" if the second number is greater. However, I've run into an issue where instead of displaying the actual number entered by the user, it shows "firstNumber is larger." How can I modify my If statement so that the program accurately reflects the input?

If both numbers are equal, the program correctly outputs "These numbers are equal."

Thank you!

JSfiddle Link: http://jsfiddle.net/9m4ohdkj/2/

<!DOCTYPE html>
<html>
<head> 
  <meta charset = "utf-8">
  <title>Compare Numbers</title>
  <script>

     var firstNumber; // variable to store first integer entered by the user
     var secondNumber; // holds second integer entered by the user


     // take first number as input from user
     firstNumber = window.prompt( "Enter an integer" );

     // take second number as input from user
     secondNumber = window.prompt("Enter second integer");

     // converting strings to integers
     firstNumber = parseInt(firstNumber);
     secondNumber = parseInt(secondNumber);

     //check conditions  
     if ( firstNumber > secondNumber )
        window.alert("firstNumber is larger");

     if ( firstNumber < secondNumber )
        window.alert("secondNumber is larger"); 

     if ( firstNumber == secondNumber )
        document.write("These numbers are equal!")


     
  </script>
  </head><body></body>
</html>

Answer №1

To modify your alert message, remove the variable name from the string displayed. See the example below:

 window.alert(firstNumber + " is greater");

Do the same for the second variable. Currently, when you include the variable name within double quotes, it is treated as a literal string (similar to 'is greater'), resulting in the alert message displaying 'firstNumber is greater' exactly as written!

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

Using .on as a substitute for live will not yield the desired results

I've been aware for some time now that the .on method is meant to replace .live, but I just can't seem to get it working. I've attempted: $(this).on('click', function(){ // Do something... }) $(this).on({ click: function ...

Loop through multiple arrays in a Jade template

For my node.js app, I am working with Jade. Is there a way to combine the data from two separate arrays and print them in a single foreach loop? for log of logs.logs && date of logs.date p #{log} cite #{date} ...

Tips for utilizing a .node file efficiently

While attempting to install node_mouse, I noticed that in my node modules folder there was a .node file extension instead of the usual .js file. How can I execute node_mouse with this file format? After some research, it seems like node_mouse may be an a ...

Deleting an List Item from an Unordered List using Angular

Within my MVC controller, I have an unordered list with a button on each item that allows the user to remove it from the list. The issue I'm facing is that while the row is deleted from the database, it still remains visible on the screen. This is ho ...

When a div in jQuery is clicked, it binds an effect to a textbox that is

HTML: <div class="infoBox2 blackBoxHover"> <h2>Link: </h2> <input class="fileInput" type="text" id="linkText" name="linkText" /> </div> JAVASCRIPT: $('#linkText').parent('div').click(function () ...

The Jquery onclick function is executing multiple times, with each iteration increasing in

I have encountered an interesting problem that I can't seem to resolve. The issue involves dataTables and the data that is retrieved via jQuery ajax post after a selection change on a select element. Furthermore, I have an onclick function for multipl ...

Unable to link JavaScript to HTML file through script tag

Upon testing out a responsive nav bar, I encountered an issue with the JavaScript not connecting. Despite placing the script tag at the bottom of the body as recommended, it still fails to function. index.html <html lang="en"> <head> ...

The state is not being configured accurately

In my ReactJs project, I have a model Component with a picture that is displayed. I want to pass the data of the clicked picture, which can be neither raw data nor a URL. I have implemented a handler that can delete the picture (if pressed with the Ctrl k ...

Automating the movement of a slider input gradually throughout a specified duration

I am working on a website that includes a range input setup like this: <input type="range" min="1036000000000" max="1510462800000" value="0" class="slider" id ="slider"/> Additionally, I have integrated some D3 code for visualizations. You can view ...

Identify and mark JavaScript variables that are not being utilized

Currently utilizing JSHint and JSCS for JavaScript code validation, but neither of them can identify the presence of unused variables like in this example: describe('XX', function () { var XXunused; beforeEach(inject(function ($injector) { ...

scrolling element resembling an iPad

Currently in search of a css/js component that resembles iscroll, but one that is compatible with non-webkit browsers as well. To elaborate, I am in need of the following features: Sleek scrollbar Scrollbar that only appears on drag and/or hover Abilit ...

Ldap.js: exploring nested searches

My current task involves using ldapjs to conduct a search where the filter is dependent on the outcome of a preceding search. ldapClient.search(base1, opts1, (err1, res1) => { res1.on("searchEntry", entry => { const myObj = { attr1: entr ...

Problem encountered when attempting to select all elements by their ID and then applying a CSS class to them

Hey there! I'm currently working on a project where I need to add a class to all elements on a webpage. The main objective is to assign a class containing font size changes to hide a specific message. Unfortunately, I've encountered an error tha ...

Bug with FullPage scrollOverflow sections when scrolling with iScroll's scrollTo() function

I've encountered an issue while using FullPage with scrollOverflow: true. I need to scroll to a specific position in a scrollable section. The problem arises from the fact that FullPage utilizes a modified version of the iScroll plugin for these overf ...

What are the steps involved in generating and implementing dynamic hierarchical JSON data structures?

I am currently creating a dynamic diagram using d3.js that incorporates hierarchical data. The goal is to make it interactive so that users can manipulate the hierarchy by adding or removing data values and children. I'm wondering if there is a way to ...

Observables and the categorization of response data

Understanding Observables can be a bit tricky for me at times, leading to some confusion. Let's say we are subscribing to getData in order to retrieve JSON data asynchronously: this.getData(id) .subscribe(res => { console.log(data.ite ...

Emitting events to multiple components in VueJS with different parent components

I have multiple Vue Components set up like this: App.js | |-- CreateTasks | |-- LatestTasks (displays 20 tasks) | |-- FooBar | |---LatestTasks (displays 10 tasks) My goal is to trigger an event from the CreateTasks component when a new task is c ...

What is the best way to combine and link a list of string elements using a separator in Powershell?

PS C:\Users\User\ps-modules> more .\MyStrings.Tests.ps1 function slist { "1", "2", "3" } Describe 'StringTests' { It 'literal -join' { "1", "2", "3&qu ...

Using Python 3 to extract specific strings from an HTTP request's response

I'm currently facing a challenge with parsing data from an http request response. Can anyone provide some assistance? Below is a snippet of my code: import requests r = requests.get('https://www.example.com', verify=True) keyword = r.text ...

Guide on repetitively invoking a function in jQuery

I am looking to create a continuous fade in and out effect on a picture with a delay using jQuery. I attempted the following code but it is not working as expected. $(document).ready( setTimeout( function () { $("#ssio").fadeToggle(1000); ...