Home | HTML | JavaScript | DOM | Data Types |
Hacks
- Write a JavaScript program that compares two variables, a and b. Log “a is greater” if a is greater than b, “b is greater” if b is greater than a, and “both are equal” if a and b are equal. Make sure to use if statements and console.log
%%js
// Use the prompt function to get user input for Batman and Joker's strength levels
let batmanStrength = parseFloat(prompt("Enter Batman's strength level (0-100):"));
let jokerStrength = parseFloat(prompt("Enter Joker's strength level (0-100):"));
// Check if both inputs are valid numbers
if (!isNaN(batmanStrength) && !isNaN(jokerStrength)) {
// Compare Batman and Joker's strengths using if statements
if (batmanStrength > jokerStrength) {
console.log("Batman is greater! He defeats the Joker.");
} else if (jokerStrength > batmanStrength) {
console.log("The Joker is greater! Batman is in trouble.");
} else {
console.log("Batman and the Joker are evenly matched in strength!");
}
} else {
console.log("Please enter valid numeric values for Batman and Joker's strength levels.");
}
<IPython.core.display.Javascript object>