You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.2 KiB
60 lines
1.2 KiB
"use strict"; |
|
|
|
function main(formName) { |
|
let result = document.getElementById("result"); |
|
|
|
let a = document.forms[formName].elements[0].value; |
|
if (isNotNumber(a)) { |
|
result.textContent = "Ошибка: a не является числом!"; |
|
return; |
|
} else { |
|
a = Number(a); |
|
} |
|
|
|
let b = document.forms[formName].elements[1].value; |
|
if (isNotNumber(b)) { |
|
result.textContent = "Ошибка: b не является числом!"; |
|
return; |
|
} else { |
|
b = Number(b); |
|
} |
|
|
|
result.textContent = "Результат: " + Calculate(a, b); |
|
} |
|
|
|
function Calculate(a, b) { |
|
class Calculator { |
|
constructor(a, b) { |
|
this.a = a; |
|
this.b = b; |
|
} |
|
|
|
add() { |
|
return this.a + this.b; |
|
} |
|
|
|
subtract() { |
|
return this.a - this.b; |
|
} |
|
|
|
multiply() { |
|
return this.a * this.b; |
|
} |
|
|
|
devide() { |
|
return this.a / this.b; |
|
} |
|
} |
|
|
|
let result; |
|
let calc = new Calculator(a, b); |
|
result = "сложение: " + calc.add(); |
|
result += ", вычитание: " + calc.subtract(); |
|
result += ", умножение: " + calc.multiply(); |
|
result += ", деление: " + calc.devide(); |
|
return result; |
|
} |
|
|
|
function isNotNumber(val) { |
|
return val.replace(/\s/g, '').length === 0 || isNaN(val); |
|
}
|
|
|