iT邦幫忙

2021 iThome 鐵人賽

DAY 15
0

To make the operators meaningful for users, let’s explain by examples

if statement

If the statement is true, the code inside of {} would be executed.
On the contrary, it would be ignored when it's false.

var money=prompt("How much money would you like to withdraw?");
if(money>30000) {
    alert("You've reached the withdraw money limit within a day.");
}

https://ithelp.ithome.com.tw/upload/images/20210914/20130362jtO6o5296p.png
https://ithelp.ithome.com.tw/upload/images/20210914/20130362f7VkTRq599.png

if else statement

Following the statement above, if the first statement is false, else {} would be executed.

var money=prompt("How much money would you like to withdraw?", "");
if(money>30000) {
    alert("You've reached the withdraw money limit within a day.");
}else{
    alert("Your money is preparing. Don't forget your ATM card.");
}

https://ithelp.ithome.com.tw/upload/images/20210914/20130362E4ppbrV9M8.png

if else statement (multiple filters)

Following the 2nd statement, when we need more filters to execute the statements, that is, if both of the first statement and the former else if {} are false, the latter else if {} would be executed until the value complies with the string.

var n=prompt("How much money would you like to withdraw?", "");
if(n>500) {
    alert(n+">500");
}else if (n>300){
    alert(n+">300");
}else if (n>150){
    alert(n+">150");
}else {
    alert(n+"<=150");
}

Example

var n1=prompt("Please key in a random number.","");
var n2=prompt("Please key in a random number.","");
var op=prompt("Please key in a math symbol: +, -, *, /",""); //op = operator
var result;
if(op=="+") {
    result=n1+n2;
}else if (op=="-"){
    result=n1-n2;
}else if (op=="*"){
    result=n1*n2;
}else if (op=="/"){
    result=n1/n2;
}else {
    result="This operator does not comply with the rule.";
}
alert(result);

However, the number user keys in would be considered as a string, like "7" instead of purely 7, so if n1 is "7", n2 is "7", and the symbol is +, the result would be 77 instead of 14.
https://ithelp.ithome.com.tw/upload/images/20210914/20130362Zp33e9X1mT.png

To change the data type, we have to transfer it from string to number by storing a function--Number--into the variable. (Not sure if it's called function here? Please correct me if I'm wrong. Thank you.)

var n1=prompt("Please key in a random number.","");
var n2=prompt("Please key in a random number.","");

n1=Number(n1);
n2=Number(n2);

var op=prompt("Please key in a math symbol: +, -, *, /",""); //op = operator
var result;
if(op=="+") {
    result=n1+n2;
}else if (op=="-"){
    result=n1-n2;
}else if (op=="*"){
    result=n1*n2;
}else if (op=="/"){
    result=n1/n2;
}else {
    result="This operator does not comply with the rule.";
}
alert(result);

Music of Today: 絕對 Absolutely by 張震嶽 ayal komod

Yes


Like/Share/Follow

Feel free to comment and share your ideas below to learn together!
If you guys find this article helpful, please kindly do the writer a favor — LIKE this article./images/emoticon/emoticon12.gif


上一篇
#14 JS: create a calculator by prompt()
下一篇
#16 JS: loop - Part 1
系列文
Learn & Play JavaScript -- Entry-Level Front-End Web Development30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言