Build this JS calculator in 15 minutes! ๐Ÿ–ฉ

Bro Code
1 Dec 202315:20

Summary

TLDRIn this instructional video, the creator guides viewers through building a basic calculator using HTML, CSS, and JavaScript. The process involves setting up a 'calculator' div with a 'display' input for number entry, which is set to read-only. A 'keys' div contains buttons for digits, operators, and functions like 'equals' and 'clear'. The buttons are styled with CSS, creating a grid layout with a modern, rounded appearance. JavaScript is used for functionality, including appending numbers to the display, calculating results with the 'eval' function, and handling errors. The final product is a simple yet visually appealing calculator that can perform basic arithmetic operations.

Takeaways

  • ๐ŸŒŸ The video tutorial focuses on creating a calculator program using HTML, CSS, and JavaScript.
  • ๐Ÿ“ HTML is used to structure the calculator with a 'div' element for the container, an 'input' element for display, and buttons for numbers and operations.
  • ๐Ÿ–Œ๏ธ CSS is applied to style the buttons, making them round with a specific size, color, and hover effects to enhance user interaction.
  • ๐Ÿ”ข The 'display' input element is set to 'readonly' to prevent direct text input, ensuring it only shows numbers and operators.
  • ๐ŸŽจ The calculator's design includes a grid layout for buttons, a specific font family, and a max-width to ensure it is responsive and visually appealing.
  • ๐Ÿ”„ The JavaScript functionality involves creating functions for appending numbers to the display, calculating the result, and clearing the display.
  • ๐Ÿ”ง Error handling is implemented using 'try' and 'catch' blocks to manage scenarios where the equation is incomplete or incorrect.
  • ๐Ÿ”„ The 'eval' function in JavaScript is utilized to calculate and return the result of the equation entered in the display.
  • ๐Ÿ‘‰ The tutorial demonstrates the process of copying and pasting code for repetitive elements, like the number buttons, to streamline development.
  • ๐ŸŽจ Additional CSS styling is applied to differentiate operator buttons with a distinct color and interactive feedback when clicked.
  • ๐Ÿ“ฑ The final result is a functional and styled calculator that can be used to perform basic arithmetic operations.

Q & A

  • What is the main purpose of the video?

    -The main purpose of the video is to guide viewers through the process of creating a calculator program using HTML, CSS, and JavaScript.

  • What is the first element created in the calculator program?

    -The first element created is a div element with an ID of 'calculator' which will serve as the container for the calculator.

  • What is the purpose of the 'display' input element?

    -The 'display' input element is used to show the numbers that are typed in. It is set to be read-only to prevent direct text entry.

  • How many buttons are initially created for the calculator keys?

    -Initially, 14 buttons are created for the calculator keys, including numbers 7 to 0, the plus, minus, multiplication, and division symbols.

  • What is the purpose of the 'keys' div element?

    -The 'keys' div element is used to nest and contain all of the button elements representing the calculator keys.

  • What is the text on the button that is used to start calculations?

    -The text on the button used to start calculations is 'equals'.

  • How are the buttons styled in the CSS stylesheet?

    -The buttons are styled with a width and height of 100 pixels, rounded corners with a border radius of 50 pixels, no border, a dark background color with HSL values, white text color, and bold font weight.

  • What CSS property is used to arrange the buttons in a grid layout?

    -The 'display' property is set to 'grid', and 'grid-template-columns' is used with the 'repeat' function to create a four-column layout.

  • What is the purpose of the 'clear' button in the calculator?

    -The 'clear' button is used to clear the current display, allowing the user to start a new calculation.

  • How does the JavaScript part of the program handle button clicks?

    -The JavaScript part uses the 'onclick' attribute of each button to call specific functions like 'appendToDisplay', 'calculate', and 'clearDisplay' when the buttons are clicked.

  • What JavaScript function is used to append characters to the display?

    -The 'appendToDisplay' function is used to append characters to the display when a button is clicked.

  • How does the video handle potential errors in the calculator program?

    -The video uses a try-catch block to handle potential errors, such as an uncaught syntax error, by changing the display value to 'error'.

  • What is the final step in the JavaScript functionality for the calculator?

    -The final step is to use the 'eval' function to evaluate the expression in the display and show the result, with error handling to manage incomplete equations.

  • How does the video ensure the calculator is centered on the page?

    -The video sets the body's display to 'flex', uses 'justify-content' and 'align-items' to center the content, and sets the body height to '100vh' to ensure the calculator is centered both horizontally and vertically.

  • What additional CSS styling is applied to the operator buttons?

    -The operator buttons are given a new class 'operatorBtn', styled with an orange background color, and lighter shades on hover and active states to create a flashing effect.

  • How does the video script guide the creation of the calculator's display?

    -The script guides the creation of a read-only input element with the ID 'display', styles it with CSS, and uses JavaScript functions to update its value based on user interactions.

