Skip to content

Instantly share code, notes, and snippets.

@NikitaGlukhi
Created December 10, 2018 12:55
Show Gist options
  • Save NikitaGlukhi/11765a367590fb6f1f58f2c1e4e452bf to your computer and use it in GitHub Desktop.
Save NikitaGlukhi/11765a367590fb6f1f58f2c1e4e452bf to your computer and use it in GitHub Desktop.
Task 2 for constructor functions
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