"if the condition is not met, the outcome will not be shown in the while

I have a looping structure that includes conditions for if, else if, and else. However, I have noticed that the else condition at the end of the loop is not being executed as expected. I am currently investigating why this might be happening. Here is the code snippet in question:

var loopEnd = 1;
while (loopEnd != 2) {
  var conversion = prompt("Do you want to convert miles to km, or km to miles?");
  if (conversion == "miles to km") {

    var miles = 0;
    var miles = prompt("How many miles do you want to convert?");
    var kilometers = 1.61 * miles;
    while (miles != 0) {
        alert("The number of kilometers for " + miles + " miles is " + kilometers + " kilometers.");
        var miles = prompt("How many miles do you want to convert?");
        var kilometers = 1.61 * miles;
                   }
    var promptEnd = prompt("Do you want to convert anything else?");
    if (promptEnd == "no") {
        var loopEnd = 2;
        } else {
        alert("Okay, let's convert something else!");
        }

    } else if (conversion == "km to miles") {

    var kilometers = 0;
    var kilometers = prompt("How many kilometers do you want to convert?");
    var miles = 0.62 * kilometers;
    while (kilometers != 0) {
      alert("The number of miles for " + kilometers + " kilometers " + miles + " miles.");
      var kilometers = prompt("How many kilometers do you want to convert?");
      var miles = 1.61 * kilometers;
    }
    var promptEnd = prompt("Do you want to convert anything else?");
    if (promptEnd == "no") {
      var loopEnd = 2;

 // } else {

    alert("Sorry, I didn't quite catch that.");
    }
  }
}

Answer №1

If you are referring to the alert section within the 'else' statement, then your code needs reformatting. Here is a revised version:

var loopEnd = 1;
while (loopEnd != 2) {
    var conversion = prompt("Do you want to convert miles to km, or km to miles?");
    if (conversion == "miles to km") {

        var miles = 0;
        miles = prompt("How many miles do you want to convert?");
        var kilometers = 1.61 * miles;
        
        while (miles != 0) {
            alert("The number of kilometers for " + miles + " miles is " + kilometers + " kilometers.");
            miles = prompt("How many miles do you want to convert?");
            kilometers = 1.61 * miles;
        }
        
        var promptEnd = prompt("Do you want to convert anything else?");
        
        if (promptEnd == "no") {
            loopEnd = 2;
        } else {
            alert("Okay, let's convert something else!");
        }

    } else if (conversion == "km to miles") {

        var kilometers = 0;
        kilometers = prompt("How many kilometers do you want to convert?");
        var miles = 0.62 * kilometers;
        
        while (kilometers != 0) {
            alert("The number of miles for " + kilometers + " kilometers is " + miles + " miles.");
            kilometers = prompt("How many kilometers do you want to convert?");
            miles = 1.61 * kilometers;
        }
        
        var promptEnd = prompt("Do you want to convert anything else?");
        
        if (promptEnd == "no") {
            loopEnd = 2;
        }
    
    } else {
        alert("Sorry, I didn't quite catch that.");
    }
}

Rhumborl also mentioned the importance of cleaning up your code and giving it proper spacing.

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

Utilizing Electron's <webview> feature to display push notification count in the title

I'm currently working on a Windows app using Electron's webViewTag to integrate WhatsApp Web. While I have successfully implemented desktop toast notifications, I am curious if it is possible to display a notification count in the title bar of my ...

Controlled Material-UI v5 DateTimePicker triggers input focus upon closure

Is there a way to have 2 DateTimePicker components as siblings, and when I click on the second one while the first one is still open, it should open a new DateTimePicker with focus on it? Can someone help me achieve this? Link to code example I want the ...

The selector-triggered JQuery event being invoked by my script

Facing an issue while trying to invoke my jQuery event. I am working with 2 jQuery events: $element.on('click', function (e) { ... }); $element.on('click', '.toggle', function (e) { ... }); My question is how can I trigge ...

Vue.js is displaying one less item

Recently I started working with Vuejs and encountered an unexpected issue in my application. The purpose of my app is to search for channels using the YouTube API and then display those channels in a list. However, when I try to render the list of subscri ...

