|
@@ -4,7 +4,6 @@
|
|
// Смотрите пример с d(), которая является коротким именем для debugger из материала лекции
|
|
// Смотрите пример с d(), которая является коротким именем для debugger из материала лекции
|
|
|
|
|
|
function a(text) {
|
|
function a(text) {
|
|
- text = prompt('Enter some text:');
|
|
|
|
return alert(text);
|
|
return alert(text);
|
|
};
|
|
};
|
|
|
|
|
|
@@ -13,9 +12,8 @@ function a(text) {
|
|
// Напишите функцию cube, которая возвращает число в третьей степени:
|
|
// Напишите функцию cube, которая возвращает число в третьей степени:
|
|
|
|
|
|
function cube(num) {
|
|
function cube(num) {
|
|
- num = prompt('Enter the number, you will get the cube:');
|
|
|
|
- return alert(Math.pow(num, 3));
|
|
|
|
-};
|
|
|
|
|
|
+ return num * num * num;
|
|
|
|
+}
|
|
|
|
|
|
// avg2
|
|
// avg2
|
|
|
|
|
|
@@ -24,9 +22,7 @@ function cube(num) {
|
|
// формула для подсчета среднего: (a + b) / 2
|
|
// формула для подсчета среднего: (a + b) / 2
|
|
|
|
|
|
function avg2(a, b) {
|
|
function avg2(a, b) {
|
|
- a = prompt('Enter the first number:');
|
|
|
|
- b = prompt('Enter the second number:');
|
|
|
|
- return alert((a + b) / 2);
|
|
|
|
|
|
+ return ((a + b) / 2);
|
|
};
|
|
};
|
|
|
|
|
|
// sum3
|
|
// sum3
|
|
@@ -35,11 +31,8 @@ function avg2(a, b) {
|
|
|
|
|
|
// Обратите внимание, что sum3 от двух параметров тоже работает корректно.
|
|
// Обратите внимание, что sum3 от двух параметров тоже работает корректно.
|
|
|
|
|
|
-function sum3(a, b, c) {
|
|
|
|
- a = prompt('Enter the first number:') || 0;
|
|
|
|
- b = prompt('Enter the second number:') || 0;
|
|
|
|
- c = prompt('Enter the third number:') || 0;
|
|
|
|
- return alert(a + b + c);
|
|
|
|
|
|
+function sum3(a = 0, b = 0, c = 0) {
|
|
|
|
+ return (a + b + c);
|
|
};
|
|
};
|
|
|
|
|
|
// intRandom
|
|
// intRandom
|
|
@@ -52,10 +45,8 @@ function sum3(a, b, c) {
|
|
// Используйте умножение для расширения значения встроенной функции Math.random c диапозона 1,
|
|
// Используйте умножение для расширения значения встроенной функции Math.random c диапозона 1,
|
|
// сложениe для смещения результата на первый параметр, и Math.round для округления результата
|
|
// сложениe для смещения результата на первый параметр, и Math.round для округления результата
|
|
|
|
|
|
-function intRandom(m, n) {
|
|
|
|
- m = +prompt('Enter the first number in diapason:') || 0;
|
|
|
|
- n = +prompt('Enter the second number in diapason:');
|
|
|
|
- return alert(Math.round(Math.random() * (m - n) + n));
|
|
|
|
|
|
+function intRandom(n, m = 0) {
|
|
|
|
+ return (Math.round(Math.random() * (m - n) + n));
|
|
};
|
|
};
|
|
|
|
|
|
// greetAll
|
|
// greetAll
|
|
@@ -64,10 +55,10 @@ function intRandom(m, n) {
|
|
// Вам поможет arguments и for
|
|
// Вам поможет arguments и for
|
|
|
|
|
|
function greetAll(a) {
|
|
function greetAll(a) {
|
|
- for (var i = 1; i < arguments.length; i++) {
|
|
|
|
|
|
+ for (i = 1; i < arguments.length; i++) {
|
|
a += ', ' + arguments[i];
|
|
a += ', ' + arguments[i];
|
|
}
|
|
}
|
|
- return alert('Hello, ' + a);
|
|
|
|
|
|
+ return 'Hello, ' + a;
|
|
};
|
|
};
|
|
|
|
|
|
// sum
|
|
// sum
|
|
@@ -76,10 +67,10 @@ function greetAll(a) {
|
|
// Используйте псевдомассив arguments для получения всех параметров, и for для итерирования по нему
|
|
// Используйте псевдомассив arguments для получения всех параметров, и for для итерирования по нему
|
|
|
|
|
|
function sum(a = 0) {
|
|
function sum(a = 0) {
|
|
- for (var i = 1; i < arguments.length; i++) {
|
|
|
|
|
|
+ for (i = 1; i < arguments.length; i++) {
|
|
a += arguments[i];
|
|
a += arguments[i];
|
|
}
|
|
}
|
|
- return alert(a);
|
|
|
|
|
|
+ return a;
|
|
};
|
|
};
|
|
|
|
|
|
// Union
|
|
// Union
|
|
@@ -104,19 +95,19 @@ function sum(a = 0) {
|
|
|
|
|
|
var sample = prompt("Введите название задания");
|
|
var sample = prompt("Введите название задания");
|
|
switch (sample.toLowerCase()) {
|
|
switch (sample.toLowerCase()) {
|
|
- case "a": a();
|
|
|
|
|
|
+ case "a": a('hi!');
|
|
break;
|
|
break;
|
|
- case "cube": cube();
|
|
|
|
|
|
+ case "cube": cube(5);
|
|
break;
|
|
break;
|
|
- case 'avg2': avg2();
|
|
|
|
|
|
+ case 'avg2': avg2(3, 14);
|
|
break;
|
|
break;
|
|
- case 'sum3': sum3();
|
|
|
|
|
|
+ case 'sum3': sum3(1, 2, 3);
|
|
break;
|
|
break;
|
|
- case 'intrandom': intRandom();
|
|
|
|
|
|
+ case 'intrandom': intRandom(1, 10);
|
|
break;
|
|
break;
|
|
case 'greetall': greetAll('Billy', 'Willy', 'Dilly');
|
|
case 'greetall': greetAll('Billy', 'Willy', 'Dilly');
|
|
break;
|
|
break;
|
|
- case 'sum': sum(3,14,15,92);
|
|
|
|
|
|
+ case 'sum': sum(3, 14, 15, 92);
|
|
break;
|
|
break;
|
|
default: alert('Something bad has happened, please try again :(');
|
|
default: alert('Something bad has happened, please try again :(');
|
|
break;
|
|
break;
|
|
@@ -128,39 +119,38 @@ switch (sample.toLowerCase()) {
|
|
|
|
|
|
var list = {
|
|
var list = {
|
|
'lista': function listA(text) {
|
|
'lista': function listA(text) {
|
|
- text = prompt('Enter some text:');
|
|
|
|
- return alert(text);
|
|
|
|
|
|
+ return text;
|
|
},
|
|
},
|
|
'listcube': function listCube(num) {
|
|
'listcube': function listCube(num) {
|
|
- num = prompt('Enter the number, you will get the cube:');
|
|
|
|
- return alert(Math.pow(num, 3));
|
|
|
|
|
|
+ return num * num * num;
|
|
},
|
|
},
|
|
'listavg2': function listAvg2(a, b) {
|
|
'listavg2': function listAvg2(a, b) {
|
|
- a = prompt('Enter the first number:');
|
|
|
|
- b = prompt('Enter the second number:');
|
|
|
|
- return alert((a + b) / 2);
|
|
|
|
|
|
+ return ((a + b) / 2);
|
|
},
|
|
},
|
|
- 'listsum3': function listSum3(a, b, c) {
|
|
|
|
- a = prompt('Enter the first number:') || 0;
|
|
|
|
- b = prompt('Enter the second number:') || 0;
|
|
|
|
- c = prompt('Enter the third number:') || 0;
|
|
|
|
- return alert(a + b + c);
|
|
|
|
|
|
+ 'listsum3': function listSum3(a = 0, b = 0, c = 0) {
|
|
|
|
+ return (a + b + c);
|
|
},
|
|
},
|
|
- 'listintrandom': function listIntRandom(m, n) {
|
|
|
|
- m = +prompt('Enter the first number in diapason:') || 0;
|
|
|
|
- n = +prompt('Enter the second number in diapason:');
|
|
|
|
- return alert(Math.round(Math.random() * (m - n) + n));
|
|
|
|
|
|
+ 'listintrandom': function listIntRandom(n, m = 0) {
|
|
|
|
+ return Math.round(Math.random() * (m - n) + n);
|
|
},
|
|
},
|
|
'listgreetall': function listGreetAll(a) {
|
|
'listgreetall': function listGreetAll(a) {
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
a += ', ' + arguments[i];
|
|
a += ', ' + arguments[i];
|
|
}
|
|
}
|
|
- return alert('Hello, ' + a);
|
|
|
|
|
|
+ return ('Hello, ' + a);
|
|
},
|
|
},
|
|
'listsum': function sum(a = 0) {
|
|
'listsum': function sum(a = 0) {
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
a += arguments[i];
|
|
a += arguments[i];
|
|
}
|
|
}
|
|
- return alert(a);
|
|
|
|
|
|
+ return a;
|
|
}
|
|
}
|
|
-};
|
|
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+list.lista('hi');
|
|
|
|
+list.listcube(4);
|
|
|
|
+list.listavg2(3, 14);
|
|
|
|
+list.listsum3(1, 2, 3);
|
|
|
|
+list.listintrandom(1, 10);
|
|
|
|
+list.listgreetall('Billy', 'Willy', 'Dilly');
|
|
|
|
+list.listsum(3, 14, 15, 92);
|