🌐
TutorialsTeacher
tutorialsteacher.com β€Ί javascript β€Ί display-popup-message-in-javascript
JavaScript Message Boxes: alert(), confirm(), prompt()
alert("This is an alert message box."); // display string message alert('This is a numer: ' + 100); // display result of a concatenation alert(100); // display number alert(Date()); // display current date
🌐
W3Schools
w3schools.com β€Ί jsref β€Ί met_win_prompt.asp
Window prompt() Method
addeventlistener() alert() atob() blur() btoa() clearInterval() clearTimeout() close() closed confirm() console defaultStatus document focus() frameElement frames history getComputedStyle() innerHeight innerWidth length localStorage location matchMedia() moveBy() moveTo() name navigator open() opener outerHeight outerWidth pageXOffset pageYOffset parent print() prompt() removeEventlistener() resizeBy() resizeTo() screen screenLeft screenTop screenX screenY scrollBy() scrollTo() scrollX scrollY sessionStorage self setInterval() setTimeout() status stop() top Window Console
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί API β€Ί Window β€Ί prompt
Window: prompt() method - Web APIs | MDN
August 19, 2025 - This feature is well established ... July 2015. ... window.prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog....
🌐
Webdevelopersnotes
webdevelopersnotes.com β€Ί the-javascript-prompt-getting-user-input
The JavaScript prompt – Getting user input
Now let’s write a small script ... box. var n = prompt("Check your number", "Type your number here"); n = parseInt(n); if (n == 0) { alert("The number is zero"); } else if (n%2) { alert("The number is odd"); } else { alert("The number is even"); } Click here ...
🌐
SheCodes
shecodes.io β€Ί athena β€Ί 36203-how-to-use-prompt-and-alert-in-javascript
[JavaScript] - How to Use Prompt and Alert in JavaScript - | SheCodes
Learn how to use the built-in JavaScript functions 'prompt' and 'alert' to display dialog boxes and message boxes.
🌐
Medium
medium.com β€Ί @meghamurari12 β€Ί lesson-16-user-input-with-prompt-in-javascript-df46a299ecab
πŸ“˜ Lesson 16: User Input with prompt() in JavaScript | by Meghamurari | Medium
August 9, 2025 - let num1 = prompt("Enter the first number:"); let num2 = prompt("Enter the second number:"); let sum = Number(num1) + Number(num2); alert(`The sum of ${num1} and ${num2} is ${sum}.`); ... Ask the user for their age and display a message based ...
🌐
W3Resource
w3resource.com β€Ί javascript-exercises β€Ί javascript-basic-exercise-8.php
JavaScript basic: Display a message when a number is between a range - w3resource
June 30, 2025 - // Generate a random integer between 1 and 10 (inclusive) var num = Math.ceil(Math.random() * 10); // Log the generated random number to the console console.log(num); // Prompt the user to guess a number between 1 and 10 (inclusive) var gnum = prompt('Guess the number between 1 and 10 inclusive'); // Check if the guessed number matches the generated random number if (gnum == num) // Log a message if the guessed number matches the random number console.log('Matched'); else // Log a message if the guessed number does not match, and also provide the correct number console.log('Not matched, the number was ' + gnum);
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_popup.asp
JavaScript Popup Boxes
let person = prompt("Please enter your name", "Harry Potter"); let text; if (person == null || person == "") { text = "User cancelled the prompt."; } else { text = "Hello " + person + "! How are you today?"; }Try it Yourself Β» Β· To display line breaks inside a popup box, use a back-slash followed by the character n. alert("Hello\nHow are you?"); Try it Yourself Β» Β·
🌐
IncludeHelp
includehelp.com β€Ί code-snippets β€Ί input-value-from-the-user-using-prompt.aspx
JavaScript | Input value from the user using prompt
... <!DOCTYPE html> <HTML> <HEAD> ... numbers i.e., integer values in JavaScript using the prompt() method, you need to parse the input value because the prompt() method returns the string....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί javascript-program-to-print-an-integer-entered-by-user
JavaScript Program to Print an Integer Entered by user - GeeksforGeeks
February 22, 2024 - <!DOCTYPE html> <html> <body> <script> let userInteger = parseInt(prompt("Enter an integer:")); alert(`You entered: ${userInteger}`); </script> </body> </html> ... In this example, prompt() displays a dialog box to the user to enter an integer, ...
Top answer
1 of 3
1

You are overriding your "total" variable in each interval with double the grade value.

var grade=parseInt(g);
 var total=grade+grade;

should be changed to

var grade=parseInt(g);
 total=total+grade;

Also, you need to initialize the "total" variable in the beginning of your code. See demo code: http://jsfiddle.net/56ouvan3/1/

