Question: Modify the code in the library.js file so it contains a class named Calculator. This class should have a constructor which defines properties for both

Modify the code in the library.js file so it contains a class named Calculator. This class should
have a constructor which defines properties for both input values.
The class should also have 4 methods, 1 for each of the math operations. If both values are valid
then each function should return the result of the calculation. If the results are not valid, it
should throw an exception.
Modify the claculate.js file to create and use an object from the Calculator class. Then use the
object to perform the calculations when each of the 4 buttons are pressed.
Note that you should still validate the user entries to prevent an exception from being thrown.
Library.js:
"use strict";
calculate.js:
"use strict";
$(document).ready(()=>{
$("#addition").click(()=>{
let value1= parseInt($("#value1").val());
let value2= parseInt($("#value2").val());
if (isNaN(value1)|| isNaN(value2)){
alert("Please make sure both values are valid integers");
} else {
$("#result").val(value1+ value2);
}
});
$("#subtraction").click(()=>{
let value1= parseInt($("#value1").val());
let value2= parseInt($("#value2").val());
if (isNaN(value1)|| isNaN(value2)){
alert("Please make sure both values are valid integers");
} else {
$("#result").val(value1- value2);
}
})
$("#division").click(()=>{
let value1= parseInt($("#value1").val());
let value2= parseInt($("#value2").val());
if (isNaN(value1)|| isNaN(value2)){
alert("Please make sure both values are valid integers");
} else {
$("#result").val(value1/ value2);
}
})
$("#multiplication").click(()=>{
let value1= parseInt($("#value1").val());
let value2= parseInt($("#value2").val());
if (isNaN(value1)|| isNaN(value2)){
alert("Please make sure both values are valid integers");
} else {
$("#result").val(value1* value2);
}
})
$("#cents").focus();
});
index.html:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!