iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0

Final Calculator Design

https://ithelp.ithome.com.tw/upload/images/20210912/20130362gh0GXsvAdm.png

<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">

var n1=prompt("Please key in a random number.","e.g.123"); 
var n2=prompt("Please key in a random number.","e.g.123");
var result=n1*n2;
alert(result);

</script>
</head>
</html>

Design a Calculator

prompt() is a dialogue box that user can key in values, and it will close until the user submit or cancel.

Basing on the rule, we can separate the user behavior into steps, like:
input: prompt(default value or an example) → operate → output: alert(data)

For example, to create a simple calculator...

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

https://ithelp.ithome.com.tw/upload/images/20210912/20130362xvqwkSkwKz.png

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

https://ithelp.ithome.com.tw/upload/images/20210912/20130362NVWa00vLOf.png

var result=n1*n2;
alert(result);

=> The result is 49.
https://ithelp.ithome.com.tw/upload/images/20210912/201303621Zo7FVzoZA.png


Music of Today: 馬祖小夜曲 Ma-Chu Song by MC Hotdog 熱狗 Feat. Agota

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


上一篇
#13 JS: Intro to Data, Variables, Operators
下一篇
#15 JS: if else statement
系列文
Learn & Play JavaScript -- Entry-Level Front-End Web Development30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言