Outlines

00:00

๐Ÿ› ๏ธ Building a Calculator with HTML, CSS, and JavaScript

In this segment, the video tutorial begins by introducing the project of creating a calculator using HTML, CSS, and JavaScript. The presenter outlines the initial steps, which include setting up the HTML structure with a 'div' element identified by the ID 'calculator'. Inside this 'div', an input element with the ID 'display' is created to show the numbers entered, with a 'readonly' property to prevent direct text input. A nested 'div' with the ID 'keys' is also established to house the buttons for calculator operations. The tutorial proceeds to describe the creation and replication of button elements for digits 0-9, basic arithmetic operators (plus, minus, multiply, divide), and a decimal point, each with an 'onclick' attribute linked to a JavaScript function that will be defined later. The first button created is for the plus operator, with a function to append its character to the display. The process involves copying this button's structure and adjusting it for the remaining digits and operators, totaling 15 buttons including the equals and clear buttons.

05:01

๐ŸŽจ Styling the Calculator Interface with CSS

The second paragraph focuses on styling the calculator's interface using CSS. The presenter details the process of selecting all buttons and applying uniform styles, such as setting their width and height to 100 pixels, using a border radius of 50 pixels for a rounded look, removing borders, and styling the text with white color, a font size of 3rem, and bold weight. A pointer cursor is specified for hover effects. The 'keys' div, which contains all buttons, is styled using CSS Grid, with four columns of equal width (1fr) and a 10-pixel gap between rows. Padding around the keys and the calculator is set to 25 pixels, and the calculator's font family, background color, border radius, and max-width are defined. The display area is styled to take up the full width with padding and a specific font size and alignment. The body of the document is styled to center the calculator both horizontally and vertically, with a height set to 100vh. The presenter also describes interactive hover and active states for the buttons, changing the background color's lightness to create a 'light up' and 'flash' effect, and differentiates the operator buttons with a distinct orange color and lighter shades for hover and active states.

10:03

๐Ÿ“ Adding Functionality with JavaScript

In this part, the tutorial moves on to adding functionality to the calculator using JavaScript. The script begins by obtaining a reference to the display element using its ID. Three main functions are outlined: 'append to display', 'calculate', and 'clear display'. The 'append to display' function takes a character as a parameter and appends it to the current value of the display. The 'clear display' function sets the display's value to an empty string, effectively clearing it. The 'calculate' function retrieves the value from the display, uses the 'eval' function to evaluate the expression as JavaScript code, and then updates the display with the result. Error handling is introduced to manage cases where an incomplete equation is entered, using a try-catch block to display an 'Error' message if an exception occurs. The video demonstrates testing the calculator's functionality with sample inputs and operations, ensuring that the calculator can handle basic arithmetic and error scenarios.

15:08

๐ŸŽ‰ Completing the Calculator Program

