Computer ScienceNEB 2082
Write JavaScript code to input any three numbers and find the smallest number among them. [5] OR Write a PHP script to connect to a MySQL database. [5]
5Answer
// JavaScript code to find the smallest number among three inputs
let num1 = parseFloat(prompt("Enter the first number:"));
let num2 = parseFloat(prompt("Enter the second number:"));
let num3 = parseFloat(prompt("Enter the third number:"));
// Using Math.min() to find the smallest number
let smallest = Math.min(num1, num2, num3);
// Display the result
alert(`The smallest number among ${num1}, ${num2}, and ${num3} is: **${smallest}**`);
// Alternative approach (without Math.min())
/*
let smallest;
if (num1 <= num2 && num1 <= num3) {
smallest = num1;
} else if (num2 <= num1 && num2 <= num3) {
smallest = num2;
} else {
smallest = num3;
}
alert(`The smallest number is: **${smallest}**`);
*/
How it works:
- The script prompts the user to input three numbers using
prompt()and converts them to floating-point numbers withparseFloat(). - The
Math.min()function compares the three numbers and returns the smallest value. - The result is displayed using
alert()with a formatted string. - The commented alternative shows a manual comparison using
if-elselogic, which is useful for understanding the underlying logic.
Sample Output:
If the user inputs 5, 2, 9, the output will be:
The smallest number among 5, 2, and 9 is: **2**.
<?php
// PHP script to connect to a MySQL database
// Database connection parameters
$servername = "localhost";
$username = "root"; // Replace with your MySQL username
$password = ""; // Replace with your MySQL password
$dbname = "test_db"; // Replace with your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully to the database!";
}
// Close connection (optional, as PHP automatically closes connections)
$conn->close();
?>
How it works:
- The script defines database connection parameters (
$servername,$username,$password,$dbname). - It creates a connection to the MySQL database using
new mysqli(). - The
connect_errorproperty checks if the connection failed. If it does, it displays an error message. - If the connection is successful, it prints a success message.
- The connection is closed explicitly (though PHP automatically closes it when the script ends).
Sample Output:
If the connection is successful, the output will be:
Connected successfully to the database!
If the connection fails (e.g., wrong credentials), the output will be:
Connection failed: [error message].
Discussion
Loading…
More Computer Science questions
Which of the following is the purpose of using primary key in database ? a) To uniquely identify a record b) To store duplicate record c) To backup data d) To…NEB 2082 (MCQs)1A company needs to modify an existing table by adding a new column for employee email addresses. For this, which SQL command should be used ? a) CREATE b)…NEB 2082 (MCQs)1Which protocol is used for secure communication over a computer network ? a) HTTP b) FTP c) HTTPS d) TelnetNEB 2082 (MCQs)1Which control structure in JS (Java Script) is used to execute a block of code repeatedly based on a given condition ? a) For loop b) if else c) switch case…NEB 2082 (MCQs)1Select the invalid variable name in PHP. a) $name b) $ name c) $1name d) $name123NEB 2082 (MCQs)1The statement int number ( int a); is a ... a) function call b) function definition c) function declaration d) function executionNEB 2082 (MCQs)1