Vitalii Polishchuk il y a 3 ans
Parent
commit
374cf8ca83
2 fichiers modifiés avec 118 ajouts et 0 suppressions
  1. 16 0
      js/06-js-functions-scopes/index.html
  2. 102 0
      js/06-js-functions-scopes/js/main.js

+ 16 - 0
js/06-js-functions-scopes/index.html

@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>HW</title>
+</head>
+
+<body>
+
+    <script src="js/main.js"></script>
+</body>
+
+</html>

+ 102 - 0
js/06-js-functions-scopes/js/main.js

@@ -0,0 +1,102 @@
+//a
+function a(msg) {
+    alert(msg)
+}
+
+//cube
+function cube(number) {
+    let cube = number * number * number;
+    return cube;
+}
+
+//avg2
+function avg2(a, b) {
+    let avg2 = (a + b) / 2;
+    return avg2;
+}
+
+//sum3
+function sum3(a = 0, b = 0, c = 0) {
+    let sum = a + b + c;
+    return sum;
+}
+
+//intRandom
+function intRandom(a = 0, b = 0) {
+    return Math.round(Math.random() * (b - a)) + a;
+}
+
+//greetAll
+function greetAll() {
+    for (let i in arguments) {
+        alert(arguments[i])
+    }
+}
+
+//sum
+function sum() {
+    let sum = 0;
+    for (let i of arguments) {
+        sum += i;
+    }
+    return sum;
+}
+
+//Union
+function aSample() {
+    a("Привет!") // вызывает alert("Привет!")
+}
+
+function cubeSample() {
+    cube(5) // => 125
+}
+
+function avg2Sample() {
+    avg2(10, 5) // возвращает 7.5
+}
+
+function sum3Sample() {
+    sum3(5, 10, 100500) // => 100515
+}
+
+function intRandomSample() {
+    intRandom(2, 15) // возвращает целое случайное число от 2 до 15 (включительно)
+}
+
+function greetAllSample() {
+    greetAll("Superman", "SpiderMan", "Captain Obvious") // выводит alert "Hello Superman, SpiderMan, Captain Obvious"
+}
+
+function sumSample() {
+    sum(10, 20, 40, 100) // => 170
+}
+
+let sample = prompt("Введите название задания");
+
+switch (sample.toLowerCase()) {
+    case "a": aSample();
+        break;
+    case "cube": cubeSample();
+        break;
+    case "avg2": avg2Sample();
+        break;
+    case "sum3": sum3Sample();
+        break;
+    case "intRandom": intRandomSample();
+        break;
+    case "greetAll": greetAllSample();
+        break;
+    case "sum": sumSample();
+        break;
+}
+
+//Union declarative
+let hometask = {
+    a: () => a("Привет!"),
+    cube: () => cube(5),
+    avg2: () => avg2(10, 5),
+    sum3: () => sum3(5, 10, 100500),
+    intRandom: () => intRandom(2, 15),
+    greetAll: () => greetAll("Superman", "SpiderMan", "Captain Obvious"),
+    sum: () => sum(10, 20, 40, 100)
+}