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.
➤ GET ACCESS TO MY LESSON MATERIAL HERE!
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 responses to “15: Exercises in JavaScript | Create a Calculator Using JavaScript | JavaScript Tutorial | mmtuts”
I love ur channel sir, thanks a lot sir, God bless u. Dummies like me have been looking for this kind of channel
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?
Great Video! This was very helpful <3
It says undefined when I click calculate.
good video, thanks
please make more javascript Exercises
wonderful tutorial bro…please do some intermediate level javascript tutorials
I cannot express how helpful these tutorials are. Keep up the good work, man!
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 😛
Better then parseint you should use parsefloat for dsml no
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!
its not working
When I do the calculator it says calculate is not defined
Please someone help me
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);
//This page says
Value 1 = NaN value 2 = NaN
that was pretty funny and nice, thanx a lot
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?
<3
so no head?
thanks man I put borders in my div box and centered the text, all in all, this is a very good video
it is saying undefine in my code
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
One of the better explanations I've seen on this subject, and I've been looking for a few…days. Thanks man!
U can also use switch statement
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.
Is there any code related, XML file pushed to Java messaging service queue using Java script
Could u also do it with an 'AddEventListener' or is that not the right way?
i dont get access to the .value i only get nodevalue help please.
Ah my degree module never describes jS like you. So easy
function calc() undefined
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="add">Add</option>
<option value="subtruct">Subtruct</option>
<option value="mul">Multiply</option>
<option value="divide">Divide</option>
<!– added power 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;
if (oper == 'add') {
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);
}
Thank you. this is really useful. keep up the good work!
@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???
Really enjoyed this! You do a great job explaining the concepts. I'm going to watch all of your JS tutorials now. 🙂
What is this tool?
Omg. It's so difficult for my stupid brain. Programming is not for me
Thank you! This is helpful.
Question: When writing the first conditional statement, does op == "add" have to use ==? Would === work the same way? Why or why not?