The final paragraph wraps up the tutorial by summarizing the completed calculator program. The presenter highlights that the calculator, now functional with JavaScript, HTML, and CSS, can be used to impress friends with its interactive design and basic arithmetic capabilities. The tutorial concludes with a demonstration of the calculator performing calculations and handling errors gracefully, showcasing the final product of the video's instructions.

Mindmap

Keywords

๐Ÿ’กCalculator Program

A calculator program is a software application designed to perform mathematical calculations. In the context of the video, the creator is building a calculator using HTML, CSS, and JavaScript. This involves creating a user interface with buttons for numbers and operations, and scripting functionality to perform calculations when the buttons are interacted with.

๐Ÿ’กHTML

HTML, or HyperText Markup Language, is the standard markup language used to create and design web pages. In the video, HTML is used to structure the calculator's interface, creating elements such as a div container, an input element for displaying numbers, and button elements for the calculator's keys.

๐Ÿ’กCSS

CSS, or Cascading Style Sheets, is a stylesheet language used for describing the presentation of a document written in HTML. In the video, CSS is used to style the calculator's buttons, making them visually appealing with properties like width, height, border radius, and background color. It also arranges the layout of the calculator on the web page.

๐Ÿ’กJavaScript

JavaScript is a programming language that allows interactive web pages and is an essential part of web applications. In the video, JavaScript is used to add functionality to the calculator, such as appending numbers to the display, performing calculations when the equals button is pressed, and clearing the display.

๐Ÿ’กDiv Element

A div element in HTML is a container used to group block-level elements. In the script, the creator uses a div with an ID of 'calculator' to contain all the elements that make up the calculator's interface.

๐Ÿ’กInput Element

The input element in HTML is used to create interactive fields where users can input data. In the video, an input element with a read-only property is used to display the numbers and operators as they are entered, without allowing the user to type directly into it.

๐Ÿ’กButton Element

A button element in HTML is used to create clickable buttons. The video script describes creating multiple button elements, each with an onclick attribute that triggers a JavaScript function to handle the button's action, such as appending a number or operator to the display.

๐Ÿ’กCSS Grid

CSS Grid is a layout system in CSS that allows elements to be positioned in rows and columns. The video mentions setting the display property to 'grid' for the 'keys' element, which contains the calculator buttons, to arrange them in a grid layout with four columns using the 'repeat' function and the 'fr' unit.

๐Ÿ’กPseudo Classes

Pseudo classes in CSS are used to define special states of an element. The video script refers to the 'hover' and 'active' pseudo classes to change the appearance of the buttons when the mouse hovers over them or when they are clicked, enhancing the user interaction experience.

๐Ÿ’กEval Function

The eval function in JavaScript is used to evaluate code represented as a string. In the video, the eval function is mentioned as a way to evaluate the mathematical expression entered into the calculator's display and return the result.

๐Ÿ’กError Handling

Error handling in programming involves responding to the occurrence of an error or exception. The video script discusses the importance of error handling when using the eval function, as it can cause an error if the mathematical expression is incomplete. The creator uses a try-catch block to handle potential errors and display an 'Error' message.

Highlights

Introduction to creating a calculator program using HTML, CSS, and JavaScript.

Creating a div element with an ID of 'calculator' to serve as the container.

Adding an input element with an ID of 'display' and setting it to read-only to prevent text entry.

Creating a nested div with an ID of 'keys' to contain the calculator buttons.

Adding buttons for digits and operators with specific onclick attributes for JavaScript functions.

Creating a function 'appendToDisplay' to handle button clicks and display updates.

Styling buttons with CSS to have equal width and height, rounded corners, and hover effects.

Arranging buttons in a grid layout using CSS grid properties.

Styling the 'calculator' ID with a specific font family, background color, and border radius.

Setting the display element to have a full width and padding for better visibility.

Centering the calculator on the page using Flexbox properties on the body.

Adding functionality to the 'clear' button to reset the display.

