Ex1: Create function User will enter first number Then enter an operation (sum ,multi…
- User
Ex1: Create function User will enter first number Then enter an operation (sum ,multi ,subtract ,division) as string Then enter second number Then do the operation and show the result to user by html and js
- Assistant
Sure! Here's an example of how you can create a function using HTML and JavaScript to perform basic arithmetic operations and display the result to the user:
HTML:
<!DOCTYPE html> <html> <head> <title>Arithmetic Calculator</title> <script src="script.js"></script> </head> <body> <h1>Arithmetic Calculator</h1> <input type="number" id="num1" placeholder="Enter first number"><br><br> <input type="text" id="operator" placeholder="Enter operation"><br><br> <input type="number" id="num2" placeholder="Enter second number"><br><br> <button onclick="calculate()">Calculate</button><br><br> <h2 id="result"></h2> </body> </html> - User
Ex1: Create function User will enter first number Then enter an operation (sum ,multi ,subtract ,division) as string Then enter second number Then do the operation and show the result to user by html and js by prompet
- Assistant
Sure! Here's an example of how you can create a function using HTML and JavaScript using the
prompt()method to get user input and display the result:HTML:
<!DOCTYPE html> <html> <head> <title>Arithmetic Calculator</title> <script src="script.js"></script> </head> <body> <h1>Arithmetic Calculator</h1> <button onclick="performOperation()">Perform Operation</button><br><br> <h2 id="result"></h2> </body> </html>JavaScript (script.js):
function performOperation() { var num1 = parseFloat(prompt("Enter first number:")); var operator = prompt("Enter operation: (sum, multi, subtract, division)"); var num2 = parseFloat(prompt("Enter second number:")); var result; switch (operator) { case "sum": result = num1 + num2; break; case "multi": result = num1 * num2; break; case "subtract": result = num1 - num2; break; case "division": if (num2 !== 0) { result = num1 / num2; } else { result = "Cannot divide by zero"; } break; default: result = "Invalid operator"; } document.getElementById("result").innerHTML = "Result: " + result; } - User
Ex2;
- Create function User will enter his Username , Password If username = “admin” and password = “421$$” Show him message “Welcome login success” If he entered incorrect username or password Tell him which data entered wrong by html and js by prompet
- Assistant
Sure! Here's an example of how you can create a function using HTML and JavaScript to check if the user entered the correct username and password:
HTML:
<!DOCTYPE html> <html> <head> <title>Login</title> <script src="script.js"></script> </head> <body> <h1>Login</h1> <button onclick="checkLogin()">Login</button> <h2 id="result"></h2> </body> </html>JavaScript (script.js):
function checkLogin() { var username = prompt("Enter your username:"); var password = prompt("Enter your password:"); if (username === "admin" && password === "421$$") { document.getElementById("result").innerHTML = "Welcome! Login success"; } else if (username !== "admin" && password !== "421$$") { document.getElementById("result").innerHTML = "Incorrect username and password entered"; } else if (username !== "admin") { document.getElementById("result").innerHTML = "Incorrect username entered"; } else if (password !== "421$$") { document.getElementById("result").innerHTML = "Incorrect password entered"; } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.