Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Enter values with prompt and evaluate on the basis of conditions in JavaScript?
JavaScript's prompt() function allows you to collect user input and evaluate it using conditional statements. This is useful for creating interactive web applications that respond based on user-provided values.
Basic Syntax
To get user input and convert it to a number:
var value = parseInt(prompt("Enter a value"));
Example: Conditional Evaluation Based on User Input
Here's a complete example that prompts for two values and evaluates their product:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prompt and Evaluate</title>
</head>
<body>
<h2>Result will appear below:</h2>
<div id="result"></div>
<script>
var firstValue = parseInt(prompt("Enter the first value"));
var secondValue = parseInt(prompt("Enter the second value"));
var resultDiv = document.getElementById("result");
if (firstValue * secondValue > 50) {
resultDiv.innerHTML = "<h3 style='color: green;'>Result is correct (Product: " + (firstValue * secondValue) + ")</h3>";
} else {
resultDiv.innerHTML = "<h3 style='color: red;'>Result is not correct (Product: " + (firstValue * secondValue) + ")</h3>";
}
</script>
</body>
</html>
How It Works
The example follows this process:
-
prompt()displays a dialog box asking for user input -
parseInt()converts the string input to an integer - The condition checks if the product of both values exceeds 50
- Based on the result, appropriate feedback is displayed
Multiple Condition Example
You can create more complex evaluations with multiple conditions:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Evaluation</title>
</head>
<body>
<div id="output"></div>
<script>
var num1 = parseInt(prompt("Enter first number"));
var num2 = parseInt(prompt("Enter second number"));
var sum = num1 + num2;
var product = num1 * num2;
var output = document.getElementById("output");
if (sum > 20 && product > 50) {
output.innerHTML = "<h3>Excellent! Both sum (" + sum + ") and product (" + product + ") meet criteria.</h3>";
} else if (sum > 20) {
output.innerHTML = "<h3>Good! Sum is " + sum + " but product " + product + " is too low.</h3>";
} else if (product > 50) {
output.innerHTML = "<h3>Good! Product is " + product + " but sum " + sum + " is too low.</h3>";
} else {
output.innerHTML = "<h3>Both sum (" + sum + ") and product (" + product + ") are below criteria.</h3>";
}
</script>
</body>
</html>
Key Points
-
prompt()always returns a string, so useparseInt()orNumber()for numeric calculations - Handle cases where users click "Cancel" or enter non-numeric values
- Use DOM manipulation instead of
document.write()for better practice - Consider input validation to prevent errors
Conclusion
Using prompt() with conditional statements creates interactive web pages that respond to user input. Always convert string inputs to numbers when performing mathematical operations and provide clear feedback based on evaluation results.
Advertisements