Creating a 'calculate' function to perform arithmetic operations using the eval function.

Handling errors with a try-catch block to manage incomplete equations.

Customizing the color and hover effects for operator buttons to distinguish them.

Finalizing the calculator program with JavaScript functionality to impress friends.

Transcripts

play00:00

hey what's going on everybody in today's

play00:02

video we're going to create a calculator

play00:04

program using HTML CSS and JavaScript so

play00:07

let's get started all right let's do

play00:09

this thing everybody we have a lot of

play00:11

buttons to create but we'll need a

play00:13

container I will create a div element

play00:16

the div element will have an ID of

play00:20

calculator within this div element we'll

play00:23

create an input

play00:25

element the input element will have an

play00:27

ID of display

play00:30

to display the numbers that we type in I

play00:33

don't want somebody to enter in some

play00:34

text for the

play00:36

display I would like this display to be

play00:39

readon I will add the read only property

play00:43

so we can't type in anything even though

play00:45

I'm trying I will create a nested

play00:49

development that has an ID of keys for

play00:52

all of the keys we need to add a lot of

play00:55

buttons we'll begin with the first I

play00:57

will create a button element

play01:00

the text on the button will be plus I

play01:03

will set the onclick attribute of this

play01:07

button to be a JavaScript function we

play01:09

still need to Define this function

play01:11

eventually we'll create a append to

play01:16

display

play01:17

function we have one argument to pass

play01:19

into this JavaScript function a

play01:22

character of

play01:24

plus that's our first button let's copy

play01:27

this button and paste 13 additional

play01:31

times if I counted

play01:39

right okay the second button will be

play01:41

seven the character we're passing in is

play01:45

seven followed by

play01:47

8

play01:50

9

play01:53

minus

play01:55

4

play01:57

5

play01:58

6

play02:01

asterisk for

play02:04

multiplication

play02:06

1

play02:07

2

play02:10

three forward slash for

play02:13

division then

play02:17

zero and here are the new buttons I'm

play02:20

Mis counted we need to add one

play02:22

more a DOT for a

play02:25

decimal now we need an equals button

play02:27

we're going to arrange that a little

play02:29

different let's create a

play02:31

button with the text of

play02:34

equals the onclick attribute of this

play02:37

specific button is going to be

play02:42

calculate then we need a button to clear

play02:45

our

play02:48

screen the text on this button will be

play02:50

capital c for clear the on click

play02:54

attribute of this button is going to be

play02:57

clear display

play03:00

and that is all the buttons we'll

play03:02

need so let's save everything and let's

play03:06

go to our CSS

play03:07

stylesheet I'm going to zoom back to

play03:11

100% first let's style all these buttons

play03:14

I will select all

play03:16

buttons for each button I will set the

play03:19

width to be 100

play03:22

pixels the height to be 100 pixels so

play03:26

they're

play03:26

even I would like rounded buttons I will

play03:30

set the Border radius property to be 50

play03:34

pixels so they're

play03:36

circles let's remove the

play03:39

border border

play03:42

none I'll change the background color of

play03:44

the buttons

play03:48

background-color I'll use hsl values

play03:51

I'll set the lightness to be 30% so

play03:54

they're

play03:54

darker for the text of the button I will

play03:57

set the color to be white

play04:00

for the font

play04:02

size I will set that to be three

play04:07

RM set the font weight to be

play04:12

bold then when I hover my cursor over

play04:14

one of the buttons I would like my

play04:16

cursor to be a

play04:18

pointer

play04:20

cursor

play04:23

pointer now we have to arrange these

play04:25

buttons

play04:26

properly let's select the ID of keys

play04:31

keys is a development that's containing

play04:33

all of the

play04:34

buttons this

play04:36

element all of the buttons are

play04:40

within to arrange these buttons in a

play04:42

grid we can set the display

play04:45

property to be a

play04:48

grid for this calculator I would like

play04:51

