Created
December 10, 2018 12:55
-
-
Save NikitaGlukhi/11765a367590fb6f1f58f2c1e4e452bf to your computer and use it in GitHub Desktop.
Task 2 for constructor functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Calculator = function () { | |
this.read = function () { | |
this.value1 = +prompt('Insert first value(task 2)', 0); | |
this.value2 = +prompt('Insert second value(task 2)', 0); | |
// Почему так? Да потому что вводимые параметры имеют тип данных string, а при сложение двух строк/числа и строки начинают происходить удивительные вещи(например, оператор + тупо складывает строки в прямом смысле, не суммируя их) | |
}; | |
this.sum = function () { | |
return this.value1 + this.value2; | |
}; | |
this.mul = function () { | |
return this.value1 * this.value2; | |
} | |
}; | |
const calculator = new Calculator(); | |
calculator.read(); | |
alert(`Сумма ${calculator.value1} и ${calculator.value2} равна ${calculator.sum()}`); | |
alert(`Произведение ${calculator.value1} и ${calculator.value2} равна ${calculator.mul()}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment