"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

Searching for real-time data with ajax. Strategies for showing alternative results when there is no user input

I am currently working on developing a live search feature using technologies like ajax, jQuery, PHP, and MySQL. The user inputs certain queries which are then sent to form_livesearch.php where the search is processed. I have successfully implemented this ...

Adjust the size of an element using the jQuery library's knob.js

There is a page Once the button is pressed, a circle element from the library jquery.knob.js appears. I am trying to change the size of the circle and have written this code: <div style="float:left; width:255px; height:155px"> <input ...

Creating dynamic and engaging animations for components using ReactCSSTransitionGroup

I'm currently attempting to add animation to a modal that appears when a button is clicked using ReactCSSTransitionGroup. The modal is showing up on button click, however, there is no transition effect. My render method is set up like this: render() ...

Develop a responsive shopping cart using PHP and JavaScript

I am in the process of developing an ePos system that allows users to add items to their basket by simply clicking on them, and then calculates the total price without needing to refresh the entire page. I initially attempted to use $_SESSION and store th ...

Tips on automatically adjusting the width of a div to fit the content, maintaining center alignment, and ensuring the contents float to the left

I am facing an issue with a div element that has multiple children. My goal is to have the children neatly fit to the bottom left of the grid when the window expands, utilizing the next available space efficiently. However, as the div expands with the wind ...

The functionality of CSSTransition seems to be malfunctioning. Perhaps switching to V2 of react

Can anyone help me troubleshoot my code snippet where I'm attempting to incorporate an animation for Tooltip Nodes? Despite my efforts, the animation does not show up on the screen after mounting. Additionally, the console.log is not triggered on the ...

What is the best way to reposition a column as a row when the user interface transitions to a different screen or

Welcome to my UI experience! https://i.stack.imgur.com/gOwAn.png Check out how the UI adapts when I resize the browser: https://i.stack.imgur.com/MyxpR.png I aim for the left component to be visible first, followed by scrolling to see the right compone ...

The issue of Next.js redux useSelector causing HTML inconsistency

Currently, I am utilizing Next.js for the development of a React application. In order to manage the state, I have integrated redux along with redux-toolkit. A peculiar error has surfaced in the console with the message: Warning: Did not expect server H ...

Substitute the ajax reply with the text currently displayed

I am facing an issue with the AJAX response in my HTML page. Currently, the response is being appended on top of the existing text instead of replacing it. Here is a snippet of my code: <html> <head> <h2>This is a test</h2> ...

Automatically initiate a click event when the page is loaded

I'm having trouble getting a click event to trigger on my controller when the page loads. I really just want the checkboxes to be clicked automatically. <!DOCTYPE html> <html > <head> <link rel="stylesheet" type="text/css" ...

The Minimax algorithm experiencing issues in Next.js

I recently wrote some code in Next.js, but unfortunately, it's not functioning as expected. Despite my best efforts and attempts at utilizing alpha beta pruning, the code still fails to work properly. The issue lies in the fact that it simply returns ...

The Angular 9 custom directive is malfunctioning on mobile devices

I recently created a custom directive in Angular 9 that allows users to input only digits and one decimal point. While the directive works perfectly on desktop, it seems not to function at all on mobile devices - almost as if it doesn't exist within t ...

Updating a particular column in a table with Jquery

In this table : $('#listview-table tr').each(function() { var status_id = $(this).find(".listViewEntryValue").$('[data-name~="cf_1525"]').text(); alert(status_id); }); <table id="listview-table" class="table listv ...

When using JSON.stringify, the output is null. However, when using document.write, the data

I am currently working on a plugin for crawljax that involves executing some JavaScript code, similar to the following: String result = browser.executeJavaScript(script).toString(); The script code is as follows: function getElementPosition(id) { var el ...

Attempting to dynamically add an image to a template snippet

Struggling with inserting images dynamically from database paths in templates. How can I display them when a user clicks the Show More button on an expense page? New to templates and unsure about syntax. Show More Button displayed in the HTML template sho ...

Unable to utilize console.log and alert functions within the Next.js application

I'm currently facing a problem in my Next.js application where the console.log and alert functions are not functioning as intended. Despite checking the code, browser settings, and environment thoroughly, pinpointing the root cause of the issue remain ...

Ways to conceal a parameter in Angularjs while functioning within the ng-bind directive

I am using Angular to create the final URL by inputting offer information. Below is the code snippet: <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> ...

Firestore is failing to accept incoming data when the setDoc method is employed

I am encountering an issue with my app's connectivity to the Firestore database when attempting to utilize setDoc. The database is successfully operational for next-auth, creating records as intended. However, the problem arises when trying to enable ...

What is the best method for retrieving the current value of an RxJS Subject or Observable?

I am dealing with an Angular 2 service: import {Storage} from './storage'; import {Injectable} from 'angular2/core'; import {Subject} from 'rxjs/Subject'; @Injectable() export class SessionStorage extends Storage { priv ...

Saving Information to a Specific Location on the Client Side in a CSV File

I am currently working on a static application that uses Angular JS technology. My goal is to write data into a CSV file at a specific path in order for another API to read the data from that location. Despite searching extensively on the internet, I hav ...