#15 JS: if else statement

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


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


<<:  【第一天 - Flutter 生命周期+基本观念介绍】

>>:  Day.7 保有日常备份重要性 - binlog 解析 &备份资料 (mysqldump / binlog)

Day14:堆积排序(Heap Sort)

堆积(Heap) 堆积,是一种树状结构,用於实现「优先伫列(Priority queue)」。Pri...

Dungeon Mizarka 005

UI版面配置 几近年的FPDC游戏,单角色的控制多以First Preson Shooter玩法为主...

Day 4 ( 入门 ) 一直向下的箭头

一直向下的箭头 教学原文参考:一直向下的箭头 这篇文章会介绍如何使用「当姿势倾斜发生」搭配「箭头数字...

认识CSS(一):什麽是CSS

CSS(Cascading Style Sheets)是建立在HTML的基础上,它既不是标准程序语言...

[Day30] Cloud Meow Meow

喵喵喵喵喵喵喵喵!!!终於写完 30 天的文章了,这 30 天中,我们从云端的概念开始,进入了 Go...