I would also recommend some input validation (such as checking that the number of grades requested to average are greater than 0, all grades are positive, etc.)

2 of 3
0

I think you wanted to accomplish something like that:

http://jsfiddle.net/3L8dL228/1/

Just replace the console.log with your own document.write.

Now, despite I totally hate using prompts and I'm not very used to them, here are you what I think you're missing in your script:

  1. CONTROL: your "n" and "g" variables HAS to be an integers, so force the user to insert an integer.
  2. Variables declaration: you're declaring total every single time you loop, therefore you're not storing anything at all.

To fix these, the very first piece of your code becomes this:

var n = prompt("Input a number: ", "Number here");
while (!parseInt(n) )
{
 n=prompt("Input a number: ", "Number here");   
}

In this way, you're asking the user to give you a NUMBER, but the script won't procede until it will effectively be able to parse an integer value.

Therefore, inputs like "hey", "hello", "foo", "bar", "baz" won't be accepted.

The second part of your code then becomes this one:

var i=1;
    var total = 0;
    do
    {
        var g = prompt("Input grade: " );
        while (!parseInt(g)) {
            g = prompt("Input grade: " );
        }
        var grade = parseInt(g);
        total += grade;
        i++;

    }
    while(i<=n);
    var average=(total)/n;
    console.log("Average is: " +average);

and console.log needs to be document.write in your case, but for testing purposes and because jsfiddle (of course) doesn't allow document.write you need to check your console to see the correct value.

What changes from your script to this one is that we are declaring total as a global variable, not as a local variable inside the do loop that will be reset each time you loop.

Next, we're using the same logic as the first prompt for the second one, because you want, again, an integer value, not a possible string like "hey".

After that, we're ADDING that value to the total variable, by not redeclaring it.

Finally, after the loop, we're dividing that global variable total by the global variable n, getting the average.

🌐
Teachics
teachics.org β€Ί home β€Ί javascript examples β€Ί javascript program to read values using prompt popup box
JavaScript program to read values using prompt popup box | JavaScript Examples | Teachics
June 4, 2024 - Write a JavaScript Program to create an Array and read values using the Prompt popup box and display the sum of elements in an Alert Box.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript-window-prompt-method
Javascript Window prompt() Method | GeeksforGeeks
September 23, 2024 - The prompt() method returns the input value when the user clicks "OK" else returns null. ... message is a string of text to display to the user. It can be omitted if there is nothing to show in the prompt window i.e.
🌐
JavaScript Tutorial
javascripttutorial.net β€Ί home β€Ί javascript bom β€Ί javascript prompt
Learn JavaScript prompt By Practical Examples
March 5, 2022 - The following example uses prompt() to display a dialog that asks users for their ages. If users are 16 years old or above, they are eligible to join. Otherwise, they will not be. let ageStr = prompt('How old are you?'); let age = Number(ageStr); let feedback = age >= 16 ? 'You're eligible to join.' : 'You must be at least 16 year old to join.'; alert(feedback);Code language: PHP (php)
🌐
Javatpoint
javatpoint.com β€Ί javascript-prompt-dialog-box
JavaScript prompt() dialog box
JavaScript prompt() dialog box with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
🌐
Filo
askfilo.com β€Ί higher education β€Ί smart solutions β€Ί set a create a program that demonstrates the use of alert, pro
SET A Create a program that demonstrates the use of alert, prompt, and c..
December 29, 2025 - Create a JavaScript function to calculate the square of a number entered by the user. Display the result using alert(). ... // Demonstration of alert, prompt, and console.log alert('Welcome to the JavaScript demonstration!'); // Prompt user ...
🌐
Coders Campus
coderscampus.com β€Ί home β€Ί ep34 – javascript alerts and prompts – getting and showing data from users
EP34 - JavaScript Alerts and Prompts - Getting and Showing Data from Users - Coders Campus
April 9, 2021 - It will just put up a dialogue box on screen with any message you'd like to display along with a text box allowing them to input anything they like. ... Notice that we use a variable (called response) to store the input that the user types into the prompt. If you'd like, you can go ahead and click here to see what that prompt looks like. The confirm function is used in a very similar fashion as the alert function, but with one big difference.
🌐
ByteScout
bytescout.com β€Ί blog β€Ί interaction-using-alert-prompt-and-confirm-using-js.html
Interaction using Alert, Prompt, and Confirm using JS – ByteScout
Similarly, the developers can create an alert box for displaying a pop-up to the user or utilizing the alert function for debugging. The prompt() function in JavaScript works by displaying a box to the users asking for their input. Usually, users can see a prompt box before entering a specific ...