Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

More Books

Students also viewed these Databases questions