Utilizing DataTables and Ajax call to refresh table data with Json response

My table is dynamically generated using Thymeleaf and I want to update its contents with jQuery. <table class="table table-hover" id="main-table"> <thead class="thead-inverse"> <tr> <th class="c ...

Is there a way for senders to also view their own messages using socket.io?

Using socket.io, I am trying to send a message to a specific user based on their socket.id, and also ensure that the sender can see their own message. The code snippet I am using for this is: socket.to(msg.id).emit('chat message', msg.message);, ...

Error encountered while attempting to send SendGrid email to multiple recipients

Currently, I am using const sgMail = require('@sendgrid/mail'); with sendgrid version 7.6.2. Whenever I attempt to add two email addresses in an array and then pass it into either send() or sendMultiple(), an error is being thrown like below. st ...

Cancel an AJAX request without interfering with the subsequent request

When working with a number input and the "onChange" event, I have come across an issue with my ajax request. Upon aborting the request, it seems to have a negative impact on subsequent requests. If I send just one request without aborting, everything runs ...

I am looking to remove identical innerText values from two arrays using JavaScript

Is it possible to use JavaScript to remove the "added" className of an element with the same innerText when the delete button (.btn-wrap>li>button) is clicked? <div class="table"> <ul> <li class="added">l ...

Utilize Bootstrap4 to position content above the navigation icon

I need assistance to modify my code so that it will have the following appearance: I successfully created a navigation bar icon and inserted the logo, but I am struggling to position the data above the navigation icon: My current code: <nav class="n ...

Click event from Angular Material menu is triggered twice on mobile devices

After implementing a basic angular material side menu from the demo, I noticed that all click events are being fired twice on the entire page when viewed in mobile browsers. This issue can even be replicated in the Chrome emulator. (To reproduce, enable th ...

What steps can be taken to update the cart if all products have been removed?

How can I implement a refresh for my cart page every time the product length is 0 without causing unreachable code? It seems like there must be a simple solution to this problem. switch (action.type){ case "REMOVE_PRODUCT": return ...

The script from '*' is being denied execution because its MIME type ('application/json') is not executable, and a strict MIME type check is in place

Here is the code I used to retrieve data from the confluence rest api: <script type="text/javascript" src="Scripts/jquery.min.js"></script> <script> $.ajax({ type: "GET", url: "https://blog.xxxxx.com/rest/api/content? ...

Is there a way to optimize Typescript compiler to avoid checking full classes and improve performance?

After experiencing slow Typescript compilation times, I decided to utilize generateTrace from https://github.com/microsoft/TypeScript/pull/40063 The trace revealed that a significant amount of time was spent comparing intricate classes with their subclass ...

error encountered in ajax contact form due to issue with select element

I'm experiencing an issue with my ajax contact form, where the input field and select element are not providing the expected results. This is the HTML Form Code (with "name" as the input and "iphone" as the select element) <div id="contact_form" ...

Attempting to incorporate Font-Awesome Icons into the navigation bar tabs

As a newcomer to React, I've been attempting to incorporate Font Awesome icons into my secondary navigation bar. Despite using switch-case statements to iterate through each element, all the icons ended up looking the same, indicating that only the de ...

Using express.js to transfer an uploaded image to Amazon S3

Currently, I am faced with an issue of passing an image uploaded from a react application through express to a managed s3 bucket. The s3 bucket is created and managed by the platform/host I am using, which also generates upload and access urls for me. So f ...

In a carousel slider, the height and width of divs are not set to specific dimensions

For a code snippet, you can visit this link: here The html: <html lang="en"> <head> <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700,700i|Merriweather:300,400,700" rel="stylesheet"> <link href="https://ma ...

Enhancing the visual display of a webpage using AngularJS

Hello there! I'm currently working on enhancing an angular 1.6 app and have encountered a dilemma that needs solving. Let me provide some context: The page in question is a lengthy form consisting of thirty questions. The client-side logic includes nu ...

Some suggestions for updating two div elements using only one Ajax response

My website features a signin() button that I want to enhance with ajax functionality. When the button is clicked, I need it to update two divs: one displaying a personalized welcome message, and the other showcasing a statistics table in a different locati ...