How can I create a JavaScript loop that continuously prompts the user for input until they type "exit"?

Can you help with a JavaScript challenge? I am looking to create a loop that continuously asks the user for input until they type "exit". Once they do, I want the program to display the minimum value that was entered.


var students = prompt("Enter Student Name: ");
var studentArr = new Array(students);

var mark = parseInt( prompt("Enter Student Mark: ") );
var markArr = new Array();

function min (mark){
    var min = Number.MAX_VALUE;
    for(var i = 0; i < mark.length; i++){
       if(parseInt(mark[i]) < min)
           min = parseInt(mark[i]); 
    }
    return mark;
}
var smallest = min(mark);

document.write(smallest);

Answer №1

Give this a shot:

let input = prompt("Please enter a number");
let min = parseInt(input);

while (input !== "exit") {
    input = parseInt(input);
    if (input < min) {
        min = input;
    }
    input = prompt("Please enter another number");
}
alert(min);

See it in action here: http://jsfiddle.net/3Jtxu/

This is the basic code for finding the minimum number. You may consider adding some additional checks to ensure the input is a valid number.

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 callback for the array change listener is not receiving any arguments

Recently, I've been pondering a way to manage change listeners for arrays without relying on Observables or external libraries. My approach involves using a .json file containing an array of objects (specifically of type Rent) as my database. The goa ...

How to distinguish if a background tab is open using JavaScript or jQuery

Is there a way to determine if a new tab with target set to "_blank" was opened from the current page using JavaScript or jQuery? I want to increment a counter in the original window every time a background tab is opened. For example, if a link is clicked ...

Guidelines for ensuring that a radio button must be chosen prior to submission

I'm struggling with implementing AngularJS validation for a set of questions with radio answer choices. I want to ensure that the user selects an answer before moving on to the next question by clicking "Next". Check out my code below: EDIT: Please k ...

The JavaScript value of the button text once it has been modified

We have a website feature where the button text changes dynamically. Here are the elements before the button text change: <button _ngcontent-wex-c70="" class="btn btn-wait buy font-family-title height-70 pp-sun-ins"><labe ...

Getting the most out of your single react-select with Yup schema

I'm having issues with the validation of my form using react-select, Formik, and Yup. The schema I'm using is as follows: const Schema = Yup.object().shape({ age: Yup.object().shape({ label: Yup.string().required("Required"), value: Yup. ...

Using either AngularJS's simple .then method or the $q service to handle asynchronous requests

I'm trying to understand the distinction between AngularJS $q service and simply using .then() after an asynchronous request. For example, using .then(): function InboxService($http) { this.getEmails = function getEmails() { return $http.get(& ...

Using the same conditional statement on multiple pages within a Next.js application

I'm currently working on a project with Next.js and I've encountered an issue when fetching data from my server using the useSWR hook. There are certain conditions that need to be met in order to display specific content on the page, as shown in ...

Angular custom filter applied only when a button is clicked

I have recently started exploring angular custom filters and I am curious to know if there is a way to trigger the filters in an ng-repeat only upon clicking a button. Check out this example on jsfiddle : http://jsfiddle.net/toddmotto/53Xuk/ <div ...

The data retrieved from the backend is being stored in an object, however, it is not directly accessible through

After receiving data from the backend and storing it in an object, I am facing an issue where trying to print it using object properties shows undefined values. Here is a snapshot of my output: view image here Snippet of my code: this.documentService.getD ...

Executing a javascript file inside a jade document

I need help incorporating a JavaScript file into my Jade file so that it runs every time the page is accessed. How can I achieve this? Here is my current code snippet: doctype html html head title= title body h2 Bus Driver Locati ...

Arrange the coordinates (x,y) of a parabola represented by the equation y=ax^2+bx+c with x values x1, x2, x3, and x4 in ascending

I attempted using a cpp codeblock: bool compare(const pair<int,int>&A, const pair<int,int>&B) { if(A.second<=B.second) { if(A.first>=B.first) return 1; else return 0; } retu ...

A guide on implementing the href attribute in Material-ui

I previously asked about this code, but my wording was unclear. I am trying to get the menu items to work with href links. Currently, only the "Home" button works with href, while the "Sessions Home", "Book a Session", and "[S] Host a session" buttons do n ...

Issue with custom directive - bi-directional binding not functioning as expected

I'm currently working on creating a custom directive in Angular that transforms a tag into a toggle button, similar to a checkbox. As of now, the code I've developed updates the internal variable within isolated scope, but bidirectional binding ...

Activate on click using JavaScript

When a link with the class .active is clicked, I want to use a JavaScript function to deactivate any other active links. The structure of the code should be as follows: <ul class="product"> <li><a href="#myanmar" class="active">Mya ...

Manipulate the CSS of Quasar components within a child component in VueJS

Currently, I am incorporating Quasar components into my VueJS application in order to create an app. Within this setup, I have a child component that is being imported into a parent component. To customize the child component's appearance, I have over ...

Enhancing the background of a website with the power of CSS

I am looking to create a customized table without the balls/pots in it. The number of rows on the y-axis will vary depending on the number of names I have, and can be more or less. The values on the x-axis are determined by a parameter included in the URL ...

The functionality of scrolling in Windows is not functioning properly in Selenium WebDriver

I am facing an issue where I need to click on an element that is being intercepted by another element. My workaround involves scrolling down the vertical scrollbar in order to reach my target element. To achieve this, I am using the following code snippet: ...

Error thrown: Upon attempting to reopen the modalbox after closing it, an uncaught TypeError is encountered, indicating that the function $(...).load

An unexpected error occurred: $(...).load(...).modal is not functioning properly After closing a modal, I encountered this error in the console when attempting to reopen it. Strangely, it seems to work intermittently for a few times before throwing this e ...

Asynchronous database calls within a loop that do not wait

I'm facing a challenge while trying to access a database during JSON parsing. This particular piece of code has become a nightmare for me: function loopColumns(val, cb){ db.one('select tag_name from t_tag where tag_id = $1', val) ...

The component fails to display on the page when using Vue-router

I'm having an issue with my router. I'm trying to display bodyComponent on my index page, but nothing is happening. I'm using laravel+vue. Can anyone point out where I might be going wrong? Here's my router: import Vue from 'vue& ...