[Learning]#14 JS: create a calculator by prompt()

Vida Lin
2 min readOct 2, 2021

Final Calculator Design

<!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 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");
var n2=prompt("Please key in a random number." , "7");
var result=n1*n2;
alert(result);

=> The result is 49.

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

Clap/Share/Follow

If you guys find this article helpful, please kindly do the writer a favor — giving 5 clicks at the GREEN area and 10~50 claps at the bottom of this page.

Most important of all, feel free to comment and share your ideas below to learn together!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Vida Lin
Vida Lin

Written by Vida Lin

PM, Relationship, Travel, or anything quirky but interesting.|Subscribe & buy Vida a drink @LikerLand, the bottom of each article. 在文章底下LikerLand訂閱、請Vida喝一杯飲料 ❤

Responses (1)

Write a response

if(錢不夠 || 心委屈了) return '離職';