# 15: Exercises in JavaScript | Create a Calculator Using JavaScript | JavaScript Tutorial | mmtuts

Exercises in JavaScript | Create a Calculator Using JavaScript | JavaScript Tutorial | mmtuts. In this JavaScript tutorial you will create your first JavaScript exercise. The exercise will be building a calculator using JavaScript which is a very good and simply project to start out with.

First of all, thank you for all the support you have given me!

I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!

I am now using Patreon to share improved and updated lesson material, and for a small fee you can access all the material. I have worked hard, and done my best to help you understand what I teach.

I hope you will find it helpful 🙂

Material for this lesson: https://www.patreon.com/posts/javascript-15-18708464

Original source

### 36 thoughts on “15: Exercises in JavaScript | Create a Calculator Using JavaScript | JavaScript Tutorial | mmtuts”

• October 30, 2019 at 18:17

I love ur channel sir, thanks a lot sir, God bless u. Dummies like me have been looking for this kind of channel

• October 30, 2019 at 18:17

Hey! Thank you so much! Quick question, Why do you do one if statement and the other 3 else if statements? Why not all if?

• October 30, 2019 at 18:17

wonderful tutorial bro…please do some intermediate level javascript tutorials

• October 30, 2019 at 18:17

I cannot express how helpful these tutorials are. Keep up the good work, man!

• October 30, 2019 at 18:17

If you want to get result on the main screen other than inner window then you can use this statement in your code document.getElementById("result").innerHTML = Answer; and make a div in HTML page as well 😛

• October 30, 2019 at 18:17

I appreciate how you explain things by starting with what you're going to do and use and by describing the connection between the code, what it does and what it allows the user to do. Nice!

• October 30, 2019 at 18:17

When I do the calculator it says calculate is not defined

• October 30, 2019 at 18:17

Why is it when I alert the value of a and b to say what the values are, it still shows as NaN(not a number) even though its been given the parseInt method???

Alert( "value 1 =" + a + "value 2 = +b);
Value 1 = NaN value 2 = NaN

• October 30, 2019 at 18:17

The calculator went great! If I want to multiply two numbers, one is input by the user and the other one is fixed (Set by the system) call it 7%, how can I achieve that?

• October 30, 2019 at 18:17

thanks man I put borders in my div box and centered the text, all in all, this is a very good video

• October 30, 2019 at 18:17

Great tutorial. There's lots of info out there explaining variables, loops etc but not a huge amount of tutorials showing how to put it into practice. Keep up the good work

• October 30, 2019 at 18:17

One of the better explanations I've seen on this subject, and I've been looking for a few…days. Thanks man!

• October 30, 2019 at 18:17

Great video. Helped me out a lot. I'mma be watching the rest of the JS tutorial since it seems to be helping me more than my university course is.

• October 30, 2019 at 18:17

Is there any code related, XML file pushed to Java messaging service queue using Java script

• October 30, 2019 at 18:17

Could u also do it with an 'AddEventListener' or is that not the right way?

• October 30, 2019 at 18:17

for class debate, a little changed:
html:

<body>
<form>
<!– type="number" -> so letters can not be Enterd –>
value1: <input type="number" id="val1"><br>
value2: <input type="number" id="val2"><br>
operator:
<select id="operator">
<option value="subtruct">Subtruct</option>
<option value="mul">Multiply</option>
<option value="divide">Divide</option>
<option value="pow">Power</option>
</select>
<button type="button" onclick="calc()">Calculate</button>
</form>
<div>
<!– result in an expected location –>
<p>Result: <span id="result"></span></p>
</div>
<script src=" js/main.js"> </script>
</body>

js:

function calc() {
var a = parseInt(document.querySelector('#val1').value);
var b = parseInt(document.querySelector('#val2').value);
var oper = document.querySelector('#operator').value;

var calculate;

calculate = a + b;
} else if (oper == 'subtruct') {
calculate = a – b;
} else if (oper == 'mul') {
calculate = a * b;
} else if (oper == 'pow') {
calculate = a ** b;
} else {
calculate = a / b;
}
document.querySelector('#result').innerHTML = calculate;
console.log(calculate);
}

• October 30, 2019 at 18:17

@mmtuts why when i try to console.log(a) i get: ///Uncaught ReferenceError: a is not defined
at <anonymous>:1:13///. But the calculator works???

• October 30, 2019 at 18:17

Really enjoyed this! You do a great job explaining the concepts. I'm going to watch all of your JS tutorials now. 🙂

• October 30, 2019 at 18:17