Write A Javascript Conditional Statement To Find The Sign Of Product Of Three Numbers. Display An Alert Box With The Specified Sign
I want to write a JavaScript conditional statement to find the sign of product of three numbers. Display an alert box with the specified sign. The three numbers have to be written
Solution 1:
Why not multiply them then set the condition on the product?
const x = document.getElementById('x').value;
const y = document.getElementById('y').value;
const z = document.getElementById('z').value;
const product = x*y*z;
let sign = (product < 0) ? '-' : '+';
sign = (product === 0) ? 'Neither' : sign;
alert(sign);
Solution 2:
You should parse value of input fields using
var x = Number("Your input field");
Then Your variable will be treated as integer and Your condition of bigger or smaller than 0 will work.
Solution 3:
- Input elements have a
value
not,innerHTML
- Even if you would have corrected that, you will not have values, since you did not assign them to any variable
- For number only
input
use thenumber
type, this will prevent someone from entering not numeral characters (in all modern browsers)
document.getElementById('check-value').addEventListener('click', (e) => {
const x = document.getElementById('x').value;
const y = document.getElementById('y').value;
const z = document.getElementById('z').value;
if (x + y + z >= 0) {
alert("The sign is +");
} elseif (x < 0 && y < 0 && z < 0) {
alert("The sign is +");
} elseif (x > 0 && y < 0 && z < 0) {
alert("The sign is +");
} elseif (x < 0 && y > 0 && z < 0) {
alert("The sign is +");
} else {
alert("The sign is -");
}
});
Please enter numbers that aren't 0.<br><inputid="x"type="number"name=""value=""><inputid="y"type="number"name=""value=""><inputid="z"type="number"name=""value=""><buttontype="button"id="check-value"name="button">OK</button>
Post a Comment for "Write A Javascript Conditional Statement To Find The Sign Of Product Of Three Numbers. Display An Alert Box With The Specified Sign"