four columns to do that I will set the

play04:55

grid template columns property to be

play05:01

we'll use the repeat function of CSS I

play05:04

would like four columns then to arrange

play05:07

these buttons evenly you can use 1

play05:10

F FR stands for fractional unit 1 FR

play05:15

indicates that each column should take

play05:17

up an even amount of space so now we

play05:20

have Columns of four if I were to set

play05:22

this to three we would have Columns of

play05:24

three but I'm going to use Columns of

play05:26

four because I would like all of the

play05:28

operators on the left hand side

play05:30

I'll set the Gap to be 10

play05:35

pixels that is the gap between each of

play05:38

the

play05:38

rows then I'll add some

play05:40

padding of 25

play05:44

pixels that's padding around the

play05:46

keys let's select the ID of

play05:49

calculator I'll add that to the top to

play05:52

keep everything

play05:54

organized with our calculator I will

play05:57

select a font family

play06:00

of Ariel with a backup of s

play06:04

serif let's pick a background color for

play06:07

the

play06:08

calculator I will select something

play06:12

darker I'll set the lightness to be

play06:16

15% I'll round the corners of the

play06:20

calculator border radius 15

play06:26

pixels the corners of the calculator are

play06:28

smooth now

play06:30

then I will set a Max width of the

play06:33

calculator to be 500

play06:36

pixels if any elements overflow I will

play06:39

set that to be

play06:41

hidden this is mostly for our display if

play06:44

we have a very long equation all right

play06:47

next let's select the display right now

play06:49

it's kind of

play06:51

small we are selecting our ID of

play06:55

display let's set the width to be 100%

play07:02

I'll add some padding of 20

play07:05

pixels for the text of the display I

play07:07

will set the font size to

play07:10

be 5

play07:14

RM let's text a line

play07:20

left I'll remove the border border

play07:25

none and I'll change the background

play07:27

color I'll just copy this property

play07:29

because I'm lazy let's increase the

play07:31

lightness to

play07:36

20% then we'll select the body of our

play07:43

document I will remove all margin margin

play07:48

zero I would like the calculator to be

play07:50

in the middle of my window right now

play07:52

it's in the top left corner if you would

play07:54

prefer it up here you can leave it as

play07:57

is I will set the display to be Flex for

play08:01

Flex

play08:03

box

play08:04

justify content in the center for a

play08:09

horizontal alignment for a vertical

play08:12

alignment we can set

play08:15

align items to be Center but we do need

play08:19

to increase the height of the body of

play08:20

the document so it's

play08:23

100% I will set the height to be 100 VH

play08:27

for 100% the viewport height that should

play08:31

place the calculator in the middle of

play08:32

the body of our document both

play08:34

horizontally and vertically for the

play08:37

background color I'm going to decrease

play08:39

the lightness

play08:40

slightly for the background color I will

play08:43

set the lightness to be like

play08:46

95% okay let's go to the bottom of our

play08:49

CSS

play08:50

stylesheet when I hover over one of

play08:52

these buttons I would like to increase

play08:54

the lightness so with all

play08:58

buttons

play08:59

with the hover sudo

play09:01

class change the background color so the

play09:05

lightness is 40% instead of

play09:08

30 now these buttons light up when we

play09:10

hover our cursor over one of the

play09:13

buttons now when I click on one of the

play09:15

buttons I'll increase the lightness

play09:17

further so it

play09:18

flashes we are selecting the active

play09:21

studo class

play09:22

now let's take our background color

play09:25

property increase the lightness to 50%

play09:29

when we hover over a button it lights up

play09:31

when we click on it it flashes

play09:35

momentarily so with all of these

play09:38

operators I would like all of these

play09:39

operator buttons to be a different color

play09:41

I'll pick orange we're going to give

play09:43

each of these buttons a new class let's

play09:46

head back to our HTML file beginning

play09:49

with the plus button I will set the

play09:52

class equal

