Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 views
in Technique[技术] by (71.8m points)

html - 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 in input fields. And when I click on an OK button, it should tell me what sign of product I have. Is it a postive number(+) or negative(-)?

<body>
Please enter numbers that aren't 0.
<input id="x" type="text" name="" value="">
<input id="y" type="text" name="" value="">
<input id="z" type="text" name="" value="">
<button type="onclick" onclick="value()" name="button">OK</button>

<script>
 function value() {
 document.getElementById('x').innerHTML;
 document.getElementById('y').innerHTML;
 document.getElementById('z').innerHTML;

 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 if (x<0 && y>0 && z<0)
       {
         alert("The sign is +");
       }
       else
       {
         alert("The sign is -");
       }
  }
    </script>
      </body>
   </html> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...