Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...