Skip to content Skip to sidebar Skip to footer

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:

  1. Input elements have a value not, innerHTML
  2. Even if you would have corrected that, you will not have values, since you did not assign them to any variable
  3. For number only input use the number 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 +");
  } else if (x < 0 && y < 0 && z < 0) {
    alert("The sign is +");
  } else if (x > 0 && y < 0 && z < 0) {
    alert("The sign is +");
  } else if (x < 0 && y > 0 && z < 0) {
    alert("The sign is +");
  } else {
    alert("The sign is -");
  }
});
Please enter numbers that aren't 0.<br>
<input id="x" type="number" name="" value="">
<input id="y" type="number" name="" value="">
<input id="z" type="number" name="" value="">
<button type="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"