play09:54

to operator Das BTN for button

play09:59

so let's copy this class paste it for

play10:03

our minus

play10:04

button multiply

play10:06

button divide button and the clear

play10:13

button now we will select the class of

play10:17

operator BTN for

play10:20

button let's change the background

play10:24

color I'm going to set the background

play10:26

color to be

play10:28

orange

play10:30

I've already pre-picked a

play10:34

color when I hover my cursor over one of

play10:37

the operator buttons I would like the

play10:39

color to be a lighter orange instead of

play10:42

gray let's take our operator button

play10:46

access the hover sudo class I'll

play10:48

increase the lightness to

play10:53

65% then when I click on one of these

play10:56

buttons I would like the lightness to

play10:57

increase further

play10:59

with the operator button

play11:01

class with the active pseudo class

play11:04

increase the lightness to

play11:07

75% so when I click on one of the

play11:09

operator buttons it's going to flash

play11:12

momentarily all right and that is all

play11:14

the HTML and CSS that we need now we

play11:17

just need to add functionality let's go

play11:19

to our Javascript

play11:20

file for our Javascript file there's not

play11:23

a lot we have to write first we need to

play11:25

get the display element so what was that

play11:28

ID

play11:31

display const display equals document.

play11:36

getet element by ID get the ID of

play11:42

display we have three functions to

play11:45

create a function for append to display

play11:48

calculate and clear display these three

play11:52

functions function append

play11:57

to display

play12:00

there is one parameter

play12:03

input because we were passing in a

play12:05

character when we call this

play12:07

function then we have a

play12:09

function to clear

play12:14

display then a function to

play12:22

calculate we'll begin with a pen to

play12:24

display all we're going to do is take

play12:26

our display this element access its

play12:30

value append it with plus equals our

play12:35

input and let's see if this works seven

play12:40

I forgot to change the font color of the

play12:42

display so let's do that within our

play12:44

display element let's set the color to

play12:47

be white okay that's much

play12:51

better

play12:56

7.13 + 5

play12:59

equals okay we know that that works when

play13:03

I click on the clear button I would like

play13:04

to clear this

play13:06

display let's access our display element

play13:09

access the value property set it equal

play13:12

to an empty string

play13:17

3.14159 when I hit clear it should clear

play13:20

the

play13:21

display lastly we have

play13:23

calculate let's take our display

play13:26

elements value property set it equal to

play13:30

now we're going to use the eval

play13:33

function the eval function takes an

play13:35

expression such as 1 + 2 + 3 and

play13:39

evaluates it and gives a result it's

play13:41

kind of like it's its own built-in

play13:43

calculator so to say evaluate the value

play13:47

within our

play13:48

display display.

play13:51

value so if I add

play13:55

3.14 plus 1

play14:00

01 I'm given a result of

play14:03

4.15 now for some reason if we get an

play14:05

error for example 7 +

play14:09

equals well we have a problem let's go

play14:12

to our

play14:13

console we've encountered an uncaught

play14:16

syntax error because we never finished

play14:18

our equation in the last lesson we

play14:20

learned about error handling this is

play14:23

dangerous code it can cause an error

play14:26

let's surround this code with a tri

play14:27

block

play14:29

we will try this code and catch any

play14:32

errors that

play14:34

happen so we need a catch block now

play14:37

catch any

play14:39

errors we will change the value of the

play14:42

display to equal some text of

play14:48

error all right and that should work

play14:53

3.14 times equals that results in an

play14:57

error we clear it and then start over

play15:02

what's 1 + 2 + 3 + 4 that would be 10

play15:08

all right everybody so that is a

play15:09

calculator program you can make using

play15:12

JavaScript HTML and CSS impress your

play15:18

friends

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Web DevelopmentCalculator ProgramHTML TutorialCSS StylingJavaScriptCode SnippetsFrontend DesignInteractive UIProgramming GuideEducational Content