From 4520013d21e8ae6c847979bbb86bb4e79993d8ea Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 27 May 2022 06:25:26 +0300 Subject: [PATCH 1/4] Homework for lection #9, all functions in one commit --- .gitignore | 1 + babel.config.js | 2 +- package.json | 2 +- src/css/style.css | 54 +++++++++++++++ src/index.html | 32 +++++++++ src/script/const.js | 12 ++++ src/script/homework_1.js | 52 ++++++++++++++ src/script/homework_1.test.js | 71 +++++++++++++++++++ src/script/homework_10.js | 39 +++++++++++ src/script/homework_10.test.js | 59 ++++++++++++++++ src/script/homework_2.js | 57 +++++++++++++++ src/script/homework_2.test.js | 85 +++++++++++++++++++++++ src/script/homework_3.js | 41 +++++++++++ src/script/homework_3.test.js | 68 ++++++++++++++++++ src/script/homework_4.js | 41 +++++++++++ src/script/homework_4.test.js | 122 +++++++++++++++++++++++++++++++++ src/script/homework_5.js | 52 ++++++++++++++ src/script/homework_5.test.js | 79 +++++++++++++++++++++ src/script/homework_6.js | 43 ++++++++++++ src/script/homework_6.test.js | 92 +++++++++++++++++++++++++ src/script/homework_7.js | 59 ++++++++++++++++ src/script/homework_7.test.js | 67 ++++++++++++++++++ src/script/homework_8.js | 52 ++++++++++++++ src/script/homework_8.test.js | 83 ++++++++++++++++++++++ src/script/homework_9.js | 71 +++++++++++++++++++ src/script/homework_9.test.js | 79 +++++++++++++++++++++ src/script/ui.js | 99 ++++++++++++++++++++++++++ src/script/ui.test.js | 84 +++++++++++++++++++++++ 28 files changed, 1596 insertions(+), 2 deletions(-) mode change 100644 => 100755 package.json create mode 100644 src/css/style.css create mode 100644 src/index.html create mode 100644 src/script/const.js create mode 100644 src/script/homework_1.js create mode 100644 src/script/homework_1.test.js create mode 100644 src/script/homework_10.js create mode 100644 src/script/homework_10.test.js create mode 100644 src/script/homework_2.js create mode 100644 src/script/homework_2.test.js create mode 100644 src/script/homework_3.js create mode 100644 src/script/homework_3.test.js create mode 100644 src/script/homework_4.js create mode 100644 src/script/homework_4.test.js create mode 100644 src/script/homework_5.js create mode 100644 src/script/homework_5.test.js create mode 100644 src/script/homework_6.js create mode 100644 src/script/homework_6.test.js create mode 100644 src/script/homework_7.js create mode 100644 src/script/homework_7.test.js create mode 100644 src/script/homework_8.js create mode 100644 src/script/homework_8.test.js create mode 100644 src/script/homework_9.js create mode 100644 src/script/homework_9.test.js create mode 100644 src/script/ui.js create mode 100644 src/script/ui.test.js diff --git a/.gitignore b/.gitignore index 3c3629e..ba2a97b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +coverage diff --git a/babel.config.js b/babel.config.js index 24d5e76..8c5a7ae 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,3 +1,3 @@ module.exports = { - presets: [["@babel/preset-env", { targets: { node: "current" } }]], + presets: [["@babel/preset-env", { targets: { node: "current" } }]], }; diff --git a/package.json b/package.json old mode 100644 new mode 100755 index a2bc26f..659da90 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "otus JS-Basic learning account", "main": "index.js", "scripts": { - "test": "jest", + "test": "jest --silent=false", "lint": "prettier --check . && eslint .", "lint-fix": "prettier --write . && eslint . --fix", "prepare": "husky install" diff --git a/src/css/style.css b/src/css/style.css new file mode 100644 index 0000000..bfc9b47 --- /dev/null +++ b/src/css/style.css @@ -0,0 +1,54 @@ +:root { + font-size: 16px; +} + +* { + box-sizing: border-box; + margin: 0; + padding: 0; +} + +html, +body { + background-color: white; + color: black; + line-height: 1.2; +} + +.cbxDiv { + position: relative; + left: 0; + right: 0; + margin: auto; + display: block; + text-align: center; +} + +.infoDiv { + height: 600px; + border: solid 1px black; + width: 50%; + display: block; + float: left; + padding: 5px; +} + +.btns { + width: 100px; + height: 25px; + display: none; + left: 0; + right: 0; + margin: auto; + margin-top: 5px; +} + +.edits { + width: 100px; + height: 25px; + display: block; + left: 0; + right: 0; + margin: auto; + margin-top: 5px; +} diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..eafc434 --- /dev/null +++ b/src/index.html @@ -0,0 +1,32 @@ + + + + + + Homework for lection #9 + + + + + + + + + + + + + + + + + + + diff --git a/src/script/const.js b/src/script/const.js new file mode 100644 index 0000000..372af0b --- /dev/null +++ b/src/script/const.js @@ -0,0 +1,12 @@ +export const MAIN_CONTAINER_ID = "mainContainer"; +export const CHECKBOX_TEXT = " Output to fake console"; +export const CHECKBOX_CLASS = "cbxDiv"; +export const CHECKBOX_ID = "cbxConsole"; +export const TASKS_CONTAINER_ID = "divTasksList"; +export const INFO_DIVS_CONTAINER_CLASS = "infoDiv"; +export const CONSOLE_CONTAINER_ID = "divConsoleText"; +export const TASK7_MAIN_CONTAINER_ID = "task7id"; +export const TASK7_BTN_CLASS_NAME = "btns"; +export const TASK7_EDIT_CLASS_NAME = "edits"; + +export const functions = []; diff --git a/src/script/homework_1.js b/src/script/homework_1.js new file mode 100644 index 0000000..4aa1376 --- /dev/null +++ b/src/script/homework_1.js @@ -0,0 +1,52 @@ +import { functions } from "./const.js"; + +export let hw1_f1 = (a, b) => { + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); + return [a * b, a + b]; +}; + +export let hw1_f2 = (str1, str2) => { + if (!str1) { + str1 = ""; + } + if (!str2) { + str2 = ""; + } + return [str1.length + str2.length]; +}; + +export let hw1_f3 = (str) => { + if (!str) { + str = "000"; + } + + let a = isNaN(Number(str[0])) ? 0 : Number(str[0]); + let b = isNaN(Number(str[1])) ? 0 : Number(str[1]); + let c = isNaN(Number(str[2])) ? 0 : Number(str[2]); + + return [a + b + c]; +}; + +functions.push({ + name: "Homework 1 task #1", + func: hw1_f1, + params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], +}); +functions.push({ + name: "Homework 1 task #2", + func: hw1_f2, + params: [ + Math.random() + .toFixed(Math.random() * 10) + .toString(), + Math.random() + .toFixed(Math.random() * 10) + .toString(), + ], +}); +functions.push({ + name: "Homework 1 task #3", + func: hw1_f3, + params: [Math.random().toFixed(3).slice(2).toString()], +}); diff --git a/src/script/homework_1.test.js b/src/script/homework_1.test.js new file mode 100644 index 0000000..ed48b1c --- /dev/null +++ b/src/script/homework_1.test.js @@ -0,0 +1,71 @@ +import { hw1_f1, hw1_f2, hw1_f3 } from "./homework_1.js"; + +describe("homework 1, function 1 test", () => { + it("is function exists", () => { + expect(hw1_f1).toBeDefined(); + }); + + it("param 5 and 4", () => { + expect(hw1_f1(5, 4)).toEqual([20, 9]); + }); + it("param 9 and 9", () => { + expect(hw1_f1(9, 9)).toEqual([81, 18]); + }); + it("param null and null", () => { + expect(hw1_f1(null, null)).toEqual([0, 0]); + }); + it("param undefined and undefined", () => { + expect(hw1_f1(undefined, undefined)).toEqual([0, 0]); + }); + it("param empty string and empty string", () => { + expect(hw1_f1("", "")).toEqual([0, 0]); + }); +}); + +describe("homework 1, function 2 test", () => { + it("is function exists", () => { + expect(hw1_f2).toBeDefined(); + }); + + it("param '123' and '1234", () => { + expect(hw1_f2("123", "1234")).toEqual([7]); + }); + + it("param null and null", () => { + expect(hw1_f2(null, null)).toEqual([0]); + }); + + it("param undefined and undefined", () => { + expect(hw1_f2(undefined, undefined)).toEqual([0]); + }); + + it("param empty string and empty string", () => { + expect(hw1_f2("", "")).toEqual([0]); + }); +}); + +describe("homework 1, function 3 test", () => { + it("is function exists", () => { + expect(hw1_f3).toBeDefined(); + }); + + it("param '123'", () => { + expect(hw1_f3("123")).toEqual([6]); + }); + + it("param '1234'", () => { + expect(hw1_f3("1234")).toEqual([6]); + }); + + it("param null", () => { + expect(hw1_f3(null)).toEqual([0]); + }); + + it("param undefined", () => { + expect(hw1_f3(undefined)).toEqual([0]); + }); + + it("param empty string and empty string", () => { + expect(hw1_f3("")).toEqual([0]); + }); +}); diff --git a/src/script/homework_10.js b/src/script/homework_10.js new file mode 100644 index 0000000..50a885f --- /dev/null +++ b/src/script/homework_10.js @@ -0,0 +1,39 @@ +import { functions } from "./const.js"; + +export let hw10_f1 = (str) => { + if ( + /^[a-zA-Z]{1}[a-zA-Z0-9_-]+[a-zA-Z0-9]{1}@[a-zA-Z0-9]{1}[a-zA-Z0-9-]+[a-zA-Z0-9]{1}\.[a-zA-Z]{2,}$/.test( + str + ) + ) { + return ["mail"]; + } + + if (/^(((\+7)|8))[89][0-9]{9}$/.test(str)) { + return ["phone number"]; + } + + if (/^([0-9]{1,2}\.[0-9]{1,2}\.)([0-9]{4}|[0-9]{2})$/.test(str)) { + return ["date"]; + } + + return ["not correct date or phone number or mail"]; +}; + +functions.push({ + name: "Homework 10 task #1", + func: hw10_f1, + params: [ + [ + "mail@mail.ru", + "89009998877", + "11.12.2012", + "mail@mail", + "8900999887", + "+79009998877", + "+78009998877", + "11.12.213", + "11.12.13", + ][(Math.random() * 9) >>> 0], + ], +}); diff --git a/src/script/homework_10.test.js b/src/script/homework_10.test.js new file mode 100644 index 0000000..a70c19a --- /dev/null +++ b/src/script/homework_10.test.js @@ -0,0 +1,59 @@ +import { hw10_f1 } from "./homework_10.js"; + +describe("homework 10, function 1 test", () => { + it("is function exists", () => { + expect(hw10_f1).toBeDefined(); + }); + + it("param 22.10.10", () => { + expect(hw10_f1("22.10.10")).toEqual(["date"]); + }); + + it("param 22.10.1910", () => { + expect(hw10_f1("22.10.1910")).toEqual(["date"]); + }); + + it("param aaa99@bbb-9.cc", () => { + expect(hw10_f1("aaa99@bbb-9.cc")).toEqual(["mail"]); + }); + + it("param aaa99.bbb-.cc", () => { + expect(hw10_f1("aaa99.bbb-9.cc").join("").split(" ")).toContain("not"); + }); + + it("param 22.10.123", () => { + expect(hw10_f1("22.10.123").join("").split(" ")).toContain("not"); + }); + + it("param aaa_2@bbb.cc", () => { + expect(hw10_f1("aaa_2@bbb.cc")).toEqual(["mail"]); + }); + + it("param aaa_2@bbb.cc", () => { + expect(hw10_f1("aaa_@bbb.cc").join("").split(" ")).toContain("not"); + }); + + it("param +69009998877", () => { + expect(hw10_f1("+69009998877").join("").split(" ")).toContain("not"); + }); + + it("param +79009998877", () => { + expect(hw10_f1("+79009998877")).toEqual(["phone number"]); + }); + + it("param 89009998877", () => { + expect(hw10_f1("89009998877")).toEqual(["phone number"]); + }); + + it("param 88009998877", () => { + expect(hw10_f1("88009998877")).toEqual(["phone number"]); + }); + + it("param 78009998877", () => { + expect(hw10_f1("78009998877").join("").split(" ")).toContain("not"); + }); + + it("param 87009998877", () => { + expect(hw10_f1("87009998877").join("").split(" ")).toContain("not"); + }); +}); diff --git a/src/script/homework_2.js b/src/script/homework_2.js new file mode 100644 index 0000000..8faa396 --- /dev/null +++ b/src/script/homework_2.js @@ -0,0 +1,57 @@ +import { functions } from "./const.js"; + +export let hw2_f1 = (a, b) => { + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); + + return [Math.max(a, b)]; +}; + +export let hw2_f2 = () => { + let m = prompt("Please enter the number of month (1..12)"); + m = isNaN(Number(m)) ? 1 : Number(m); + if (m > 12) { + m = 12; + } + if (m < 1) { + m = 1; + } + let months = [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec", + ]; + return [months[m - 1]]; +}; + +export let hw2_f3 = (circle, square) => { + square = isNaN(Number(square)) ? 0 : Number(square); + circle = isNaN(Number(circle)) ? 0 : Number(circle); + + let r = circle / (Math.PI * 2); + return r <= Math.sqrt(square) / 2 ? ["fitted"] : ["not fitted"]; +}; + +functions.push({ + name: "Homework 2 task #1", + func: hw2_f1, + params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], +}); +functions.push({ name: "Homework 2 task #2", func: hw2_f2, params: [] }); +functions.push({ + name: "Homework 2 task #3", + func: hw2_f3, + params: [ + [18.85, 18][(Math.random() * 2) >>> 0], + [36.1, 25][(Math.random() * 2) >> 0], + ], +}); diff --git a/src/script/homework_2.test.js b/src/script/homework_2.test.js new file mode 100644 index 0000000..2ea72be --- /dev/null +++ b/src/script/homework_2.test.js @@ -0,0 +1,85 @@ +import { hw2_f1, hw2_f2, hw2_f3 } from "./homework_2.js"; + +describe("homework 2, function 1 test", () => { + it("is function exists", () => { + expect(hw2_f1).toBeDefined(); + }); + + it("param 5 and 4", () => { + expect(hw2_f1(5, 4)).toEqual([5]); + }); + + it("param 7 and 8", () => { + expect(hw2_f1(7, 8)).toEqual([8]); + }); + + it("param null and null", () => { + expect(hw2_f1(null, null)).toEqual([0]); + }); + it("param undefined and undefined", () => { + expect(hw2_f1(undefined, undefined)).toEqual([0]); + }); + it("param empty string and empty string", () => { + expect(hw2_f1("", "")).toEqual([0]); + }); +}); + +describe("homework 2, function 2 test", () => { + it("is function exists", () => { + expect(hw2_f2).toBeDefined(); + }); + + it("prompted 1", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "1"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted 6", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "6"); + expect(hw2_f2()).toEqual(["Jun"]); + }); + + it("prompted 12", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "12"); + expect(hw2_f2()).toEqual(["Dec"]); + }); + + it("prompted 13", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "13"); + expect(hw2_f2()).toEqual(["Dec"]); + }); + + it("prompted 0", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "0"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted null", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "null"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted undefined", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted empty string", () => { + jest.spyOn(window, "prompt").mockImplementation(() => ""); + expect(hw2_f2()).toEqual(["Jan"]); + }); +}); + +describe("homework 2, function 3 test", () => { + it("is function exists", () => { + expect(hw2_f3).toBeDefined(); + }); + + it("param 16 36", () => { + expect(hw2_f3(16, 36)).toEqual(["fitted"]); + }); + + it("param 36 16", () => { + expect(hw2_f3(36, 16)).toEqual(["not fitted"]); + }); +}); diff --git a/src/script/homework_3.js b/src/script/homework_3.js new file mode 100644 index 0000000..a5358a1 --- /dev/null +++ b/src/script/homework_3.js @@ -0,0 +1,41 @@ +import { functions } from "./const.js"; + +export let hw3_f1 = () => { + let sum = 0; + for (let i = 50; i <= 100; i++) { + sum += i; + } + return [sum]; +}; + +export let hw3_f2 = () => { + let result = []; + for (let i = 1; i < 10; i++) { + result.push("
"); + result.push(`7 x ${i} = ${7 * i}`); + } + return result; +}; + +export let hw3_f3 = () => { + let n = prompt("Please enter the number N"); + n = isNaN(Number(n)) ? 0 : Number(n); + + if (n < 1) { + return [0]; + } + + let sum = 0; + let cnt = 0; + for (let i = 1; i <= n; i++) { + if (i & 0x01) { + cnt++; + sum += i; + } + } + return [sum / cnt]; +}; + +functions.push({ name: "Homework 3 task #1", func: hw3_f1, params: [] }); +functions.push({ name: "Homework 3 task #2", func: hw3_f2, params: [] }); +functions.push({ name: "Homework 3 task #3", func: hw3_f3, params: [] }); diff --git a/src/script/homework_3.test.js b/src/script/homework_3.test.js new file mode 100644 index 0000000..ca2c356 --- /dev/null +++ b/src/script/homework_3.test.js @@ -0,0 +1,68 @@ +import { hw3_f1, hw3_f2, hw3_f3 } from "./homework_3.js"; + +describe("homework 3, function 1 test", () => { + it("is function exists", () => { + expect(hw3_f1).toBeDefined(); + }); + + it("check sum", () => { + let sum = 0; + for (let i = 100; i >= 50; i--) { + sum += i; + } + expect(hw3_f1()).toEqual([sum]); + }); +}); + +describe("homework 3, function 2 test", () => { + it("is function exists", () => { + expect(hw3_f2).toBeDefined(); + }); + + it("checking seven multiple table", () => { + let check = []; + for (let i = 1; i < 10; i++) { + check.push(i * 7); + } + let rslt = hw3_f2() + .join("") + .split("
") + .map((el) => { + return Number(el.split("=").pop().trim()); + }) + .slice(1); + expect(check).toEqual(rslt); + }); +}); + +describe("homework 3, function 3 test", () => { + it("prompted 3", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "3"); + expect(hw3_f3()).toEqual([2]); + }); + + it("prompted 9", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "9"); + expect(hw3_f3()).toEqual([5]); + }); + + it("prompted 0", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "0"); + expect(hw3_f3()).toEqual([0]); + }); + + it("prompted null", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "null"); + expect(hw3_f3()).toEqual([0]); + }); + + it("prompted undefined", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); + expect(hw3_f3()).toEqual([0]); + }); + + it("prompted empty string", () => { + jest.spyOn(window, "prompt").mockImplementation(() => ""); + expect(hw3_f3()).toEqual([0]); + }); +}); diff --git a/src/script/homework_4.js b/src/script/homework_4.js new file mode 100644 index 0000000..e584761 --- /dev/null +++ b/src/script/homework_4.js @@ -0,0 +1,41 @@ +import { functions } from "./const.js"; + +let user = {}; +let admin = {}; + +export let hw4_f1 = () => { + let n = prompt("Please enter the age of user"); + n = isNaN(Number(n)) ? 0 : Number(n); + user.name = "John"; + user.age = n; + + let rslt = []; + rslt.push(user); + rslt.push("
"); + rslt.push(`user.name = ${user.name}, user.age = ${user.age}`); + return rslt; +}; + +export let hw4_f2 = () => { + admin = Object.assign({}, user); + admin.role = "admin"; + + let rslt = []; + rslt.push(admin); + rslt.push("
"); + rslt.push( + `admin.name = ${user.name}, admin.age = ${user.age}, admin.role = ${admin.role}` + ); + return rslt; +}; + +export let hw4_f3 = () => { + for (let prop in admin) { + window[prop] = admin[prop]; + } + return ["Variables are created in Window object"]; +}; + +functions.push({ name: "Homework 4 task #1", func: hw4_f1, params: [] }); +functions.push({ name: "Homework 4 task #2", func: hw4_f2, params: [] }); +functions.push({ name: "Homework 4 task #3", func: hw4_f3, params: [] }); diff --git a/src/script/homework_4.test.js b/src/script/homework_4.test.js new file mode 100644 index 0000000..3582c50 --- /dev/null +++ b/src/script/homework_4.test.js @@ -0,0 +1,122 @@ +import { hw4_f1, hw4_f2, hw4_f3 } from "./homework_4.js"; + +describe("homework 4, function 1 test", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "23"); + + it("is function exists", () => { + expect(hw4_f1).toBeDefined(); + }); + + it("check user object", () => { + expect(typeof hw4_f1()[0]).toEqual("object"); + }); + + it("check user name exists", () => { + expect(hw4_f1()[0].name).toBeDefined(); + }); + + it("check user name value", () => { + expect(hw4_f1()[0].name).toEqual("John"); + }); + + it("check user age exists", () => { + expect(hw4_f1()[0].age).toBeDefined(); + }); + + it("check user age value", () => { + expect(hw4_f1()[0].age).toEqual(23); + }); + + it("check user age modified", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "21"); + expect(hw4_f1()[0].age).toEqual(21); + }); + + it("check user age null prompted", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "null"); + expect(hw4_f1()[0].age).toEqual(0); + }); + + it("check user age undefined prompted", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); + expect(hw4_f1()[0].age).toEqual(0); + }); + + it("check user age empty string prompted", () => { + jest.spyOn(window, "prompt").mockImplementation(() => ""); + expect(hw4_f1()[0].age).toEqual(0); + }); +}); + +describe("homework 3, function 2 test", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "23"); + let user = hw4_f1()[0]; + + it("is function exists", () => { + expect(hw4_f2).toBeDefined(); + }); + + it("check admin object", () => { + expect(typeof hw4_f2()[0]).toEqual("object"); + }); + + it("check admin name exists", () => { + expect(hw4_f2()[0].name).toBeDefined(); + }); + + it("check that admin name is equal to user name", () => { + expect(hw4_f2()[0].name).toEqual(user.name); + }); + + it("check admin age exists", () => { + expect(hw4_f2()[0].age).toBeDefined(); + }); + + it("check admin age value", () => { + expect(hw4_f2()[0].age).toEqual(user.age); + }); + + it("check admin role exists", () => { + expect(hw4_f2()[0].role).toBeDefined(); + }); + + it("check admin role value", () => { + expect(hw4_f2()[0].role).toEqual("admin"); + }); +}); + +describe("homework 3, function 3 test", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "23"); + + let admin = hw4_f2()[0]; + + it("is function exists", () => { + expect(hw4_f3).toBeDefined(); + }); + + hw4_f3(); + + it("check variable age exists", () => { + expect(window.age).toBeDefined(); + }); + + it("check variable age value", () => { + expect(window.age).toEqual(admin.age); + }); + + it("check variable name exists", () => { + expect(window.name).toBeDefined(); + }); + + it("check variable name value", () => { + expect(window.name).toEqual(admin.name); + }); + + it("check variable role exists", () => { + expect(window.role).toBeDefined(); + }); + + it("check variable role value", () => { + expect(window.role).toEqual(admin.role); + }); +}); diff --git a/src/script/homework_5.js b/src/script/homework_5.js new file mode 100644 index 0000000..36a78da --- /dev/null +++ b/src/script/homework_5.js @@ -0,0 +1,52 @@ +import { functions } from "./const.js"; + +export let hw5_f1 = (a) => { + let sum = a.reduce((prev, curr) => { + return prev + (isNaN(Number(curr)) ? 0 : Number(curr)); + }, 0); + return [sum]; +}; + +export let hw5_f2 = (a) => { + let rslt = a.map((el) => { + return (isNaN(Number(el)) ? 0 : Number(el)) * 2; + }); + return rslt; +}; + +export let hw5_f3 = (a) => { + a = a.map((el) => { + return isNaN(Number(el)) ? 0 : Number(el); + }); + let rslt = [Math.max(...a)]; + rslt.push(Math.min(...a)); + return rslt; +}; + +functions.push({ + name: "Homework 5 task #1", + func: hw5_f1, + params: [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { + return (Math.random() * 100).toFixed(); + }), + ], +}); +functions.push({ + name: "Homework 5 task #2", + func: hw5_f2, + params: [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { + return (Math.random() * 10).toFixed(); + }), + ], +}); +functions.push({ + name: "Homework 5 task #3", + func: hw5_f3, + params: [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { + return (Math.random() * 100).toFixed(); + }), + ], +}); diff --git a/src/script/homework_5.test.js b/src/script/homework_5.test.js new file mode 100644 index 0000000..8dc441d --- /dev/null +++ b/src/script/homework_5.test.js @@ -0,0 +1,79 @@ +import { hw5_f1, hw5_f2, hw5_f3 } from "./homework_5.js"; + +describe("homework 5, function 1 test", () => { + it("is function exists", () => { + expect(hw5_f1).toBeDefined(); + }); + + it("check array elements summary", () => { + expect(hw5_f1([1, 2, 3])[0]).toEqual(6); + }); + + it("check wrong array paramater (null)", () => { + expect(hw5_f1([null, null, null])[0]).toEqual(0); + }); + + it("check wrong array paramater (undefined)", () => { + expect(hw5_f1([undefined, undefined, undefined])[0]).toEqual(0); + }); + + it("check wrong array paramater (empty strings)", () => { + expect(hw5_f1(["", "", ""])[0]).toEqual(0); + }); + + it("check wrong array paramater (NaN)", () => { + expect(hw5_f1([NaN, NaN, NaN])[0]).toEqual(0); + }); +}); + +describe("homework 3, function 2 test", () => { + it("is function exists", () => { + expect(hw5_f2).toBeDefined(); + }); + + it("check new array generation", () => { + expect(hw5_f2([1, 2, 3])).toEqual([2, 4, 6]); + }); + + it("check wrong array paramater (null)", () => { + expect(hw5_f2([null, null, null])).toEqual([0, 0, 0]); + }); + + it("check wrong array paramater (undefined)", () => { + expect(hw5_f2([undefined, undefined, undefined])).toEqual([0, 0, 0]); + }); + + it("check wrong array paramater (empty strings)", () => { + expect(hw5_f2(["", "", ""])).toEqual([0, 0, 0]); + }); + + it("check wrong array paramater (NaN)", () => { + expect(hw5_f2([NaN, NaN, NaN])).toEqual([0, 0, 0]); + }); +}); + +describe("homework 3, function 3 test", () => { + it("is function exists", () => { + expect(hw5_f3).toBeDefined(); + }); + + it("check array elements summary", () => { + expect(hw5_f3([1, 2, 3])).toEqual([3, 1]); + }); + + it("check wrong array (null)", () => { + expect(hw5_f3([null, null, null])).toEqual([0, 0]); + }); + + it("check wrong array (undefined)", () => { + expect(hw5_f3([undefined, undefined])).toEqual([0, 0]); + }); + + it("check wrong array (empty strings)", () => { + expect(hw5_f3(["", ""])).toEqual([0, 0]); + }); + + it("check wrong array (NaN)", () => { + expect(hw5_f3([NaN, NaN])).toEqual([0, 0]); + }); +}); diff --git a/src/script/homework_6.js b/src/script/homework_6.js new file mode 100644 index 0000000..dedf90f --- /dev/null +++ b/src/script/homework_6.js @@ -0,0 +1,43 @@ +import { functions } from "./const.js"; + +export let hw6_f1 = (a, b) => { + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); + return [Math.abs(a - b)]; +}; + +export let hw6_f2 = (str) => { + str = String(str); + return [str.split(" ").length !== 1 ? "false" : "true"]; +}; + +export let hw6_f3 = (a, b) => { + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); + + return [Math.pow(a, b)]; +}; + +functions.push({ + name: "Homework 6 task #1", + func: hw6_f1, + params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], +}); +functions.push({ + name: "Homework 6 task #2", + func: hw6_f2, + params: [ + Math.random() + .toFixed(Math.random() * 10) + .toString() + + ["", " "][(Math.random() * 2) >>> 0] + + Math.random() + .toFixed(Math.random() * 10) + .toString(), + ], +}); +functions.push({ + name: "Homework 6 task #3", + func: hw6_f3, + params: [+(Math.random() * 10).toFixed(), +(Math.random() * 4).toFixed()], +}); diff --git a/src/script/homework_6.test.js b/src/script/homework_6.test.js new file mode 100644 index 0000000..db98bc1 --- /dev/null +++ b/src/script/homework_6.test.js @@ -0,0 +1,92 @@ +import { hw6_f1, hw6_f2, hw6_f3 } from "./homework_6.js"; + +describe("homework 6, function 1 test", () => { + it("is function exists", () => { + expect(hw6_f1).toBeDefined(); + }); + + it("param 5 and 4", () => { + expect(hw6_f1(51, 45)).toEqual([6]); + }); + + it("param 4 and 5", () => { + expect(hw6_f1(4, 5)).toEqual([1]); + }); + + it("param 9 and 9", () => { + expect(hw6_f1(9, 9)).toEqual([0]); + }); + it("param null and null", () => { + expect(hw6_f1(null, null)).toEqual([0]); + }); + it("param undefined and undefined", () => { + expect(hw6_f1(undefined, undefined)).toEqual([0]); + }); + it("param empty string and empty string", () => { + expect(hw6_f1("", "")).toEqual([0]); + }); + + it("param empty NaN and NaN", () => { + expect(hw6_f1(NaN, NaN)).toEqual([0]); + }); +}); + +describe("homework 6, function 2 test", () => { + it("is function exists", () => { + expect(hw6_f2).toBeDefined(); + }); + + it("param '123 1234", () => { + expect(hw6_f2("123 1234")).toEqual(["false"]); + }); + + it("param 123", () => { + expect(hw6_f2("123")).toEqual(["true"]); + }); + + it("param null", () => { + expect(hw6_f2(null)).toEqual(["true"]); + }); + + it("param undefined", () => { + expect(hw6_f2(undefined)).toEqual(["true"]); + }); + + it("param empty string", () => { + expect(hw6_f2("")).toEqual(["true"]); + }); + + it("param NaN", () => { + expect(hw6_f2(NaN)).toEqual(["true"]); + }); +}); + +describe("homework 6, function 3 test", () => { + it("is function exists", () => { + expect(hw6_f3).toBeDefined(); + }); + + it("param 3 and 3", () => { + expect(hw6_f3(3, 3)).toEqual([27]); + }); + + it("param 4 and 2", () => { + expect(hw6_f3(4, 2)).toEqual([16]); + }); + + it("param null and null", () => { + expect(hw6_f3(null, null)).toEqual([1]); + }); + + it("param undefined and undefined", () => { + expect(hw6_f3(undefined, undefined)).toEqual([1]); + }); + + it("param empty string and empty string", () => { + expect(hw6_f3("", "")).toEqual([1]); + }); + + it("param NaN and NaN", () => { + expect(hw6_f3(NaN, NaN)).toEqual([1]); + }); +}); diff --git a/src/script/homework_7.js b/src/script/homework_7.js new file mode 100644 index 0000000..65e63ed --- /dev/null +++ b/src/script/homework_7.js @@ -0,0 +1,59 @@ +import { + functions, + TASK7_BTN_CLASS_NAME, + TASK7_EDIT_CLASS_NAME, + TASK7_MAIN_CONTAINER_ID, +} from "./const.js"; + +export let hw7_f1 = (mainContainer) => { + let task7Div = document.getElementById(TASK7_MAIN_CONTAINER_ID); + if (task7Div) { + task7Div.innerHTML = ""; + } else { + task7Div = document.createElement("div"); + task7Div.id = TASK7_MAIN_CONTAINER_ID; + } + + let btn = document.createElement("input"); + + let edit = document.createElement("input"); + edit.type = "text"; + edit.className = TASK7_EDIT_CLASS_NAME; + edit.addEventListener("keyup", () => { + if (edit.value.length) { + btn.style["display"] = "block"; + } else { + btn.style["display"] = "none"; + } + }); + + btn.type = "button"; + btn.className = TASK7_BTN_CLASS_NAME; + btn.addEventListener("click", () => { + let p = document.createElement("p"); + p.innerHTML = edit.value; + task7Div.appendChild(p); + + let allP = document.querySelectorAll("p"); + if (allP.length > 5) { + task7Div.removeChild(allP[0]); + } + }); + + task7Div.appendChild(edit); + task7Div.appendChild(btn); + + for (let i = 0; i < 3; i++) { + let p = document.createElement("p"); + p.innerHTML = `Text for paragraph ${i}`; + task7Div.appendChild(p); + } + + mainContainer.appendChild(task7Div); + return ["All need elements are created"]; +}; +functions.push({ + name: "Homework 7 task #1", + func: hw7_f1, + params: [document.body], +}); diff --git a/src/script/homework_7.test.js b/src/script/homework_7.test.js new file mode 100644 index 0000000..4b1a313 --- /dev/null +++ b/src/script/homework_7.test.js @@ -0,0 +1,67 @@ +import { hw7_f1 } from "./homework_7.js"; + +describe("homework 7, function 1 test", () => { + it("is function exists", () => { + expect(hw7_f1).toBeDefined(); + }); + + document.body.innerHTML = "
"; + let container = document.getElementById("testDiv"); + + hw7_f1(container); + + it("check inputs creation", () => { + expect(container.querySelectorAll("input")).toHaveLength(2); + }); + + it("check paragraphs creation", () => { + expect(container.querySelectorAll("p")).toHaveLength(3); + }); + + it("check inputs types", () => { + expect(container.querySelector("input[type=button]")).toBeDefined(); + expect(container.querySelector("input[type=text]")).toBeDefined(); + }); + + it("check button set visible", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "1"; + edit.dispatchEvent(new Event("keyup")); + expect(btn.style["display"]).toEqual("block"); + }); + + it("check button set unvisible", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = ""; + edit.dispatchEvent(new Event("keyup")); + expect(btn.style["display"]).toEqual("none"); + }); + + it("check that button press adding paragraphs", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "1"; + btn.click(); + expect(container.querySelectorAll("p")).toHaveLength(4); + }); + + it("check that button press adding paragraphs with need text", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "2"; + btn.click(); + expect(container.querySelector("p:last-child").innerHTML).toEqual( + edit.value + ); + }); + + it("check that button press adding paragraphs but only five for maximum count", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "3"; + btn.click(); + expect(container.querySelectorAll("p")).toHaveLength(5); + }); +}); diff --git a/src/script/homework_8.js b/src/script/homework_8.js new file mode 100644 index 0000000..9850df5 --- /dev/null +++ b/src/script/homework_8.js @@ -0,0 +1,52 @@ +import { functions } from "./const.js"; + +export let hw8_f1 = (strDate) => { + let date = String(strDate).split(".").reverse(); + if (date.length !== 3) { + return ["Error date in input string"]; + } + date[1]--; + + let day = new Intl.DateTimeFormat("en-US", { weekday: "long" }).format( + new Date(...date) + ); + return [day]; +}; + +export let hw8_f2 = () => { + let date = new Date(); + return [date.getHours() * 60 + date.getMinutes()]; +}; + +export let hw8_f3 = (strDate1, strDate2) => { + let date1 = String(strDate1).split(".").reverse(); + let date2 = String(strDate2).split(".").reverse(); + if (date1.length !== 3 || date2.length !== 3) { + return ["Error date in input string"]; + } + if (date1 > date2) { + return ["first is yonger"]; + } + return date1 < date2 ? ["second is yonger"] : ["both is same age"]; +}; + +functions.push({ + name: "Homework 8 task #1", + func: hw8_f1, + params: [ + `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.2022`, + ], +}); +functions.push({ name: "Homework 8 task #2", func: hw8_f2, params: [] }); +functions.push({ + name: "Homework 8 task #3", + func: hw8_f3, + params: [ + `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ + 1980 + ((Math.random() * 30) >>> 0) + }`, + `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ + 1980 + ((Math.random() * 30) >>> 0) + }`, + ], +}); diff --git a/src/script/homework_8.test.js b/src/script/homework_8.test.js new file mode 100644 index 0000000..e6f3d22 --- /dev/null +++ b/src/script/homework_8.test.js @@ -0,0 +1,83 @@ +import { hw8_f1, hw8_f2, hw8_f3 } from "./homework_8.js"; + +describe("homework 8, function 1 test", () => { + it("is function exists", () => { + expect(hw8_f1).toBeDefined(); + }); + + it("param 26.05.2022", () => { + expect(hw8_f1("26.05.2022")).toEqual(["Thursday"]); + }); + it("param 24.05.2022", () => { + expect(hw8_f1("24.05.2022")).toEqual(["Tuesday"]); + }); + it("param null", () => { + expect(hw8_f1(null).join("").split(" ")).toContain("Error"); + }); + it("param undefined", () => { + expect(hw8_f1(undefined).join("").split(" ")).toContain("Error"); + }); + it("param empty string and empty string", () => { + expect(hw8_f1("").join("").split(" ")).toContain("Error"); + }); +}); + +describe("homework 8, function 2 test", () => { + it("is function exists", () => { + expect(hw8_f2).toBeDefined(); + }); + + it("is correct working", () => { + let m1 = 0; + let m2 = 1; + let m; + + while (m1 !== m2) { + m1 = new Date().getMinutes() + new Date().getHours() * 60; + m = hw8_f2(); + m2 = new Date().getMinutes() + new Date().getHours() * 60; + if (m1 === m2) { + expect(m).toEqual([m1]); + break; + } + } + }); +}); + +describe("homework 8, function 3 test", () => { + it("is function exists", () => { + expect(hw8_f3).toBeDefined(); + }); + + it("param 21.01.2000 and 23.01.2000", () => { + expect(hw8_f3("21.01.2000", "23.01.2000").join("").split(" ")).toContain( + "second" + ); + }); + + it("param 23.01.2000 and 21.01.2000", () => { + expect(hw8_f3("23.01.2000", "21.01.2000").join("").split(" ")).toContain( + "first" + ); + }); + + it("param 21.01.2000 and 21.01.2000", () => { + expect(hw8_f3("21.01.2000", "21.01.2000").join("").split(" ")).toContain( + "same" + ); + }); + + it("param null and 21.01.2000", () => { + expect(hw8_f3(null, "21.01.2000").join("").split(" ")).toContain("Error"); + }); + + it("param 21.01.2000 and undefined", () => { + expect(hw8_f3("21.01.2000", undefined).join("").split(" ")).toContain( + "Error" + ); + }); + + it("param empty string and empty string", () => { + expect(hw8_f3("", "").join("").split(" ")).toContain("Error"); + }); +}); diff --git a/src/script/homework_9.js b/src/script/homework_9.js new file mode 100644 index 0000000..fed94fe --- /dev/null +++ b/src/script/homework_9.js @@ -0,0 +1,71 @@ +import { functions } from "./const.js"; + +export let hw9_f1 = (a, b, c) => { + a = isNaN(Number(a)) ? -1 : Number(a); + b = isNaN(Number(b)) ? -1 : Number(b); + c = isNaN(Number(c)) ? -1 : Number(c); + + if (a < 0 || b < 0 || c < 0) { + return ["Error in input parameters"]; + } + + if ( + Math.pow(a, 2) === Math.pow(b, 2) + Math.pow(c, 2) || + Math.pow(b, 2) === Math.pow(c, 2) + Math.pow(a, 2) || + Math.pow(c, 2) === Math.pow(a, 2) + Math.pow(b, 2) + ) { + return ["right triangle"]; + } + + return ["not right tirangle"]; +}; + +export let hw9_f2 = (r) => { + r = isNaN(Number(r)) ? -1 : Number(r); + + if (r < 0) { + return ["Error in input parameters"]; + } + return [Math.PI * r * 2, Math.PI * Math.pow(2 * r, 2)]; +}; + +export let hw9_f3 = (a, b, c) => { + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); + c = isNaN(Number(c)) ? 0 : Number(c); + + let d = Math.pow(b, 2) - 4 * a * c; + if (d < 0) { + return ["no solution"]; + } else { + if (d > 0) { + return [(Math.sqrt(d) - b) / (2 * a), (-Math.sqrt(d) - b) / (2 * a)]; + } + } + + return [-b / (2 * a)]; +}; + +functions.push({ + name: "Homework 9 task #1", + func: hw9_f1, + params: [ + [3, 4, 5][(Math.random() * 3) >>> 0], + [3, 4, 5][(Math.random() * 3) >>> 0], + [3, 4, 5][(Math.random() * 3) >>> 0], + ], +}); +functions.push({ + name: "Homework 9 task #2", + func: hw9_f2, + params: [(Math.random() * 10) >>> 0], +}); +functions.push({ + name: "Homework 9 task #3", + func: hw9_f3, + params: [ + [3, -4, 54][(Math.random() * 3) >>> 0], + [3, 28, 6][(Math.random() * 3) >>> 0], + [3, -49, 0][(Math.random() * 3) >>> 0], + ], +}); diff --git a/src/script/homework_9.test.js b/src/script/homework_9.test.js new file mode 100644 index 0000000..17525d6 --- /dev/null +++ b/src/script/homework_9.test.js @@ -0,0 +1,79 @@ +import { hw9_f1, hw9_f2, hw9_f3 } from "./homework_9.js"; + +describe("homework 9, function 1 test", () => { + it("is function exists", () => { + expect(hw9_f1).toBeDefined(); + }); + + it("param 4, 5, 3", () => { + expect(hw9_f1(4, 5, 3).join("").split(" ")).not.toContain("not"); + }); + + it("param 3, 4, 5", () => { + expect(hw9_f1(3, 4, 5).join("").split(" ")).not.toContain("not"); + }); + + it("param 5, 4, 3", () => { + expect(hw9_f1(5, 4, 3).join("").split(" ")).not.toContain("not"); + }); + + it("param 6, 5, 3", () => { + expect(hw9_f1(6, 5, 3).join("").split(" ")).toContain("not"); + }); + + it("param 6, 4, 5", () => { + expect(hw9_f1(6, 4, 5).join("").split(" ")).toContain("not"); + }); + + it("param 6, 4, 3", () => { + expect(hw9_f1(6, 4, 3).join("").split(" ")).toContain("not"); + }); + + it("param -4, 5, 3", () => { + expect(hw9_f1(-4, 5, 3).join("").split(" ")).toContain("Error"); + }); + + it("param 3, -4, 5", () => { + expect(hw9_f1(3, -4, 5).join("").split(" ")).toContain("Error"); + }); + + it("param 3, 4, -5", () => { + expect(hw9_f1(3, 4, -5).join("").split(" ")).toContain("Error"); + }); +}); + +describe("homework 8, function 2 test", () => { + it("is function exists", () => { + expect(hw9_f2).toBeDefined(); + }); + + it("param 3", () => { + expect(hw9_f2(3)).toEqual([Math.PI * 3 * 2, Math.PI * Math.pow(3 * 2, 2)]); + }); + + it("param 5", () => { + expect(hw9_f2(5)).toEqual([Math.PI * 5 * 2, Math.PI * Math.pow(5 * 2, 2)]); + }); +}); + +describe("homework 8, function 3 test", () => { + it("is function exists", () => { + expect(hw9_f3).toBeDefined(); + }); + + it("param 4, -1, -5", () => { + expect(hw9_f3(4, -1, -5)).toEqual([1.25, -1]); + }); + + it("param 5, -6, -32", () => { + expect(hw9_f3(5, -6, -32)).toEqual([3.2, -2]); + }); + + it("param 8, 0, 5", () => { + expect(hw9_f3(8, 0, 5).join("").split(" ")).toContain("no"); + }); + + it("param -4, 28, -49", () => { + expect(hw9_f3(-4, 28, -49)).toEqual([3.5]); + }); +}); diff --git a/src/script/ui.js b/src/script/ui.js new file mode 100644 index 0000000..1421618 --- /dev/null +++ b/src/script/ui.js @@ -0,0 +1,99 @@ +import { + CHECKBOX_ID, + CHECKBOX_TEXT, + CHECKBOX_CLASS, + TASKS_CONTAINER_ID, + INFO_DIVS_CONTAINER_CLASS, + CONSOLE_CONTAINER_ID, + functions, +} from "./const.js"; + +let fakeConsole; +let outputConsole; + +export let addTask = (containerId, task, func) => { + if (containerId && task && func) { + let tasksDiv = document.getElementById(containerId); + let a = document.createElement("a"); + a.href = "#"; + a.text = task; + a.onclick = func; + tasksDiv.appendChild(a); + tasksDiv.appendChild(document.createElement("br")); + } +}; + +export function FakeConsole(containerId) { + this.container = document.getElementById(containerId); + this.clear = () => { + this.container.innerHTML = ""; + }; + this.log = (str) => { + this.container.innerHTML += str + "
"; + }; + this.isFake = () => { + return true; + }; + this.clear(); +} + +export let createUI = (containerId) => { + let mainDiv = document.getElementById(containerId); + + let cbxConsole = document.createElement("input"); + cbxConsole.type = "checkbox"; + cbxConsole.checked = true; + cbxConsole.id = CHECKBOX_ID; + cbxConsole.addEventListener("click", (e) => { + outputConsole = e.currentTarget.checked ? fakeConsole : console; + outputConsole.log("!"); + }); + + let lblConsole = document.createElement("span"); + lblConsole.innerHTML = CHECKBOX_TEXT; + + let cbxDiv = document.createElement("div"); + cbxDiv.appendChild(cbxConsole); + cbxDiv.appendChild(lblConsole); + cbxDiv.className = CHECKBOX_CLASS; + + let tasksDiv = document.createElement("div"); + tasksDiv.id = TASKS_CONTAINER_ID; + tasksDiv.className = INFO_DIVS_CONTAINER_CLASS; + + let consoleDiv = document.createElement("div"); + consoleDiv.id = CONSOLE_CONTAINER_ID; + consoleDiv.className = INFO_DIVS_CONTAINER_CLASS; + + mainDiv.appendChild(cbxDiv); + mainDiv.appendChild(tasksDiv); + mainDiv.appendChild(consoleDiv); + + fakeConsole = new FakeConsole(CONSOLE_CONTAINER_ID); + outputConsole = fakeConsole; + + functions.forEach((el) => { + addTask(TASKS_CONTAINER_ID, el.name, () => { + outputConsole.clear(); + if (el.params.length) { + let str = "Parameters: "; + el.params.forEach((el) => { + str += typeof el === "string" ? `"${el}" ` : `${el} `; + }); + outputConsole.log(str); + } + let str = "Results: "; + let fake = "isFake" in outputConsole; + el.func(...el.params).forEach((el) => { + if (!fake && el === "
") { + el = "\n"; + } + str += + typeof el === "string" && el !== "
" && el !== "\n" + ? `"${el}" ` + : `${el} `; + }); + outputConsole.log(str); + }); + }); +}; diff --git a/src/script/ui.test.js b/src/script/ui.test.js new file mode 100644 index 0000000..fef1b63 --- /dev/null +++ b/src/script/ui.test.js @@ -0,0 +1,84 @@ +import { createUI, addTask, FakeConsole } from "./ui.js"; +import { functions } from "./const.js"; + +describe("task adding test", () => { + document.body.innerHTML = "
"; + + let container = document.getElementById("testDiv"); + let func = () => {}; + + addTask("testDiv", "task0", func); + addTask("testDiv", "task1", func); + addTask("testDiv", "task2", func); + + let tasks = container.querySelectorAll("a"); + + it("check added tasks", () => { + expect(container.querySelectorAll("a")).toHaveLength(3); + }); + + it("check tasks captions", () => { + expect(tasks[0].text).toEqual("task0"); + expect(tasks[1].text).toEqual("task1"); + expect(tasks[2].text).toEqual("task2"); + }); + + it("check tasks functions", () => { + expect(tasks[0].onclick).toEqual(func); + expect(tasks[1].onclick).toEqual(func); + expect(tasks[2].onclick).toEqual(func); + }); +}); + +describe("FakeConsole working test", () => { + document.body.innerHTML = "
"; + + let container = document.getElementById("testDiv"); + let myConsole = new FakeConsole("testDiv"); + + it("check console creating", () => { + expect(myConsole).toBeDefined(); + }); + + it("check console container choosing", () => { + expect(myConsole.container).toEqual(container); + }); + + it("check console printing", () => { + myConsole.log("test1!"); + myConsole.log("test2!"); + expect(container.innerHTML).toEqual("test1!
test2!
"); + }); + + it("check console erasing", () => { + myConsole.clear(); + expect(container.innerHTML).toEqual(""); + }); +}); + +describe("UI creation test", () => { + document.body.innerHTML = "
"; + + functions[0] = { name: "task0", func: () => {}, params: [] }; + functions[1] = { name: "task1", func: () => {}, params: [] }; + + createUI("testDiv"); + + let container = document.getElementById("testDiv"); + + it("check divs", () => { + expect(container.querySelectorAll("div")).toHaveLength(3); + }); + + it("check inputs", () => { + expect(container.querySelectorAll("input")).toHaveLength(1); + }); + + it("check spans", () => { + expect(container.querySelectorAll("span")).toHaveLength(1); + }); + + it("check links, created from tasks", () => { + expect(container.querySelectorAll("a")).toHaveLength(2); + }); +}); From c8a15c3762f469666bdcd9e601029d22abbf5190 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 27 May 2022 06:52:23 +0300 Subject: [PATCH 2/4] Homework for lection #9 is prettied right --- .eslintrc.js | 2 +- babel.config.js | 2 +- src/script/homework_1.test.js | 102 +++++++++--------- src/script/homework_10.js | 58 +++++----- src/script/homework_10.test.js | 84 +++++++-------- src/script/homework_2.js | 78 +++++++------- src/script/homework_2.test.js | 146 ++++++++++++------------- src/script/homework_3.js | 50 ++++----- src/script/homework_3.test.js | 102 +++++++++--------- src/script/homework_4.js | 44 ++++---- src/script/homework_4.test.js | 190 ++++++++++++++++----------------- src/script/homework_5.js | 70 ++++++------ src/script/homework_5.test.js | 108 +++++++++---------- src/script/homework_6.js | 46 ++++---- src/script/homework_6.test.js | 140 ++++++++++++------------ src/script/homework_7.js | 104 +++++++++--------- src/script/homework_7.test.js | 106 +++++++++--------- src/script/homework_8.js | 70 ++++++------ src/script/homework_8.test.js | 128 +++++++++++----------- src/script/homework_9.js | 92 ++++++++-------- src/script/homework_9.test.js | 108 +++++++++---------- src/script/ui.js | 152 +++++++++++++------------- src/script/ui.test.js | 110 +++++++++---------- 23 files changed, 1046 insertions(+), 1046 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 5bdd8d2..5d88015 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -11,7 +11,7 @@ module.exports = { sourceType: "module", }, rules: { - indent: ["error", "tab"], + indent: ["error", 2], "linebreak-style": ["error", "unix"], quotes: ["error", "double"], semi: ["error", "always"], diff --git a/babel.config.js b/babel.config.js index 8c5a7ae..24d5e76 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,3 +1,3 @@ module.exports = { - presets: [["@babel/preset-env", { targets: { node: "current" } }]], + presets: [["@babel/preset-env", { targets: { node: "current" } }]], }; diff --git a/src/script/homework_1.test.js b/src/script/homework_1.test.js index ed48b1c..6cf3a79 100644 --- a/src/script/homework_1.test.js +++ b/src/script/homework_1.test.js @@ -1,71 +1,71 @@ import { hw1_f1, hw1_f2, hw1_f3 } from "./homework_1.js"; describe("homework 1, function 1 test", () => { - it("is function exists", () => { - expect(hw1_f1).toBeDefined(); - }); + it("is function exists", () => { + expect(hw1_f1).toBeDefined(); + }); - it("param 5 and 4", () => { - expect(hw1_f1(5, 4)).toEqual([20, 9]); - }); - it("param 9 and 9", () => { - expect(hw1_f1(9, 9)).toEqual([81, 18]); - }); - it("param null and null", () => { - expect(hw1_f1(null, null)).toEqual([0, 0]); - }); - it("param undefined and undefined", () => { - expect(hw1_f1(undefined, undefined)).toEqual([0, 0]); - }); - it("param empty string and empty string", () => { - expect(hw1_f1("", "")).toEqual([0, 0]); - }); + it("param 5 and 4", () => { + expect(hw1_f1(5, 4)).toEqual([20, 9]); + }); + it("param 9 and 9", () => { + expect(hw1_f1(9, 9)).toEqual([81, 18]); + }); + it("param null and null", () => { + expect(hw1_f1(null, null)).toEqual([0, 0]); + }); + it("param undefined and undefined", () => { + expect(hw1_f1(undefined, undefined)).toEqual([0, 0]); + }); + it("param empty string and empty string", () => { + expect(hw1_f1("", "")).toEqual([0, 0]); + }); }); describe("homework 1, function 2 test", () => { - it("is function exists", () => { - expect(hw1_f2).toBeDefined(); - }); + it("is function exists", () => { + expect(hw1_f2).toBeDefined(); + }); - it("param '123' and '1234", () => { - expect(hw1_f2("123", "1234")).toEqual([7]); - }); + it("param '123' and '1234", () => { + expect(hw1_f2("123", "1234")).toEqual([7]); + }); - it("param null and null", () => { - expect(hw1_f2(null, null)).toEqual([0]); - }); + it("param null and null", () => { + expect(hw1_f2(null, null)).toEqual([0]); + }); - it("param undefined and undefined", () => { - expect(hw1_f2(undefined, undefined)).toEqual([0]); - }); + it("param undefined and undefined", () => { + expect(hw1_f2(undefined, undefined)).toEqual([0]); + }); - it("param empty string and empty string", () => { - expect(hw1_f2("", "")).toEqual([0]); - }); + it("param empty string and empty string", () => { + expect(hw1_f2("", "")).toEqual([0]); + }); }); describe("homework 1, function 3 test", () => { - it("is function exists", () => { - expect(hw1_f3).toBeDefined(); - }); + it("is function exists", () => { + expect(hw1_f3).toBeDefined(); + }); - it("param '123'", () => { - expect(hw1_f3("123")).toEqual([6]); - }); + it("param '123'", () => { + expect(hw1_f3("123")).toEqual([6]); + }); - it("param '1234'", () => { - expect(hw1_f3("1234")).toEqual([6]); - }); + it("param '1234'", () => { + expect(hw1_f3("1234")).toEqual([6]); + }); - it("param null", () => { - expect(hw1_f3(null)).toEqual([0]); - }); + it("param null", () => { + expect(hw1_f3(null)).toEqual([0]); + }); - it("param undefined", () => { - expect(hw1_f3(undefined)).toEqual([0]); - }); + it("param undefined", () => { + expect(hw1_f3(undefined)).toEqual([0]); + }); - it("param empty string and empty string", () => { - expect(hw1_f3("")).toEqual([0]); - }); + it("param empty string and empty string", () => { + expect(hw1_f3("")).toEqual([0]); + }); }); diff --git a/src/script/homework_10.js b/src/script/homework_10.js index 50a885f..a3100e6 100644 --- a/src/script/homework_10.js +++ b/src/script/homework_10.js @@ -1,39 +1,39 @@ import { functions } from "./const.js"; export let hw10_f1 = (str) => { - if ( - /^[a-zA-Z]{1}[a-zA-Z0-9_-]+[a-zA-Z0-9]{1}@[a-zA-Z0-9]{1}[a-zA-Z0-9-]+[a-zA-Z0-9]{1}\.[a-zA-Z]{2,}$/.test( - str - ) - ) { - return ["mail"]; - } + if ( + /^[a-zA-Z]{1}[a-zA-Z0-9_-]+[a-zA-Z0-9]{1}@[a-zA-Z0-9]{1}[a-zA-Z0-9-]+[a-zA-Z0-9]{1}\.[a-zA-Z]{2,}$/.test( + str + ) + ) { + return ["mail"]; + } - if (/^(((\+7)|8))[89][0-9]{9}$/.test(str)) { - return ["phone number"]; - } + if (/^(((\+7)|8))[89][0-9]{9}$/.test(str)) { + return ["phone number"]; + } - if (/^([0-9]{1,2}\.[0-9]{1,2}\.)([0-9]{4}|[0-9]{2})$/.test(str)) { - return ["date"]; - } + if (/^([0-9]{1,2}\.[0-9]{1,2}\.)([0-9]{4}|[0-9]{2})$/.test(str)) { + return ["date"]; + } - return ["not correct date or phone number or mail"]; + return ["not correct date or phone number or mail"]; }; functions.push({ - name: "Homework 10 task #1", - func: hw10_f1, - params: [ - [ - "mail@mail.ru", - "89009998877", - "11.12.2012", - "mail@mail", - "8900999887", - "+79009998877", - "+78009998877", - "11.12.213", - "11.12.13", - ][(Math.random() * 9) >>> 0], - ], + name: "Homework 10 task #1", + func: hw10_f1, + params: [ + [ + "mail@mail.ru", + "89009998877", + "11.12.2012", + "mail@mail", + "8900999887", + "+79009998877", + "+78009998877", + "11.12.213", + "11.12.13", + ][(Math.random() * 9) >>> 0], + ], }); diff --git a/src/script/homework_10.test.js b/src/script/homework_10.test.js index a70c19a..da89a07 100644 --- a/src/script/homework_10.test.js +++ b/src/script/homework_10.test.js @@ -1,59 +1,59 @@ import { hw10_f1 } from "./homework_10.js"; describe("homework 10, function 1 test", () => { - it("is function exists", () => { - expect(hw10_f1).toBeDefined(); - }); + it("is function exists", () => { + expect(hw10_f1).toBeDefined(); + }); - it("param 22.10.10", () => { - expect(hw10_f1("22.10.10")).toEqual(["date"]); - }); + it("param 22.10.10", () => { + expect(hw10_f1("22.10.10")).toEqual(["date"]); + }); - it("param 22.10.1910", () => { - expect(hw10_f1("22.10.1910")).toEqual(["date"]); - }); + it("param 22.10.1910", () => { + expect(hw10_f1("22.10.1910")).toEqual(["date"]); + }); - it("param aaa99@bbb-9.cc", () => { - expect(hw10_f1("aaa99@bbb-9.cc")).toEqual(["mail"]); - }); + it("param aaa99@bbb-9.cc", () => { + expect(hw10_f1("aaa99@bbb-9.cc")).toEqual(["mail"]); + }); - it("param aaa99.bbb-.cc", () => { - expect(hw10_f1("aaa99.bbb-9.cc").join("").split(" ")).toContain("not"); - }); + it("param aaa99.bbb-.cc", () => { + expect(hw10_f1("aaa99.bbb-9.cc").join("").split(" ")).toContain("not"); + }); - it("param 22.10.123", () => { - expect(hw10_f1("22.10.123").join("").split(" ")).toContain("not"); - }); + it("param 22.10.123", () => { + expect(hw10_f1("22.10.123").join("").split(" ")).toContain("not"); + }); - it("param aaa_2@bbb.cc", () => { - expect(hw10_f1("aaa_2@bbb.cc")).toEqual(["mail"]); - }); + it("param aaa_2@bbb.cc", () => { + expect(hw10_f1("aaa_2@bbb.cc")).toEqual(["mail"]); + }); - it("param aaa_2@bbb.cc", () => { - expect(hw10_f1("aaa_@bbb.cc").join("").split(" ")).toContain("not"); - }); + it("param aaa_2@bbb.cc", () => { + expect(hw10_f1("aaa_@bbb.cc").join("").split(" ")).toContain("not"); + }); - it("param +69009998877", () => { - expect(hw10_f1("+69009998877").join("").split(" ")).toContain("not"); - }); + it("param +69009998877", () => { + expect(hw10_f1("+69009998877").join("").split(" ")).toContain("not"); + }); - it("param +79009998877", () => { - expect(hw10_f1("+79009998877")).toEqual(["phone number"]); - }); + it("param +79009998877", () => { + expect(hw10_f1("+79009998877")).toEqual(["phone number"]); + }); - it("param 89009998877", () => { - expect(hw10_f1("89009998877")).toEqual(["phone number"]); - }); + it("param 89009998877", () => { + expect(hw10_f1("89009998877")).toEqual(["phone number"]); + }); - it("param 88009998877", () => { - expect(hw10_f1("88009998877")).toEqual(["phone number"]); - }); + it("param 88009998877", () => { + expect(hw10_f1("88009998877")).toEqual(["phone number"]); + }); - it("param 78009998877", () => { - expect(hw10_f1("78009998877").join("").split(" ")).toContain("not"); - }); + it("param 78009998877", () => { + expect(hw10_f1("78009998877").join("").split(" ")).toContain("not"); + }); - it("param 87009998877", () => { - expect(hw10_f1("87009998877").join("").split(" ")).toContain("not"); - }); + it("param 87009998877", () => { + expect(hw10_f1("87009998877").join("").split(" ")).toContain("not"); + }); }); diff --git a/src/script/homework_2.js b/src/script/homework_2.js index 8faa396..8ff7fa3 100644 --- a/src/script/homework_2.js +++ b/src/script/homework_2.js @@ -1,57 +1,57 @@ import { functions } from "./const.js"; export let hw2_f1 = (a, b) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); - return [Math.max(a, b)]; + return [Math.max(a, b)]; }; export let hw2_f2 = () => { - let m = prompt("Please enter the number of month (1..12)"); - m = isNaN(Number(m)) ? 1 : Number(m); - if (m > 12) { - m = 12; - } - if (m < 1) { - m = 1; - } - let months = [ - "Jan", - "Feb", - "Mar", - "Apr", - "May", - "Jun", - "Jul", - "Aug", - "Sep", - "Oct", - "Nov", - "Dec", - ]; - return [months[m - 1]]; + let m = prompt("Please enter the number of month (1..12)"); + m = isNaN(Number(m)) ? 1 : Number(m); + if (m > 12) { + m = 12; + } + if (m < 1) { + m = 1; + } + let months = [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec", + ]; + return [months[m - 1]]; }; export let hw2_f3 = (circle, square) => { - square = isNaN(Number(square)) ? 0 : Number(square); - circle = isNaN(Number(circle)) ? 0 : Number(circle); + square = isNaN(Number(square)) ? 0 : Number(square); + circle = isNaN(Number(circle)) ? 0 : Number(circle); - let r = circle / (Math.PI * 2); - return r <= Math.sqrt(square) / 2 ? ["fitted"] : ["not fitted"]; + let r = circle / (Math.PI * 2); + return r <= Math.sqrt(square) / 2 ? ["fitted"] : ["not fitted"]; }; functions.push({ - name: "Homework 2 task #1", - func: hw2_f1, - params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], + name: "Homework 2 task #1", + func: hw2_f1, + params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], }); functions.push({ name: "Homework 2 task #2", func: hw2_f2, params: [] }); functions.push({ - name: "Homework 2 task #3", - func: hw2_f3, - params: [ - [18.85, 18][(Math.random() * 2) >>> 0], - [36.1, 25][(Math.random() * 2) >> 0], - ], + name: "Homework 2 task #3", + func: hw2_f3, + params: [ + [18.85, 18][(Math.random() * 2) >>> 0], + [36.1, 25][(Math.random() * 2) >> 0], + ], }); diff --git a/src/script/homework_2.test.js b/src/script/homework_2.test.js index 2ea72be..c2b4a73 100644 --- a/src/script/homework_2.test.js +++ b/src/script/homework_2.test.js @@ -1,85 +1,85 @@ import { hw2_f1, hw2_f2, hw2_f3 } from "./homework_2.js"; describe("homework 2, function 1 test", () => { - it("is function exists", () => { - expect(hw2_f1).toBeDefined(); - }); - - it("param 5 and 4", () => { - expect(hw2_f1(5, 4)).toEqual([5]); - }); - - it("param 7 and 8", () => { - expect(hw2_f1(7, 8)).toEqual([8]); - }); - - it("param null and null", () => { - expect(hw2_f1(null, null)).toEqual([0]); - }); - it("param undefined and undefined", () => { - expect(hw2_f1(undefined, undefined)).toEqual([0]); - }); - it("param empty string and empty string", () => { - expect(hw2_f1("", "")).toEqual([0]); - }); + it("is function exists", () => { + expect(hw2_f1).toBeDefined(); + }); + + it("param 5 and 4", () => { + expect(hw2_f1(5, 4)).toEqual([5]); + }); + + it("param 7 and 8", () => { + expect(hw2_f1(7, 8)).toEqual([8]); + }); + + it("param null and null", () => { + expect(hw2_f1(null, null)).toEqual([0]); + }); + it("param undefined and undefined", () => { + expect(hw2_f1(undefined, undefined)).toEqual([0]); + }); + it("param empty string and empty string", () => { + expect(hw2_f1("", "")).toEqual([0]); + }); }); describe("homework 2, function 2 test", () => { - it("is function exists", () => { - expect(hw2_f2).toBeDefined(); - }); - - it("prompted 1", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "1"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted 6", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "6"); - expect(hw2_f2()).toEqual(["Jun"]); - }); - - it("prompted 12", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "12"); - expect(hw2_f2()).toEqual(["Dec"]); - }); - - it("prompted 13", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "13"); - expect(hw2_f2()).toEqual(["Dec"]); - }); - - it("prompted 0", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "0"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted null", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "null"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted undefined", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted empty string", () => { - jest.spyOn(window, "prompt").mockImplementation(() => ""); - expect(hw2_f2()).toEqual(["Jan"]); - }); + it("is function exists", () => { + expect(hw2_f2).toBeDefined(); + }); + + it("prompted 1", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "1"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted 6", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "6"); + expect(hw2_f2()).toEqual(["Jun"]); + }); + + it("prompted 12", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "12"); + expect(hw2_f2()).toEqual(["Dec"]); + }); + + it("prompted 13", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "13"); + expect(hw2_f2()).toEqual(["Dec"]); + }); + + it("prompted 0", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "0"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted null", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "null"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted undefined", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); + expect(hw2_f2()).toEqual(["Jan"]); + }); + + it("prompted empty string", () => { + jest.spyOn(window, "prompt").mockImplementation(() => ""); + expect(hw2_f2()).toEqual(["Jan"]); + }); }); describe("homework 2, function 3 test", () => { - it("is function exists", () => { - expect(hw2_f3).toBeDefined(); - }); + it("is function exists", () => { + expect(hw2_f3).toBeDefined(); + }); - it("param 16 36", () => { - expect(hw2_f3(16, 36)).toEqual(["fitted"]); - }); + it("param 16 36", () => { + expect(hw2_f3(16, 36)).toEqual(["fitted"]); + }); - it("param 36 16", () => { - expect(hw2_f3(36, 16)).toEqual(["not fitted"]); - }); + it("param 36 16", () => { + expect(hw2_f3(36, 16)).toEqual(["not fitted"]); + }); }); diff --git a/src/script/homework_3.js b/src/script/homework_3.js index a5358a1..19bc050 100644 --- a/src/script/homework_3.js +++ b/src/script/homework_3.js @@ -1,39 +1,39 @@ import { functions } from "./const.js"; export let hw3_f1 = () => { - let sum = 0; - for (let i = 50; i <= 100; i++) { - sum += i; - } - return [sum]; + let sum = 0; + for (let i = 50; i <= 100; i++) { + sum += i; + } + return [sum]; }; export let hw3_f2 = () => { - let result = []; - for (let i = 1; i < 10; i++) { - result.push("
"); - result.push(`7 x ${i} = ${7 * i}`); - } - return result; + let result = []; + for (let i = 1; i < 10; i++) { + result.push("
"); + result.push(`7 x ${i} = ${7 * i}`); + } + return result; }; export let hw3_f3 = () => { - let n = prompt("Please enter the number N"); - n = isNaN(Number(n)) ? 0 : Number(n); + let n = prompt("Please enter the number N"); + n = isNaN(Number(n)) ? 0 : Number(n); - if (n < 1) { - return [0]; - } + if (n < 1) { + return [0]; + } - let sum = 0; - let cnt = 0; - for (let i = 1; i <= n; i++) { - if (i & 0x01) { - cnt++; - sum += i; - } - } - return [sum / cnt]; + let sum = 0; + let cnt = 0; + for (let i = 1; i <= n; i++) { + if (i & 0x01) { + cnt++; + sum += i; + } + } + return [sum / cnt]; }; functions.push({ name: "Homework 3 task #1", func: hw3_f1, params: [] }); diff --git a/src/script/homework_3.test.js b/src/script/homework_3.test.js index ca2c356..ba2a028 100644 --- a/src/script/homework_3.test.js +++ b/src/script/homework_3.test.js @@ -1,68 +1,68 @@ import { hw3_f1, hw3_f2, hw3_f3 } from "./homework_3.js"; describe("homework 3, function 1 test", () => { - it("is function exists", () => { - expect(hw3_f1).toBeDefined(); - }); + it("is function exists", () => { + expect(hw3_f1).toBeDefined(); + }); - it("check sum", () => { - let sum = 0; - for (let i = 100; i >= 50; i--) { - sum += i; - } - expect(hw3_f1()).toEqual([sum]); - }); + it("check sum", () => { + let sum = 0; + for (let i = 100; i >= 50; i--) { + sum += i; + } + expect(hw3_f1()).toEqual([sum]); + }); }); describe("homework 3, function 2 test", () => { - it("is function exists", () => { - expect(hw3_f2).toBeDefined(); - }); + it("is function exists", () => { + expect(hw3_f2).toBeDefined(); + }); - it("checking seven multiple table", () => { - let check = []; - for (let i = 1; i < 10; i++) { - check.push(i * 7); - } - let rslt = hw3_f2() - .join("") - .split("
") - .map((el) => { - return Number(el.split("=").pop().trim()); - }) - .slice(1); - expect(check).toEqual(rslt); - }); + it("checking seven multiple table", () => { + let check = []; + for (let i = 1; i < 10; i++) { + check.push(i * 7); + } + let rslt = hw3_f2() + .join("") + .split("
") + .map((el) => { + return Number(el.split("=").pop().trim()); + }) + .slice(1); + expect(check).toEqual(rslt); + }); }); describe("homework 3, function 3 test", () => { - it("prompted 3", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "3"); - expect(hw3_f3()).toEqual([2]); - }); + it("prompted 3", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "3"); + expect(hw3_f3()).toEqual([2]); + }); - it("prompted 9", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "9"); - expect(hw3_f3()).toEqual([5]); - }); + it("prompted 9", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "9"); + expect(hw3_f3()).toEqual([5]); + }); - it("prompted 0", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "0"); - expect(hw3_f3()).toEqual([0]); - }); + it("prompted 0", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "0"); + expect(hw3_f3()).toEqual([0]); + }); - it("prompted null", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "null"); - expect(hw3_f3()).toEqual([0]); - }); + it("prompted null", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "null"); + expect(hw3_f3()).toEqual([0]); + }); - it("prompted undefined", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); - expect(hw3_f3()).toEqual([0]); - }); + it("prompted undefined", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); + expect(hw3_f3()).toEqual([0]); + }); - it("prompted empty string", () => { - jest.spyOn(window, "prompt").mockImplementation(() => ""); - expect(hw3_f3()).toEqual([0]); - }); + it("prompted empty string", () => { + jest.spyOn(window, "prompt").mockImplementation(() => ""); + expect(hw3_f3()).toEqual([0]); + }); }); diff --git a/src/script/homework_4.js b/src/script/homework_4.js index e584761..c101734 100644 --- a/src/script/homework_4.js +++ b/src/script/homework_4.js @@ -4,36 +4,36 @@ let user = {}; let admin = {}; export let hw4_f1 = () => { - let n = prompt("Please enter the age of user"); - n = isNaN(Number(n)) ? 0 : Number(n); - user.name = "John"; - user.age = n; + let n = prompt("Please enter the age of user"); + n = isNaN(Number(n)) ? 0 : Number(n); + user.name = "John"; + user.age = n; - let rslt = []; - rslt.push(user); - rslt.push("
"); - rslt.push(`user.name = ${user.name}, user.age = ${user.age}`); - return rslt; + let rslt = []; + rslt.push(user); + rslt.push("
"); + rslt.push(`user.name = ${user.name}, user.age = ${user.age}`); + return rslt; }; export let hw4_f2 = () => { - admin = Object.assign({}, user); - admin.role = "admin"; + admin = Object.assign({}, user); + admin.role = "admin"; - let rslt = []; - rslt.push(admin); - rslt.push("
"); - rslt.push( - `admin.name = ${user.name}, admin.age = ${user.age}, admin.role = ${admin.role}` - ); - return rslt; + let rslt = []; + rslt.push(admin); + rslt.push("
"); + rslt.push( + `admin.name = ${user.name}, admin.age = ${user.age}, admin.role = ${admin.role}` + ); + return rslt; }; export let hw4_f3 = () => { - for (let prop in admin) { - window[prop] = admin[prop]; - } - return ["Variables are created in Window object"]; + for (let prop in admin) { + window[prop] = admin[prop]; + } + return ["Variables are created in Window object"]; }; functions.push({ name: "Homework 4 task #1", func: hw4_f1, params: [] }); diff --git a/src/script/homework_4.test.js b/src/script/homework_4.test.js index 3582c50..ff101f5 100644 --- a/src/script/homework_4.test.js +++ b/src/script/homework_4.test.js @@ -1,122 +1,122 @@ import { hw4_f1, hw4_f2, hw4_f3 } from "./homework_4.js"; describe("homework 4, function 1 test", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "23"); - - it("is function exists", () => { - expect(hw4_f1).toBeDefined(); - }); - - it("check user object", () => { - expect(typeof hw4_f1()[0]).toEqual("object"); - }); - - it("check user name exists", () => { - expect(hw4_f1()[0].name).toBeDefined(); - }); - - it("check user name value", () => { - expect(hw4_f1()[0].name).toEqual("John"); - }); - - it("check user age exists", () => { - expect(hw4_f1()[0].age).toBeDefined(); - }); - - it("check user age value", () => { - expect(hw4_f1()[0].age).toEqual(23); - }); - - it("check user age modified", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "21"); - expect(hw4_f1()[0].age).toEqual(21); - }); - - it("check user age null prompted", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "null"); - expect(hw4_f1()[0].age).toEqual(0); - }); - - it("check user age undefined prompted", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); - expect(hw4_f1()[0].age).toEqual(0); - }); - - it("check user age empty string prompted", () => { - jest.spyOn(window, "prompt").mockImplementation(() => ""); - expect(hw4_f1()[0].age).toEqual(0); - }); + jest.spyOn(window, "prompt").mockImplementation(() => "23"); + + it("is function exists", () => { + expect(hw4_f1).toBeDefined(); + }); + + it("check user object", () => { + expect(typeof hw4_f1()[0]).toEqual("object"); + }); + + it("check user name exists", () => { + expect(hw4_f1()[0].name).toBeDefined(); + }); + + it("check user name value", () => { + expect(hw4_f1()[0].name).toEqual("John"); + }); + + it("check user age exists", () => { + expect(hw4_f1()[0].age).toBeDefined(); + }); + + it("check user age value", () => { + expect(hw4_f1()[0].age).toEqual(23); + }); + + it("check user age modified", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "21"); + expect(hw4_f1()[0].age).toEqual(21); + }); + + it("check user age null prompted", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "null"); + expect(hw4_f1()[0].age).toEqual(0); + }); + + it("check user age undefined prompted", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); + expect(hw4_f1()[0].age).toEqual(0); + }); + + it("check user age empty string prompted", () => { + jest.spyOn(window, "prompt").mockImplementation(() => ""); + expect(hw4_f1()[0].age).toEqual(0); + }); }); describe("homework 3, function 2 test", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "23"); - let user = hw4_f1()[0]; + jest.spyOn(window, "prompt").mockImplementation(() => "23"); + let user = hw4_f1()[0]; - it("is function exists", () => { - expect(hw4_f2).toBeDefined(); - }); + it("is function exists", () => { + expect(hw4_f2).toBeDefined(); + }); - it("check admin object", () => { - expect(typeof hw4_f2()[0]).toEqual("object"); - }); + it("check admin object", () => { + expect(typeof hw4_f2()[0]).toEqual("object"); + }); - it("check admin name exists", () => { - expect(hw4_f2()[0].name).toBeDefined(); - }); + it("check admin name exists", () => { + expect(hw4_f2()[0].name).toBeDefined(); + }); - it("check that admin name is equal to user name", () => { - expect(hw4_f2()[0].name).toEqual(user.name); - }); + it("check that admin name is equal to user name", () => { + expect(hw4_f2()[0].name).toEqual(user.name); + }); - it("check admin age exists", () => { - expect(hw4_f2()[0].age).toBeDefined(); - }); + it("check admin age exists", () => { + expect(hw4_f2()[0].age).toBeDefined(); + }); - it("check admin age value", () => { - expect(hw4_f2()[0].age).toEqual(user.age); - }); + it("check admin age value", () => { + expect(hw4_f2()[0].age).toEqual(user.age); + }); - it("check admin role exists", () => { - expect(hw4_f2()[0].role).toBeDefined(); - }); + it("check admin role exists", () => { + expect(hw4_f2()[0].role).toBeDefined(); + }); - it("check admin role value", () => { - expect(hw4_f2()[0].role).toEqual("admin"); - }); + it("check admin role value", () => { + expect(hw4_f2()[0].role).toEqual("admin"); + }); }); describe("homework 3, function 3 test", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "23"); + jest.spyOn(window, "prompt").mockImplementation(() => "23"); - let admin = hw4_f2()[0]; + let admin = hw4_f2()[0]; - it("is function exists", () => { - expect(hw4_f3).toBeDefined(); - }); + it("is function exists", () => { + expect(hw4_f3).toBeDefined(); + }); - hw4_f3(); + hw4_f3(); - it("check variable age exists", () => { - expect(window.age).toBeDefined(); - }); + it("check variable age exists", () => { + expect(window.age).toBeDefined(); + }); - it("check variable age value", () => { - expect(window.age).toEqual(admin.age); - }); + it("check variable age value", () => { + expect(window.age).toEqual(admin.age); + }); - it("check variable name exists", () => { - expect(window.name).toBeDefined(); - }); + it("check variable name exists", () => { + expect(window.name).toBeDefined(); + }); - it("check variable name value", () => { - expect(window.name).toEqual(admin.name); - }); + it("check variable name value", () => { + expect(window.name).toEqual(admin.name); + }); - it("check variable role exists", () => { - expect(window.role).toBeDefined(); - }); + it("check variable role exists", () => { + expect(window.role).toBeDefined(); + }); - it("check variable role value", () => { - expect(window.role).toEqual(admin.role); - }); + it("check variable role value", () => { + expect(window.role).toEqual(admin.role); + }); }); diff --git a/src/script/homework_5.js b/src/script/homework_5.js index 36a78da..4c4f746 100644 --- a/src/script/homework_5.js +++ b/src/script/homework_5.js @@ -1,52 +1,52 @@ import { functions } from "./const.js"; export let hw5_f1 = (a) => { - let sum = a.reduce((prev, curr) => { - return prev + (isNaN(Number(curr)) ? 0 : Number(curr)); - }, 0); - return [sum]; + let sum = a.reduce((prev, curr) => { + return prev + (isNaN(Number(curr)) ? 0 : Number(curr)); + }, 0); + return [sum]; }; export let hw5_f2 = (a) => { - let rslt = a.map((el) => { - return (isNaN(Number(el)) ? 0 : Number(el)) * 2; - }); - return rslt; + let rslt = a.map((el) => { + return (isNaN(Number(el)) ? 0 : Number(el)) * 2; + }); + return rslt; }; export let hw5_f3 = (a) => { - a = a.map((el) => { - return isNaN(Number(el)) ? 0 : Number(el); - }); - let rslt = [Math.max(...a)]; - rslt.push(Math.min(...a)); - return rslt; + a = a.map((el) => { + return isNaN(Number(el)) ? 0 : Number(el); + }); + let rslt = [Math.max(...a)]; + rslt.push(Math.min(...a)); + return rslt; }; functions.push({ - name: "Homework 5 task #1", - func: hw5_f1, - params: [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { - return (Math.random() * 100).toFixed(); - }), - ], + name: "Homework 5 task #1", + func: hw5_f1, + params: [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { + return (Math.random() * 100).toFixed(); + }), + ], }); functions.push({ - name: "Homework 5 task #2", - func: hw5_f2, - params: [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { - return (Math.random() * 10).toFixed(); - }), - ], + name: "Homework 5 task #2", + func: hw5_f2, + params: [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { + return (Math.random() * 10).toFixed(); + }), + ], }); functions.push({ - name: "Homework 5 task #3", - func: hw5_f3, - params: [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { - return (Math.random() * 100).toFixed(); - }), - ], + name: "Homework 5 task #3", + func: hw5_f3, + params: [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { + return (Math.random() * 100).toFixed(); + }), + ], }); diff --git a/src/script/homework_5.test.js b/src/script/homework_5.test.js index 8dc441d..cb379d3 100644 --- a/src/script/homework_5.test.js +++ b/src/script/homework_5.test.js @@ -1,79 +1,79 @@ import { hw5_f1, hw5_f2, hw5_f3 } from "./homework_5.js"; describe("homework 5, function 1 test", () => { - it("is function exists", () => { - expect(hw5_f1).toBeDefined(); - }); + it("is function exists", () => { + expect(hw5_f1).toBeDefined(); + }); - it("check array elements summary", () => { - expect(hw5_f1([1, 2, 3])[0]).toEqual(6); - }); + it("check array elements summary", () => { + expect(hw5_f1([1, 2, 3])[0]).toEqual(6); + }); - it("check wrong array paramater (null)", () => { - expect(hw5_f1([null, null, null])[0]).toEqual(0); - }); + it("check wrong array paramater (null)", () => { + expect(hw5_f1([null, null, null])[0]).toEqual(0); + }); - it("check wrong array paramater (undefined)", () => { - expect(hw5_f1([undefined, undefined, undefined])[0]).toEqual(0); - }); + it("check wrong array paramater (undefined)", () => { + expect(hw5_f1([undefined, undefined, undefined])[0]).toEqual(0); + }); - it("check wrong array paramater (empty strings)", () => { - expect(hw5_f1(["", "", ""])[0]).toEqual(0); - }); + it("check wrong array paramater (empty strings)", () => { + expect(hw5_f1(["", "", ""])[0]).toEqual(0); + }); - it("check wrong array paramater (NaN)", () => { - expect(hw5_f1([NaN, NaN, NaN])[0]).toEqual(0); - }); + it("check wrong array paramater (NaN)", () => { + expect(hw5_f1([NaN, NaN, NaN])[0]).toEqual(0); + }); }); describe("homework 3, function 2 test", () => { - it("is function exists", () => { - expect(hw5_f2).toBeDefined(); - }); + it("is function exists", () => { + expect(hw5_f2).toBeDefined(); + }); - it("check new array generation", () => { - expect(hw5_f2([1, 2, 3])).toEqual([2, 4, 6]); - }); + it("check new array generation", () => { + expect(hw5_f2([1, 2, 3])).toEqual([2, 4, 6]); + }); - it("check wrong array paramater (null)", () => { - expect(hw5_f2([null, null, null])).toEqual([0, 0, 0]); - }); + it("check wrong array paramater (null)", () => { + expect(hw5_f2([null, null, null])).toEqual([0, 0, 0]); + }); - it("check wrong array paramater (undefined)", () => { - expect(hw5_f2([undefined, undefined, undefined])).toEqual([0, 0, 0]); - }); + it("check wrong array paramater (undefined)", () => { + expect(hw5_f2([undefined, undefined, undefined])).toEqual([0, 0, 0]); + }); - it("check wrong array paramater (empty strings)", () => { - expect(hw5_f2(["", "", ""])).toEqual([0, 0, 0]); - }); + it("check wrong array paramater (empty strings)", () => { + expect(hw5_f2(["", "", ""])).toEqual([0, 0, 0]); + }); - it("check wrong array paramater (NaN)", () => { - expect(hw5_f2([NaN, NaN, NaN])).toEqual([0, 0, 0]); - }); + it("check wrong array paramater (NaN)", () => { + expect(hw5_f2([NaN, NaN, NaN])).toEqual([0, 0, 0]); + }); }); describe("homework 3, function 3 test", () => { - it("is function exists", () => { - expect(hw5_f3).toBeDefined(); - }); + it("is function exists", () => { + expect(hw5_f3).toBeDefined(); + }); - it("check array elements summary", () => { - expect(hw5_f3([1, 2, 3])).toEqual([3, 1]); - }); + it("check array elements summary", () => { + expect(hw5_f3([1, 2, 3])).toEqual([3, 1]); + }); - it("check wrong array (null)", () => { - expect(hw5_f3([null, null, null])).toEqual([0, 0]); - }); + it("check wrong array (null)", () => { + expect(hw5_f3([null, null, null])).toEqual([0, 0]); + }); - it("check wrong array (undefined)", () => { - expect(hw5_f3([undefined, undefined])).toEqual([0, 0]); - }); + it("check wrong array (undefined)", () => { + expect(hw5_f3([undefined, undefined])).toEqual([0, 0]); + }); - it("check wrong array (empty strings)", () => { - expect(hw5_f3(["", ""])).toEqual([0, 0]); - }); + it("check wrong array (empty strings)", () => { + expect(hw5_f3(["", ""])).toEqual([0, 0]); + }); - it("check wrong array (NaN)", () => { - expect(hw5_f3([NaN, NaN])).toEqual([0, 0]); - }); + it("check wrong array (NaN)", () => { + expect(hw5_f3([NaN, NaN])).toEqual([0, 0]); + }); }); diff --git a/src/script/homework_6.js b/src/script/homework_6.js index dedf90f..3f9256c 100644 --- a/src/script/homework_6.js +++ b/src/script/homework_6.js @@ -1,43 +1,43 @@ import { functions } from "./const.js"; export let hw6_f1 = (a, b) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); - return [Math.abs(a - b)]; + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); + return [Math.abs(a - b)]; }; export let hw6_f2 = (str) => { - str = String(str); - return [str.split(" ").length !== 1 ? "false" : "true"]; + str = String(str); + return [str.split(" ").length !== 1 ? "false" : "true"]; }; export let hw6_f3 = (a, b) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); - return [Math.pow(a, b)]; + return [Math.pow(a, b)]; }; functions.push({ - name: "Homework 6 task #1", - func: hw6_f1, - params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], + name: "Homework 6 task #1", + func: hw6_f1, + params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], }); functions.push({ - name: "Homework 6 task #2", - func: hw6_f2, - params: [ - Math.random() - .toFixed(Math.random() * 10) - .toString() + + name: "Homework 6 task #2", + func: hw6_f2, + params: [ + Math.random() + .toFixed(Math.random() * 10) + .toString() + ["", " "][(Math.random() * 2) >>> 0] + Math.random() - .toFixed(Math.random() * 10) - .toString(), - ], + .toFixed(Math.random() * 10) + .toString(), + ], }); functions.push({ - name: "Homework 6 task #3", - func: hw6_f3, - params: [+(Math.random() * 10).toFixed(), +(Math.random() * 4).toFixed()], + name: "Homework 6 task #3", + func: hw6_f3, + params: [+(Math.random() * 10).toFixed(), +(Math.random() * 4).toFixed()], }); diff --git a/src/script/homework_6.test.js b/src/script/homework_6.test.js index db98bc1..ecda99a 100644 --- a/src/script/homework_6.test.js +++ b/src/script/homework_6.test.js @@ -1,92 +1,92 @@ import { hw6_f1, hw6_f2, hw6_f3 } from "./homework_6.js"; describe("homework 6, function 1 test", () => { - it("is function exists", () => { - expect(hw6_f1).toBeDefined(); - }); - - it("param 5 and 4", () => { - expect(hw6_f1(51, 45)).toEqual([6]); - }); - - it("param 4 and 5", () => { - expect(hw6_f1(4, 5)).toEqual([1]); - }); - - it("param 9 and 9", () => { - expect(hw6_f1(9, 9)).toEqual([0]); - }); - it("param null and null", () => { - expect(hw6_f1(null, null)).toEqual([0]); - }); - it("param undefined and undefined", () => { - expect(hw6_f1(undefined, undefined)).toEqual([0]); - }); - it("param empty string and empty string", () => { - expect(hw6_f1("", "")).toEqual([0]); - }); - - it("param empty NaN and NaN", () => { - expect(hw6_f1(NaN, NaN)).toEqual([0]); - }); + it("is function exists", () => { + expect(hw6_f1).toBeDefined(); + }); + + it("param 5 and 4", () => { + expect(hw6_f1(51, 45)).toEqual([6]); + }); + + it("param 4 and 5", () => { + expect(hw6_f1(4, 5)).toEqual([1]); + }); + + it("param 9 and 9", () => { + expect(hw6_f1(9, 9)).toEqual([0]); + }); + it("param null and null", () => { + expect(hw6_f1(null, null)).toEqual([0]); + }); + it("param undefined and undefined", () => { + expect(hw6_f1(undefined, undefined)).toEqual([0]); + }); + it("param empty string and empty string", () => { + expect(hw6_f1("", "")).toEqual([0]); + }); + + it("param empty NaN and NaN", () => { + expect(hw6_f1(NaN, NaN)).toEqual([0]); + }); }); describe("homework 6, function 2 test", () => { - it("is function exists", () => { - expect(hw6_f2).toBeDefined(); - }); + it("is function exists", () => { + expect(hw6_f2).toBeDefined(); + }); - it("param '123 1234", () => { - expect(hw6_f2("123 1234")).toEqual(["false"]); - }); + it("param '123 1234", () => { + expect(hw6_f2("123 1234")).toEqual(["false"]); + }); - it("param 123", () => { - expect(hw6_f2("123")).toEqual(["true"]); - }); + it("param 123", () => { + expect(hw6_f2("123")).toEqual(["true"]); + }); - it("param null", () => { - expect(hw6_f2(null)).toEqual(["true"]); - }); + it("param null", () => { + expect(hw6_f2(null)).toEqual(["true"]); + }); - it("param undefined", () => { - expect(hw6_f2(undefined)).toEqual(["true"]); - }); + it("param undefined", () => { + expect(hw6_f2(undefined)).toEqual(["true"]); + }); - it("param empty string", () => { - expect(hw6_f2("")).toEqual(["true"]); - }); + it("param empty string", () => { + expect(hw6_f2("")).toEqual(["true"]); + }); - it("param NaN", () => { - expect(hw6_f2(NaN)).toEqual(["true"]); - }); + it("param NaN", () => { + expect(hw6_f2(NaN)).toEqual(["true"]); + }); }); describe("homework 6, function 3 test", () => { - it("is function exists", () => { - expect(hw6_f3).toBeDefined(); - }); + it("is function exists", () => { + expect(hw6_f3).toBeDefined(); + }); - it("param 3 and 3", () => { - expect(hw6_f3(3, 3)).toEqual([27]); - }); + it("param 3 and 3", () => { + expect(hw6_f3(3, 3)).toEqual([27]); + }); - it("param 4 and 2", () => { - expect(hw6_f3(4, 2)).toEqual([16]); - }); + it("param 4 and 2", () => { + expect(hw6_f3(4, 2)).toEqual([16]); + }); - it("param null and null", () => { - expect(hw6_f3(null, null)).toEqual([1]); - }); + it("param null and null", () => { + expect(hw6_f3(null, null)).toEqual([1]); + }); - it("param undefined and undefined", () => { - expect(hw6_f3(undefined, undefined)).toEqual([1]); - }); + it("param undefined and undefined", () => { + expect(hw6_f3(undefined, undefined)).toEqual([1]); + }); - it("param empty string and empty string", () => { - expect(hw6_f3("", "")).toEqual([1]); - }); + it("param empty string and empty string", () => { + expect(hw6_f3("", "")).toEqual([1]); + }); - it("param NaN and NaN", () => { - expect(hw6_f3(NaN, NaN)).toEqual([1]); - }); + it("param NaN and NaN", () => { + expect(hw6_f3(NaN, NaN)).toEqual([1]); + }); }); diff --git a/src/script/homework_7.js b/src/script/homework_7.js index 65e63ed..805bd71 100644 --- a/src/script/homework_7.js +++ b/src/script/homework_7.js @@ -1,59 +1,59 @@ import { - functions, - TASK7_BTN_CLASS_NAME, - TASK7_EDIT_CLASS_NAME, - TASK7_MAIN_CONTAINER_ID, + functions, + TASK7_BTN_CLASS_NAME, + TASK7_EDIT_CLASS_NAME, + TASK7_MAIN_CONTAINER_ID, } from "./const.js"; export let hw7_f1 = (mainContainer) => { - let task7Div = document.getElementById(TASK7_MAIN_CONTAINER_ID); - if (task7Div) { - task7Div.innerHTML = ""; - } else { - task7Div = document.createElement("div"); - task7Div.id = TASK7_MAIN_CONTAINER_ID; - } - - let btn = document.createElement("input"); - - let edit = document.createElement("input"); - edit.type = "text"; - edit.className = TASK7_EDIT_CLASS_NAME; - edit.addEventListener("keyup", () => { - if (edit.value.length) { - btn.style["display"] = "block"; - } else { - btn.style["display"] = "none"; - } - }); - - btn.type = "button"; - btn.className = TASK7_BTN_CLASS_NAME; - btn.addEventListener("click", () => { - let p = document.createElement("p"); - p.innerHTML = edit.value; - task7Div.appendChild(p); - - let allP = document.querySelectorAll("p"); - if (allP.length > 5) { - task7Div.removeChild(allP[0]); - } - }); - - task7Div.appendChild(edit); - task7Div.appendChild(btn); - - for (let i = 0; i < 3; i++) { - let p = document.createElement("p"); - p.innerHTML = `Text for paragraph ${i}`; - task7Div.appendChild(p); - } - - mainContainer.appendChild(task7Div); - return ["All need elements are created"]; + let task7Div = document.getElementById(TASK7_MAIN_CONTAINER_ID); + if (task7Div) { + task7Div.innerHTML = ""; + } else { + task7Div = document.createElement("div"); + task7Div.id = TASK7_MAIN_CONTAINER_ID; + } + + let btn = document.createElement("input"); + + let edit = document.createElement("input"); + edit.type = "text"; + edit.className = TASK7_EDIT_CLASS_NAME; + edit.addEventListener("keyup", () => { + if (edit.value.length) { + btn.style["display"] = "block"; + } else { + btn.style["display"] = "none"; + } + }); + + btn.type = "button"; + btn.className = TASK7_BTN_CLASS_NAME; + btn.addEventListener("click", () => { + let p = document.createElement("p"); + p.innerHTML = edit.value; + task7Div.appendChild(p); + + let allP = document.querySelectorAll("p"); + if (allP.length > 5) { + task7Div.removeChild(allP[0]); + } + }); + + task7Div.appendChild(edit); + task7Div.appendChild(btn); + + for (let i = 0; i < 3; i++) { + let p = document.createElement("p"); + p.innerHTML = `Text for paragraph ${i}`; + task7Div.appendChild(p); + } + + mainContainer.appendChild(task7Div); + return ["All need elements are created"]; }; functions.push({ - name: "Homework 7 task #1", - func: hw7_f1, - params: [document.body], + name: "Homework 7 task #1", + func: hw7_f1, + params: [document.body], }); diff --git a/src/script/homework_7.test.js b/src/script/homework_7.test.js index 4b1a313..4cd98b2 100644 --- a/src/script/homework_7.test.js +++ b/src/script/homework_7.test.js @@ -1,67 +1,67 @@ import { hw7_f1 } from "./homework_7.js"; describe("homework 7, function 1 test", () => { - it("is function exists", () => { - expect(hw7_f1).toBeDefined(); - }); + it("is function exists", () => { + expect(hw7_f1).toBeDefined(); + }); - document.body.innerHTML = "
"; - let container = document.getElementById("testDiv"); + document.body.innerHTML = "
"; + let container = document.getElementById("testDiv"); - hw7_f1(container); + hw7_f1(container); - it("check inputs creation", () => { - expect(container.querySelectorAll("input")).toHaveLength(2); - }); + it("check inputs creation", () => { + expect(container.querySelectorAll("input")).toHaveLength(2); + }); - it("check paragraphs creation", () => { - expect(container.querySelectorAll("p")).toHaveLength(3); - }); + it("check paragraphs creation", () => { + expect(container.querySelectorAll("p")).toHaveLength(3); + }); - it("check inputs types", () => { - expect(container.querySelector("input[type=button]")).toBeDefined(); - expect(container.querySelector("input[type=text]")).toBeDefined(); - }); + it("check inputs types", () => { + expect(container.querySelector("input[type=button]")).toBeDefined(); + expect(container.querySelector("input[type=text]")).toBeDefined(); + }); - it("check button set visible", () => { - let edit = container.querySelector("input[type=text]"); - let btn = container.querySelector("input[type=button]"); - edit.value = "1"; - edit.dispatchEvent(new Event("keyup")); - expect(btn.style["display"]).toEqual("block"); - }); + it("check button set visible", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "1"; + edit.dispatchEvent(new Event("keyup")); + expect(btn.style["display"]).toEqual("block"); + }); - it("check button set unvisible", () => { - let edit = container.querySelector("input[type=text]"); - let btn = container.querySelector("input[type=button]"); - edit.value = ""; - edit.dispatchEvent(new Event("keyup")); - expect(btn.style["display"]).toEqual("none"); - }); + it("check button set unvisible", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = ""; + edit.dispatchEvent(new Event("keyup")); + expect(btn.style["display"]).toEqual("none"); + }); - it("check that button press adding paragraphs", () => { - let edit = container.querySelector("input[type=text]"); - let btn = container.querySelector("input[type=button]"); - edit.value = "1"; - btn.click(); - expect(container.querySelectorAll("p")).toHaveLength(4); - }); + it("check that button press adding paragraphs", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "1"; + btn.click(); + expect(container.querySelectorAll("p")).toHaveLength(4); + }); - it("check that button press adding paragraphs with need text", () => { - let edit = container.querySelector("input[type=text]"); - let btn = container.querySelector("input[type=button]"); - edit.value = "2"; - btn.click(); - expect(container.querySelector("p:last-child").innerHTML).toEqual( - edit.value - ); - }); + it("check that button press adding paragraphs with need text", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "2"; + btn.click(); + expect(container.querySelector("p:last-child").innerHTML).toEqual( + edit.value + ); + }); - it("check that button press adding paragraphs but only five for maximum count", () => { - let edit = container.querySelector("input[type=text]"); - let btn = container.querySelector("input[type=button]"); - edit.value = "3"; - btn.click(); - expect(container.querySelectorAll("p")).toHaveLength(5); - }); + it("check that button press adding paragraphs but only five for maximum count", () => { + let edit = container.querySelector("input[type=text]"); + let btn = container.querySelector("input[type=button]"); + edit.value = "3"; + btn.click(); + expect(container.querySelectorAll("p")).toHaveLength(5); + }); }); diff --git a/src/script/homework_8.js b/src/script/homework_8.js index 9850df5..db3d8ad 100644 --- a/src/script/homework_8.js +++ b/src/script/homework_8.js @@ -1,52 +1,52 @@ import { functions } from "./const.js"; export let hw8_f1 = (strDate) => { - let date = String(strDate).split(".").reverse(); - if (date.length !== 3) { - return ["Error date in input string"]; - } - date[1]--; + let date = String(strDate).split(".").reverse(); + if (date.length !== 3) { + return ["Error date in input string"]; + } + date[1]--; - let day = new Intl.DateTimeFormat("en-US", { weekday: "long" }).format( - new Date(...date) - ); - return [day]; + let day = new Intl.DateTimeFormat("en-US", { weekday: "long" }).format( + new Date(...date) + ); + return [day]; }; export let hw8_f2 = () => { - let date = new Date(); - return [date.getHours() * 60 + date.getMinutes()]; + let date = new Date(); + return [date.getHours() * 60 + date.getMinutes()]; }; export let hw8_f3 = (strDate1, strDate2) => { - let date1 = String(strDate1).split(".").reverse(); - let date2 = String(strDate2).split(".").reverse(); - if (date1.length !== 3 || date2.length !== 3) { - return ["Error date in input string"]; - } - if (date1 > date2) { - return ["first is yonger"]; - } - return date1 < date2 ? ["second is yonger"] : ["both is same age"]; + let date1 = String(strDate1).split(".").reverse(); + let date2 = String(strDate2).split(".").reverse(); + if (date1.length !== 3 || date2.length !== 3) { + return ["Error date in input string"]; + } + if (date1 > date2) { + return ["first is yonger"]; + } + return date1 < date2 ? ["second is yonger"] : ["both is same age"]; }; functions.push({ - name: "Homework 8 task #1", - func: hw8_f1, - params: [ - `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.2022`, - ], + name: "Homework 8 task #1", + func: hw8_f1, + params: [ + `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.2022`, + ], }); functions.push({ name: "Homework 8 task #2", func: hw8_f2, params: [] }); functions.push({ - name: "Homework 8 task #3", - func: hw8_f3, - params: [ - `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ - 1980 + ((Math.random() * 30) >>> 0) - }`, - `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ - 1980 + ((Math.random() * 30) >>> 0) - }`, - ], + name: "Homework 8 task #3", + func: hw8_f3, + params: [ + `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ + 1980 + ((Math.random() * 30) >>> 0) + }`, + `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ + 1980 + ((Math.random() * 30) >>> 0) + }`, + ], }); diff --git a/src/script/homework_8.test.js b/src/script/homework_8.test.js index e6f3d22..800fb8d 100644 --- a/src/script/homework_8.test.js +++ b/src/script/homework_8.test.js @@ -1,83 +1,83 @@ import { hw8_f1, hw8_f2, hw8_f3 } from "./homework_8.js"; describe("homework 8, function 1 test", () => { - it("is function exists", () => { - expect(hw8_f1).toBeDefined(); - }); + it("is function exists", () => { + expect(hw8_f1).toBeDefined(); + }); - it("param 26.05.2022", () => { - expect(hw8_f1("26.05.2022")).toEqual(["Thursday"]); - }); - it("param 24.05.2022", () => { - expect(hw8_f1("24.05.2022")).toEqual(["Tuesday"]); - }); - it("param null", () => { - expect(hw8_f1(null).join("").split(" ")).toContain("Error"); - }); - it("param undefined", () => { - expect(hw8_f1(undefined).join("").split(" ")).toContain("Error"); - }); - it("param empty string and empty string", () => { - expect(hw8_f1("").join("").split(" ")).toContain("Error"); - }); + it("param 26.05.2022", () => { + expect(hw8_f1("26.05.2022")).toEqual(["Thursday"]); + }); + it("param 24.05.2022", () => { + expect(hw8_f1("24.05.2022")).toEqual(["Tuesday"]); + }); + it("param null", () => { + expect(hw8_f1(null).join("").split(" ")).toContain("Error"); + }); + it("param undefined", () => { + expect(hw8_f1(undefined).join("").split(" ")).toContain("Error"); + }); + it("param empty string and empty string", () => { + expect(hw8_f1("").join("").split(" ")).toContain("Error"); + }); }); describe("homework 8, function 2 test", () => { - it("is function exists", () => { - expect(hw8_f2).toBeDefined(); - }); + it("is function exists", () => { + expect(hw8_f2).toBeDefined(); + }); - it("is correct working", () => { - let m1 = 0; - let m2 = 1; - let m; + it("is correct working", () => { + let m1 = 0; + let m2 = 1; + let m; - while (m1 !== m2) { - m1 = new Date().getMinutes() + new Date().getHours() * 60; - m = hw8_f2(); - m2 = new Date().getMinutes() + new Date().getHours() * 60; - if (m1 === m2) { - expect(m).toEqual([m1]); - break; - } - } - }); + while (m1 !== m2) { + m1 = new Date().getMinutes() + new Date().getHours() * 60; + m = hw8_f2(); + m2 = new Date().getMinutes() + new Date().getHours() * 60; + if (m1 === m2) { + expect(m).toEqual([m1]); + break; + } + } + }); }); describe("homework 8, function 3 test", () => { - it("is function exists", () => { - expect(hw8_f3).toBeDefined(); - }); + it("is function exists", () => { + expect(hw8_f3).toBeDefined(); + }); - it("param 21.01.2000 and 23.01.2000", () => { - expect(hw8_f3("21.01.2000", "23.01.2000").join("").split(" ")).toContain( - "second" - ); - }); + it("param 21.01.2000 and 23.01.2000", () => { + expect(hw8_f3("21.01.2000", "23.01.2000").join("").split(" ")).toContain( + "second" + ); + }); - it("param 23.01.2000 and 21.01.2000", () => { - expect(hw8_f3("23.01.2000", "21.01.2000").join("").split(" ")).toContain( - "first" - ); - }); + it("param 23.01.2000 and 21.01.2000", () => { + expect(hw8_f3("23.01.2000", "21.01.2000").join("").split(" ")).toContain( + "first" + ); + }); - it("param 21.01.2000 and 21.01.2000", () => { - expect(hw8_f3("21.01.2000", "21.01.2000").join("").split(" ")).toContain( - "same" - ); - }); + it("param 21.01.2000 and 21.01.2000", () => { + expect(hw8_f3("21.01.2000", "21.01.2000").join("").split(" ")).toContain( + "same" + ); + }); - it("param null and 21.01.2000", () => { - expect(hw8_f3(null, "21.01.2000").join("").split(" ")).toContain("Error"); - }); + it("param null and 21.01.2000", () => { + expect(hw8_f3(null, "21.01.2000").join("").split(" ")).toContain("Error"); + }); - it("param 21.01.2000 and undefined", () => { - expect(hw8_f3("21.01.2000", undefined).join("").split(" ")).toContain( - "Error" - ); - }); + it("param 21.01.2000 and undefined", () => { + expect(hw8_f3("21.01.2000", undefined).join("").split(" ")).toContain( + "Error" + ); + }); - it("param empty string and empty string", () => { - expect(hw8_f3("", "").join("").split(" ")).toContain("Error"); - }); + it("param empty string and empty string", () => { + expect(hw8_f3("", "").join("").split(" ")).toContain("Error"); + }); }); diff --git a/src/script/homework_9.js b/src/script/homework_9.js index fed94fe..caa53b5 100644 --- a/src/script/homework_9.js +++ b/src/script/homework_9.js @@ -1,71 +1,71 @@ import { functions } from "./const.js"; export let hw9_f1 = (a, b, c) => { - a = isNaN(Number(a)) ? -1 : Number(a); - b = isNaN(Number(b)) ? -1 : Number(b); - c = isNaN(Number(c)) ? -1 : Number(c); + a = isNaN(Number(a)) ? -1 : Number(a); + b = isNaN(Number(b)) ? -1 : Number(b); + c = isNaN(Number(c)) ? -1 : Number(c); - if (a < 0 || b < 0 || c < 0) { - return ["Error in input parameters"]; - } + if (a < 0 || b < 0 || c < 0) { + return ["Error in input parameters"]; + } - if ( - Math.pow(a, 2) === Math.pow(b, 2) + Math.pow(c, 2) || + if ( + Math.pow(a, 2) === Math.pow(b, 2) + Math.pow(c, 2) || Math.pow(b, 2) === Math.pow(c, 2) + Math.pow(a, 2) || Math.pow(c, 2) === Math.pow(a, 2) + Math.pow(b, 2) - ) { - return ["right triangle"]; - } + ) { + return ["right triangle"]; + } - return ["not right tirangle"]; + return ["not right tirangle"]; }; export let hw9_f2 = (r) => { - r = isNaN(Number(r)) ? -1 : Number(r); + r = isNaN(Number(r)) ? -1 : Number(r); - if (r < 0) { - return ["Error in input parameters"]; - } - return [Math.PI * r * 2, Math.PI * Math.pow(2 * r, 2)]; + if (r < 0) { + return ["Error in input parameters"]; + } + return [Math.PI * r * 2, Math.PI * Math.pow(2 * r, 2)]; }; export let hw9_f3 = (a, b, c) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); - c = isNaN(Number(c)) ? 0 : Number(c); + a = isNaN(Number(a)) ? 0 : Number(a); + b = isNaN(Number(b)) ? 0 : Number(b); + c = isNaN(Number(c)) ? 0 : Number(c); - let d = Math.pow(b, 2) - 4 * a * c; - if (d < 0) { - return ["no solution"]; - } else { - if (d > 0) { - return [(Math.sqrt(d) - b) / (2 * a), (-Math.sqrt(d) - b) / (2 * a)]; - } - } + let d = Math.pow(b, 2) - 4 * a * c; + if (d < 0) { + return ["no solution"]; + } else { + if (d > 0) { + return [(Math.sqrt(d) - b) / (2 * a), (-Math.sqrt(d) - b) / (2 * a)]; + } + } - return [-b / (2 * a)]; + return [-b / (2 * a)]; }; functions.push({ - name: "Homework 9 task #1", - func: hw9_f1, - params: [ - [3, 4, 5][(Math.random() * 3) >>> 0], - [3, 4, 5][(Math.random() * 3) >>> 0], - [3, 4, 5][(Math.random() * 3) >>> 0], - ], + name: "Homework 9 task #1", + func: hw9_f1, + params: [ + [3, 4, 5][(Math.random() * 3) >>> 0], + [3, 4, 5][(Math.random() * 3) >>> 0], + [3, 4, 5][(Math.random() * 3) >>> 0], + ], }); functions.push({ - name: "Homework 9 task #2", - func: hw9_f2, - params: [(Math.random() * 10) >>> 0], + name: "Homework 9 task #2", + func: hw9_f2, + params: [(Math.random() * 10) >>> 0], }); functions.push({ - name: "Homework 9 task #3", - func: hw9_f3, - params: [ - [3, -4, 54][(Math.random() * 3) >>> 0], - [3, 28, 6][(Math.random() * 3) >>> 0], - [3, -49, 0][(Math.random() * 3) >>> 0], - ], + name: "Homework 9 task #3", + func: hw9_f3, + params: [ + [3, -4, 54][(Math.random() * 3) >>> 0], + [3, 28, 6][(Math.random() * 3) >>> 0], + [3, -49, 0][(Math.random() * 3) >>> 0], + ], }); diff --git a/src/script/homework_9.test.js b/src/script/homework_9.test.js index 17525d6..5890083 100644 --- a/src/script/homework_9.test.js +++ b/src/script/homework_9.test.js @@ -1,79 +1,79 @@ import { hw9_f1, hw9_f2, hw9_f3 } from "./homework_9.js"; describe("homework 9, function 1 test", () => { - it("is function exists", () => { - expect(hw9_f1).toBeDefined(); - }); + it("is function exists", () => { + expect(hw9_f1).toBeDefined(); + }); - it("param 4, 5, 3", () => { - expect(hw9_f1(4, 5, 3).join("").split(" ")).not.toContain("not"); - }); + it("param 4, 5, 3", () => { + expect(hw9_f1(4, 5, 3).join("").split(" ")).not.toContain("not"); + }); - it("param 3, 4, 5", () => { - expect(hw9_f1(3, 4, 5).join("").split(" ")).not.toContain("not"); - }); + it("param 3, 4, 5", () => { + expect(hw9_f1(3, 4, 5).join("").split(" ")).not.toContain("not"); + }); - it("param 5, 4, 3", () => { - expect(hw9_f1(5, 4, 3).join("").split(" ")).not.toContain("not"); - }); + it("param 5, 4, 3", () => { + expect(hw9_f1(5, 4, 3).join("").split(" ")).not.toContain("not"); + }); - it("param 6, 5, 3", () => { - expect(hw9_f1(6, 5, 3).join("").split(" ")).toContain("not"); - }); + it("param 6, 5, 3", () => { + expect(hw9_f1(6, 5, 3).join("").split(" ")).toContain("not"); + }); - it("param 6, 4, 5", () => { - expect(hw9_f1(6, 4, 5).join("").split(" ")).toContain("not"); - }); + it("param 6, 4, 5", () => { + expect(hw9_f1(6, 4, 5).join("").split(" ")).toContain("not"); + }); - it("param 6, 4, 3", () => { - expect(hw9_f1(6, 4, 3).join("").split(" ")).toContain("not"); - }); + it("param 6, 4, 3", () => { + expect(hw9_f1(6, 4, 3).join("").split(" ")).toContain("not"); + }); - it("param -4, 5, 3", () => { - expect(hw9_f1(-4, 5, 3).join("").split(" ")).toContain("Error"); - }); + it("param -4, 5, 3", () => { + expect(hw9_f1(-4, 5, 3).join("").split(" ")).toContain("Error"); + }); - it("param 3, -4, 5", () => { - expect(hw9_f1(3, -4, 5).join("").split(" ")).toContain("Error"); - }); + it("param 3, -4, 5", () => { + expect(hw9_f1(3, -4, 5).join("").split(" ")).toContain("Error"); + }); - it("param 3, 4, -5", () => { - expect(hw9_f1(3, 4, -5).join("").split(" ")).toContain("Error"); - }); + it("param 3, 4, -5", () => { + expect(hw9_f1(3, 4, -5).join("").split(" ")).toContain("Error"); + }); }); describe("homework 8, function 2 test", () => { - it("is function exists", () => { - expect(hw9_f2).toBeDefined(); - }); + it("is function exists", () => { + expect(hw9_f2).toBeDefined(); + }); - it("param 3", () => { - expect(hw9_f2(3)).toEqual([Math.PI * 3 * 2, Math.PI * Math.pow(3 * 2, 2)]); - }); + it("param 3", () => { + expect(hw9_f2(3)).toEqual([Math.PI * 3 * 2, Math.PI * Math.pow(3 * 2, 2)]); + }); - it("param 5", () => { - expect(hw9_f2(5)).toEqual([Math.PI * 5 * 2, Math.PI * Math.pow(5 * 2, 2)]); - }); + it("param 5", () => { + expect(hw9_f2(5)).toEqual([Math.PI * 5 * 2, Math.PI * Math.pow(5 * 2, 2)]); + }); }); describe("homework 8, function 3 test", () => { - it("is function exists", () => { - expect(hw9_f3).toBeDefined(); - }); + it("is function exists", () => { + expect(hw9_f3).toBeDefined(); + }); - it("param 4, -1, -5", () => { - expect(hw9_f3(4, -1, -5)).toEqual([1.25, -1]); - }); + it("param 4, -1, -5", () => { + expect(hw9_f3(4, -1, -5)).toEqual([1.25, -1]); + }); - it("param 5, -6, -32", () => { - expect(hw9_f3(5, -6, -32)).toEqual([3.2, -2]); - }); + it("param 5, -6, -32", () => { + expect(hw9_f3(5, -6, -32)).toEqual([3.2, -2]); + }); - it("param 8, 0, 5", () => { - expect(hw9_f3(8, 0, 5).join("").split(" ")).toContain("no"); - }); + it("param 8, 0, 5", () => { + expect(hw9_f3(8, 0, 5).join("").split(" ")).toContain("no"); + }); - it("param -4, 28, -49", () => { - expect(hw9_f3(-4, 28, -49)).toEqual([3.5]); - }); + it("param -4, 28, -49", () => { + expect(hw9_f3(-4, 28, -49)).toEqual([3.5]); + }); }); diff --git a/src/script/ui.js b/src/script/ui.js index 1421618..2f2f924 100644 --- a/src/script/ui.js +++ b/src/script/ui.js @@ -1,99 +1,99 @@ import { - CHECKBOX_ID, - CHECKBOX_TEXT, - CHECKBOX_CLASS, - TASKS_CONTAINER_ID, - INFO_DIVS_CONTAINER_CLASS, - CONSOLE_CONTAINER_ID, - functions, + CHECKBOX_ID, + CHECKBOX_TEXT, + CHECKBOX_CLASS, + TASKS_CONTAINER_ID, + INFO_DIVS_CONTAINER_CLASS, + CONSOLE_CONTAINER_ID, + functions, } from "./const.js"; let fakeConsole; let outputConsole; export let addTask = (containerId, task, func) => { - if (containerId && task && func) { - let tasksDiv = document.getElementById(containerId); - let a = document.createElement("a"); - a.href = "#"; - a.text = task; - a.onclick = func; - tasksDiv.appendChild(a); - tasksDiv.appendChild(document.createElement("br")); - } + if (containerId && task && func) { + let tasksDiv = document.getElementById(containerId); + let a = document.createElement("a"); + a.href = "#"; + a.text = task; + a.onclick = func; + tasksDiv.appendChild(a); + tasksDiv.appendChild(document.createElement("br")); + } }; export function FakeConsole(containerId) { - this.container = document.getElementById(containerId); - this.clear = () => { - this.container.innerHTML = ""; - }; - this.log = (str) => { - this.container.innerHTML += str + "
"; - }; - this.isFake = () => { - return true; - }; - this.clear(); + this.container = document.getElementById(containerId); + this.clear = () => { + this.container.innerHTML = ""; + }; + this.log = (str) => { + this.container.innerHTML += str + "
"; + }; + this.isFake = () => { + return true; + }; + this.clear(); } export let createUI = (containerId) => { - let mainDiv = document.getElementById(containerId); + let mainDiv = document.getElementById(containerId); - let cbxConsole = document.createElement("input"); - cbxConsole.type = "checkbox"; - cbxConsole.checked = true; - cbxConsole.id = CHECKBOX_ID; - cbxConsole.addEventListener("click", (e) => { - outputConsole = e.currentTarget.checked ? fakeConsole : console; - outputConsole.log("!"); - }); + let cbxConsole = document.createElement("input"); + cbxConsole.type = "checkbox"; + cbxConsole.checked = true; + cbxConsole.id = CHECKBOX_ID; + cbxConsole.addEventListener("click", (e) => { + outputConsole = e.currentTarget.checked ? fakeConsole : console; + outputConsole.log("!"); + }); - let lblConsole = document.createElement("span"); - lblConsole.innerHTML = CHECKBOX_TEXT; + let lblConsole = document.createElement("span"); + lblConsole.innerHTML = CHECKBOX_TEXT; - let cbxDiv = document.createElement("div"); - cbxDiv.appendChild(cbxConsole); - cbxDiv.appendChild(lblConsole); - cbxDiv.className = CHECKBOX_CLASS; + let cbxDiv = document.createElement("div"); + cbxDiv.appendChild(cbxConsole); + cbxDiv.appendChild(lblConsole); + cbxDiv.className = CHECKBOX_CLASS; - let tasksDiv = document.createElement("div"); - tasksDiv.id = TASKS_CONTAINER_ID; - tasksDiv.className = INFO_DIVS_CONTAINER_CLASS; + let tasksDiv = document.createElement("div"); + tasksDiv.id = TASKS_CONTAINER_ID; + tasksDiv.className = INFO_DIVS_CONTAINER_CLASS; - let consoleDiv = document.createElement("div"); - consoleDiv.id = CONSOLE_CONTAINER_ID; - consoleDiv.className = INFO_DIVS_CONTAINER_CLASS; + let consoleDiv = document.createElement("div"); + consoleDiv.id = CONSOLE_CONTAINER_ID; + consoleDiv.className = INFO_DIVS_CONTAINER_CLASS; - mainDiv.appendChild(cbxDiv); - mainDiv.appendChild(tasksDiv); - mainDiv.appendChild(consoleDiv); + mainDiv.appendChild(cbxDiv); + mainDiv.appendChild(tasksDiv); + mainDiv.appendChild(consoleDiv); - fakeConsole = new FakeConsole(CONSOLE_CONTAINER_ID); - outputConsole = fakeConsole; + fakeConsole = new FakeConsole(CONSOLE_CONTAINER_ID); + outputConsole = fakeConsole; - functions.forEach((el) => { - addTask(TASKS_CONTAINER_ID, el.name, () => { - outputConsole.clear(); - if (el.params.length) { - let str = "Parameters: "; - el.params.forEach((el) => { - str += typeof el === "string" ? `"${el}" ` : `${el} `; - }); - outputConsole.log(str); - } - let str = "Results: "; - let fake = "isFake" in outputConsole; - el.func(...el.params).forEach((el) => { - if (!fake && el === "
") { - el = "\n"; - } - str += + functions.forEach((el) => { + addTask(TASKS_CONTAINER_ID, el.name, () => { + outputConsole.clear(); + if (el.params.length) { + let str = "Parameters: "; + el.params.forEach((el) => { + str += typeof el === "string" ? `"${el}" ` : `${el} `; + }); + outputConsole.log(str); + } + let str = "Results: "; + let fake = "isFake" in outputConsole; + el.func(...el.params).forEach((el) => { + if (!fake && el === "
") { + el = "\n"; + } + str += typeof el === "string" && el !== "
" && el !== "\n" - ? `"${el}" ` - : `${el} `; - }); - outputConsole.log(str); - }); - }); + ? `"${el}" ` + : `${el} `; + }); + outputConsole.log(str); + }); + }); }; diff --git a/src/script/ui.test.js b/src/script/ui.test.js index fef1b63..f138f26 100644 --- a/src/script/ui.test.js +++ b/src/script/ui.test.js @@ -2,83 +2,83 @@ import { createUI, addTask, FakeConsole } from "./ui.js"; import { functions } from "./const.js"; describe("task adding test", () => { - document.body.innerHTML = "
"; + document.body.innerHTML = "
"; - let container = document.getElementById("testDiv"); - let func = () => {}; + let container = document.getElementById("testDiv"); + let func = () => {}; - addTask("testDiv", "task0", func); - addTask("testDiv", "task1", func); - addTask("testDiv", "task2", func); + addTask("testDiv", "task0", func); + addTask("testDiv", "task1", func); + addTask("testDiv", "task2", func); - let tasks = container.querySelectorAll("a"); + let tasks = container.querySelectorAll("a"); - it("check added tasks", () => { - expect(container.querySelectorAll("a")).toHaveLength(3); - }); + it("check added tasks", () => { + expect(container.querySelectorAll("a")).toHaveLength(3); + }); - it("check tasks captions", () => { - expect(tasks[0].text).toEqual("task0"); - expect(tasks[1].text).toEqual("task1"); - expect(tasks[2].text).toEqual("task2"); - }); + it("check tasks captions", () => { + expect(tasks[0].text).toEqual("task0"); + expect(tasks[1].text).toEqual("task1"); + expect(tasks[2].text).toEqual("task2"); + }); - it("check tasks functions", () => { - expect(tasks[0].onclick).toEqual(func); - expect(tasks[1].onclick).toEqual(func); - expect(tasks[2].onclick).toEqual(func); - }); + it("check tasks functions", () => { + expect(tasks[0].onclick).toEqual(func); + expect(tasks[1].onclick).toEqual(func); + expect(tasks[2].onclick).toEqual(func); + }); }); describe("FakeConsole working test", () => { - document.body.innerHTML = "
"; + document.body.innerHTML = "
"; - let container = document.getElementById("testDiv"); - let myConsole = new FakeConsole("testDiv"); + let container = document.getElementById("testDiv"); + let myConsole = new FakeConsole("testDiv"); - it("check console creating", () => { - expect(myConsole).toBeDefined(); - }); + it("check console creating", () => { + expect(myConsole).toBeDefined(); + }); - it("check console container choosing", () => { - expect(myConsole.container).toEqual(container); - }); + it("check console container choosing", () => { + expect(myConsole.container).toEqual(container); + }); - it("check console printing", () => { - myConsole.log("test1!"); - myConsole.log("test2!"); - expect(container.innerHTML).toEqual("test1!
test2!
"); - }); + it("check console printing", () => { + myConsole.log("test1!"); + myConsole.log("test2!"); + expect(container.innerHTML).toEqual("test1!
test2!
"); + }); - it("check console erasing", () => { - myConsole.clear(); - expect(container.innerHTML).toEqual(""); - }); + it("check console erasing", () => { + myConsole.clear(); + expect(container.innerHTML).toEqual(""); + }); }); describe("UI creation test", () => { - document.body.innerHTML = "
"; + document.body.innerHTML = "
"; - functions[0] = { name: "task0", func: () => {}, params: [] }; - functions[1] = { name: "task1", func: () => {}, params: [] }; + functions[0] = { name: "task0", func: () => {}, params: [] }; + functions[1] = { name: "task1", func: () => {}, params: [] }; - createUI("testDiv"); + createUI("testDiv"); - let container = document.getElementById("testDiv"); + let container = document.getElementById("testDiv"); - it("check divs", () => { - expect(container.querySelectorAll("div")).toHaveLength(3); - }); + it("check divs", () => { + expect(container.querySelectorAll("div")).toHaveLength(3); + }); - it("check inputs", () => { - expect(container.querySelectorAll("input")).toHaveLength(1); - }); + it("check inputs", () => { + expect(container.querySelectorAll("input")).toHaveLength(1); + }); - it("check spans", () => { - expect(container.querySelectorAll("span")).toHaveLength(1); - }); + it("check spans", () => { + expect(container.querySelectorAll("span")).toHaveLength(1); + }); - it("check links, created from tasks", () => { - expect(container.querySelectorAll("a")).toHaveLength(2); - }); + it("check links, created from tasks", () => { + expect(container.querySelectorAll("a")).toHaveLength(2); + }); }); From d20396750872b078e34702e51cf1ae088b404ac2 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 3 Jun 2022 06:54:02 +0300 Subject: [PATCH 3/4] doing it more easier --- src/css/style.css | 54 ------- src/index.html | 32 ++-- src/script/const.js | 12 -- src/script/homework_1.js | 52 ------- src/script/homework_1.test.js | 71 --------- src/script/homework_1/homework_1.js | 15 ++ src/script/homework_1/homework_1.test.js | 79 ++++++++++ src/script/homework_10.js | 39 ----- src/script/homework_10.test.js | 59 -------- src/script/homework_10/homework_10.js | 24 +++ src/script/homework_10/homework_10.test.js | 138 ++++++++++++++++++ src/script/homework_2.js | 57 -------- src/script/homework_2.test.js | 85 ----------- src/script/homework_2/homework_2.js | 40 +++++ src/script/homework_2/homework_2.test.js | 95 ++++++++++++ src/script/homework_3.js | 41 ------ src/script/homework_3/homework_3.js | 35 +++++ .../{ => homework_3}/homework_3.test.js | 50 +++---- src/script/homework_4.js | 41 ------ src/script/homework_4/homework_4.js | 25 ++++ .../{ => homework_4}/homework_4.test.js | 64 ++++---- src/script/homework_5.js | 52 ------- src/script/homework_5.test.js | 79 ---------- src/script/homework_5/homework_5.js | 21 +++ src/script/homework_5/homework_5.test.js | 54 +++++++ src/script/homework_6.js | 43 ------ src/script/homework_6.test.js | 92 ------------ src/script/homework_6/homework_6.js | 15 ++ src/script/homework_6/homework_6.test.js | 59 ++++++++ src/script/{ => homework_7}/homework_7.js | 15 +- .../{ => homework_7}/homework_7.test.js | 6 + src/script/homework_8.js | 52 ------- src/script/homework_8.test.js | 83 ----------- src/script/homework_8/homework_8.js | 29 ++++ src/script/homework_8/homework_8.test.js | 81 ++++++++++ src/script/homework_9.js | 71 --------- src/script/homework_9.test.js | 79 ---------- src/script/homework_9/homework_9.js | 40 +++++ src/script/homework_9/homework_9.test.js | 129 ++++++++++++++++ src/script/ui.js | 99 ------------- src/script/ui.test.js | 84 ----------- 41 files changed, 950 insertions(+), 1341 deletions(-) delete mode 100644 src/css/style.css delete mode 100644 src/script/const.js delete mode 100644 src/script/homework_1.js delete mode 100644 src/script/homework_1.test.js create mode 100644 src/script/homework_1/homework_1.js create mode 100644 src/script/homework_1/homework_1.test.js delete mode 100644 src/script/homework_10.js delete mode 100644 src/script/homework_10.test.js create mode 100644 src/script/homework_10/homework_10.js create mode 100644 src/script/homework_10/homework_10.test.js delete mode 100644 src/script/homework_2.js delete mode 100644 src/script/homework_2.test.js create mode 100644 src/script/homework_2/homework_2.js create mode 100644 src/script/homework_2/homework_2.test.js delete mode 100644 src/script/homework_3.js create mode 100644 src/script/homework_3/homework_3.js rename src/script/{ => homework_3}/homework_3.test.js (54%) delete mode 100644 src/script/homework_4.js create mode 100644 src/script/homework_4/homework_4.js rename src/script/{ => homework_4}/homework_4.test.js (56%) delete mode 100644 src/script/homework_5.js delete mode 100644 src/script/homework_5.test.js create mode 100644 src/script/homework_5/homework_5.js create mode 100644 src/script/homework_5/homework_5.test.js delete mode 100644 src/script/homework_6.js delete mode 100644 src/script/homework_6.test.js create mode 100644 src/script/homework_6/homework_6.js create mode 100644 src/script/homework_6/homework_6.test.js rename src/script/{ => homework_7}/homework_7.js (81%) rename src/script/{ => homework_7}/homework_7.test.js (90%) delete mode 100644 src/script/homework_8.js delete mode 100644 src/script/homework_8.test.js create mode 100644 src/script/homework_8/homework_8.js create mode 100644 src/script/homework_8/homework_8.test.js delete mode 100644 src/script/homework_9.js delete mode 100644 src/script/homework_9.test.js create mode 100644 src/script/homework_9/homework_9.js create mode 100644 src/script/homework_9/homework_9.test.js delete mode 100644 src/script/ui.js delete mode 100644 src/script/ui.test.js diff --git a/src/css/style.css b/src/css/style.css deleted file mode 100644 index bfc9b47..0000000 --- a/src/css/style.css +++ /dev/null @@ -1,54 +0,0 @@ -:root { - font-size: 16px; -} - -* { - box-sizing: border-box; - margin: 0; - padding: 0; -} - -html, -body { - background-color: white; - color: black; - line-height: 1.2; -} - -.cbxDiv { - position: relative; - left: 0; - right: 0; - margin: auto; - display: block; - text-align: center; -} - -.infoDiv { - height: 600px; - border: solid 1px black; - width: 50%; - display: block; - float: left; - padding: 5px; -} - -.btns { - width: 100px; - height: 25px; - display: none; - left: 0; - right: 0; - margin: auto; - margin-top: 5px; -} - -.edits { - width: 100px; - height: 25px; - display: block; - left: 0; - right: 0; - margin: auto; - margin-top: 5px; -} diff --git a/src/index.html b/src/index.html index eafc434..1682605 100644 --- a/src/index.html +++ b/src/index.html @@ -5,28 +5,16 @@ Homework for lection #9 - - - - - - - - - - - - - - - + + + + + + + + + + diff --git a/src/script/const.js b/src/script/const.js deleted file mode 100644 index 372af0b..0000000 --- a/src/script/const.js +++ /dev/null @@ -1,12 +0,0 @@ -export const MAIN_CONTAINER_ID = "mainContainer"; -export const CHECKBOX_TEXT = " Output to fake console"; -export const CHECKBOX_CLASS = "cbxDiv"; -export const CHECKBOX_ID = "cbxConsole"; -export const TASKS_CONTAINER_ID = "divTasksList"; -export const INFO_DIVS_CONTAINER_CLASS = "infoDiv"; -export const CONSOLE_CONTAINER_ID = "divConsoleText"; -export const TASK7_MAIN_CONTAINER_ID = "task7id"; -export const TASK7_BTN_CLASS_NAME = "btns"; -export const TASK7_EDIT_CLASS_NAME = "edits"; - -export const functions = []; diff --git a/src/script/homework_1.js b/src/script/homework_1.js deleted file mode 100644 index 4aa1376..0000000 --- a/src/script/homework_1.js +++ /dev/null @@ -1,52 +0,0 @@ -import { functions } from "./const.js"; - -export let hw1_f1 = (a, b) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); - return [a * b, a + b]; -}; - -export let hw1_f2 = (str1, str2) => { - if (!str1) { - str1 = ""; - } - if (!str2) { - str2 = ""; - } - return [str1.length + str2.length]; -}; - -export let hw1_f3 = (str) => { - if (!str) { - str = "000"; - } - - let a = isNaN(Number(str[0])) ? 0 : Number(str[0]); - let b = isNaN(Number(str[1])) ? 0 : Number(str[1]); - let c = isNaN(Number(str[2])) ? 0 : Number(str[2]); - - return [a + b + c]; -}; - -functions.push({ - name: "Homework 1 task #1", - func: hw1_f1, - params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], -}); -functions.push({ - name: "Homework 1 task #2", - func: hw1_f2, - params: [ - Math.random() - .toFixed(Math.random() * 10) - .toString(), - Math.random() - .toFixed(Math.random() * 10) - .toString(), - ], -}); -functions.push({ - name: "Homework 1 task #3", - func: hw1_f3, - params: [Math.random().toFixed(3).slice(2).toString()], -}); diff --git a/src/script/homework_1.test.js b/src/script/homework_1.test.js deleted file mode 100644 index 6cf3a79..0000000 --- a/src/script/homework_1.test.js +++ /dev/null @@ -1,71 +0,0 @@ -import { hw1_f1, hw1_f2, hw1_f3 } from "./homework_1.js"; - -describe("homework 1, function 1 test", () => { - it("is function exists", () => { - expect(hw1_f1).toBeDefined(); - }); - - it("param 5 and 4", () => { - expect(hw1_f1(5, 4)).toEqual([20, 9]); - }); - it("param 9 and 9", () => { - expect(hw1_f1(9, 9)).toEqual([81, 18]); - }); - it("param null and null", () => { - expect(hw1_f1(null, null)).toEqual([0, 0]); - }); - it("param undefined and undefined", () => { - expect(hw1_f1(undefined, undefined)).toEqual([0, 0]); - }); - it("param empty string and empty string", () => { - expect(hw1_f1("", "")).toEqual([0, 0]); - }); -}); - -describe("homework 1, function 2 test", () => { - it("is function exists", () => { - expect(hw1_f2).toBeDefined(); - }); - - it("param '123' and '1234", () => { - expect(hw1_f2("123", "1234")).toEqual([7]); - }); - - it("param null and null", () => { - expect(hw1_f2(null, null)).toEqual([0]); - }); - - it("param undefined and undefined", () => { - expect(hw1_f2(undefined, undefined)).toEqual([0]); - }); - - it("param empty string and empty string", () => { - expect(hw1_f2("", "")).toEqual([0]); - }); -}); - -describe("homework 1, function 3 test", () => { - it("is function exists", () => { - expect(hw1_f3).toBeDefined(); - }); - - it("param '123'", () => { - expect(hw1_f3("123")).toEqual([6]); - }); - - it("param '1234'", () => { - expect(hw1_f3("1234")).toEqual([6]); - }); - - it("param null", () => { - expect(hw1_f3(null)).toEqual([0]); - }); - - it("param undefined", () => { - expect(hw1_f3(undefined)).toEqual([0]); - }); - - it("param empty string and empty string", () => { - expect(hw1_f3("")).toEqual([0]); - }); -}); diff --git a/src/script/homework_1/homework_1.js b/src/script/homework_1/homework_1.js new file mode 100644 index 0000000..bf626c1 --- /dev/null +++ b/src/script/homework_1/homework_1.js @@ -0,0 +1,15 @@ +//даны два числа, вывести в консоль их произведение и сумму +export let hw1_f1 = (a = 0, b = 0) => { + console.log(a * b, a + b); +}; + +//даны две строки, вывести в консоль сумму их длинн +export let hw1_f2 = (str1 = "", str2 = "") => { + console.log(str1.length + str2.length); +}; + +//запросить у пользователя число, вывести в консоль сумму первых трёх цифр +export let hw1_f3 = () => { + let str = window.prompt("please enter the free-digit number"); + console.log(Number(str[0]) + Number(str[1]) + Number(str[2])); +}; diff --git a/src/script/homework_1/homework_1.test.js b/src/script/homework_1/homework_1.test.js new file mode 100644 index 0000000..405b7fa --- /dev/null +++ b/src/script/homework_1/homework_1.test.js @@ -0,0 +1,79 @@ +import { hw1_f1, hw1_f2, hw1_f3 } from "./homework_1.js"; + +describe("homework 1, function 1 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw1_f1).toBeDefined(); + }); + + it("param 5 and 4", () => { + hw1_f1(5, 4); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(20, 9); + }); + + it("param 9 and 9", () => { + hw1_f1(9, 9); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(81, 18); + }); + + it("w/o params", () => { + hw1_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(0, 0); + }); +}); + +describe("homework 1, function 2 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw1_f2).toBeDefined(); + }); + + it("param '123' and '1234", () => { + hw1_f2("123", "1234"); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(7); + }); + + it("param empty string and empty string", () => { + hw1_f2("", ""); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(0); + }); + + it("w/o params", () => { + hw1_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(0); + }); +}); + +describe("homework 1, function 3 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw1_f3).toBeDefined(); + }); + + it("param '123'", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "123"; + }); + hw1_f3("123"); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(6); + }); + + it("param '123'", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "456"; + }); + hw1_f3("456"); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(15); + }); +}); diff --git a/src/script/homework_10.js b/src/script/homework_10.js deleted file mode 100644 index a3100e6..0000000 --- a/src/script/homework_10.js +++ /dev/null @@ -1,39 +0,0 @@ -import { functions } from "./const.js"; - -export let hw10_f1 = (str) => { - if ( - /^[a-zA-Z]{1}[a-zA-Z0-9_-]+[a-zA-Z0-9]{1}@[a-zA-Z0-9]{1}[a-zA-Z0-9-]+[a-zA-Z0-9]{1}\.[a-zA-Z]{2,}$/.test( - str - ) - ) { - return ["mail"]; - } - - if (/^(((\+7)|8))[89][0-9]{9}$/.test(str)) { - return ["phone number"]; - } - - if (/^([0-9]{1,2}\.[0-9]{1,2}\.)([0-9]{4}|[0-9]{2})$/.test(str)) { - return ["date"]; - } - - return ["not correct date or phone number or mail"]; -}; - -functions.push({ - name: "Homework 10 task #1", - func: hw10_f1, - params: [ - [ - "mail@mail.ru", - "89009998877", - "11.12.2012", - "mail@mail", - "8900999887", - "+79009998877", - "+78009998877", - "11.12.213", - "11.12.13", - ][(Math.random() * 9) >>> 0], - ], -}); diff --git a/src/script/homework_10.test.js b/src/script/homework_10.test.js deleted file mode 100644 index da89a07..0000000 --- a/src/script/homework_10.test.js +++ /dev/null @@ -1,59 +0,0 @@ -import { hw10_f1 } from "./homework_10.js"; - -describe("homework 10, function 1 test", () => { - it("is function exists", () => { - expect(hw10_f1).toBeDefined(); - }); - - it("param 22.10.10", () => { - expect(hw10_f1("22.10.10")).toEqual(["date"]); - }); - - it("param 22.10.1910", () => { - expect(hw10_f1("22.10.1910")).toEqual(["date"]); - }); - - it("param aaa99@bbb-9.cc", () => { - expect(hw10_f1("aaa99@bbb-9.cc")).toEqual(["mail"]); - }); - - it("param aaa99.bbb-.cc", () => { - expect(hw10_f1("aaa99.bbb-9.cc").join("").split(" ")).toContain("not"); - }); - - it("param 22.10.123", () => { - expect(hw10_f1("22.10.123").join("").split(" ")).toContain("not"); - }); - - it("param aaa_2@bbb.cc", () => { - expect(hw10_f1("aaa_2@bbb.cc")).toEqual(["mail"]); - }); - - it("param aaa_2@bbb.cc", () => { - expect(hw10_f1("aaa_@bbb.cc").join("").split(" ")).toContain("not"); - }); - - it("param +69009998877", () => { - expect(hw10_f1("+69009998877").join("").split(" ")).toContain("not"); - }); - - it("param +79009998877", () => { - expect(hw10_f1("+79009998877")).toEqual(["phone number"]); - }); - - it("param 89009998877", () => { - expect(hw10_f1("89009998877")).toEqual(["phone number"]); - }); - - it("param 88009998877", () => { - expect(hw10_f1("88009998877")).toEqual(["phone number"]); - }); - - it("param 78009998877", () => { - expect(hw10_f1("78009998877").join("").split(" ")).toContain("not"); - }); - - it("param 87009998877", () => { - expect(hw10_f1("87009998877").join("").split(" ")).toContain("not"); - }); -}); diff --git a/src/script/homework_10/homework_10.js b/src/script/homework_10/homework_10.js new file mode 100644 index 0000000..cad8837 --- /dev/null +++ b/src/script/homework_10/homework_10.js @@ -0,0 +1,24 @@ +//определить, чем является введенная строка - датой, адресом электронной почты или номером телефона +export let hw10_f1 = () => { + let str = window.prompt("please enter the phone number/mail/date"); + if ( + /^[a-zA-Z]{1}[a-zA-Z0-9_-]+[a-zA-Z0-9]{1}@[a-zA-Z0-9]{1}[a-zA-Z0-9-]+[a-zA-Z0-9]{1}\.[a-zA-Z]{2,}$/.test( + str + ) + ) { + console.log("mail"); + return; + } + + if (/^(((\+7)|8))[89][0-9]{9}$/.test(str)) { + console.log("phone number"); + return; + } + + if (/^([0-9]{1,2}\.[0-9]{1,2}\.)([0-9]{4}|[0-9]{2})$/.test(str)) { + console.log("date"); + return; + } + + console.log("not correct date or phone number or mail"); +}; diff --git a/src/script/homework_10/homework_10.test.js b/src/script/homework_10/homework_10.test.js new file mode 100644 index 0000000..5a1f80c --- /dev/null +++ b/src/script/homework_10/homework_10.test.js @@ -0,0 +1,138 @@ +import { hw10_f1 } from "./homework_10.js"; + +describe("homework 10, function 1 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw10_f1).toBeDefined(); + }); + + it("param 22.10.10", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "22.10.10"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("date"); + }); + + it("param 22.10.1910", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "22.10.1910"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("date"); + }); + + it("param aaa99@bbb-9.cc", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "aaa99@bbb-9.cc"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("mail"); + }); + + it("param aaa99.bbb-.cc", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "aaa99.bbb-9.cc"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + "not correct date or phone number or mail" + ); + }); + + it("param 22.10.123", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "22.10.123"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + "not correct date or phone number or mail" + ); + }); + + it("param aaa_2@bbb.cc", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "aaa_2@bbb.cc"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("mail"); + }); + + it("param aaa_2@bbb.cc", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "aaa_@bbb.cc"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + "not correct date or phone number or mail" + ); + }); + + it("param +69009998877", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "+69009998877"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + "not correct date or phone number or mail" + ); + }); + + it("param +79009998877", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "+79009998877"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("phone number"); + }); + + it("param 89009998877", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "89009998877"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("phone number"); + }); + + it("param 88009998877", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "88009998877"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("phone number"); + }); + + it("param 78009998877", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "78009998877"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + "not correct date or phone number or mail" + ); + }); + + it("param 87009998877", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "87009998877"; + }); + hw10_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + "not correct date or phone number or mail" + ); + }); +}); diff --git a/src/script/homework_2.js b/src/script/homework_2.js deleted file mode 100644 index 8ff7fa3..0000000 --- a/src/script/homework_2.js +++ /dev/null @@ -1,57 +0,0 @@ -import { functions } from "./const.js"; - -export let hw2_f1 = (a, b) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); - - return [Math.max(a, b)]; -}; - -export let hw2_f2 = () => { - let m = prompt("Please enter the number of month (1..12)"); - m = isNaN(Number(m)) ? 1 : Number(m); - if (m > 12) { - m = 12; - } - if (m < 1) { - m = 1; - } - let months = [ - "Jan", - "Feb", - "Mar", - "Apr", - "May", - "Jun", - "Jul", - "Aug", - "Sep", - "Oct", - "Nov", - "Dec", - ]; - return [months[m - 1]]; -}; - -export let hw2_f3 = (circle, square) => { - square = isNaN(Number(square)) ? 0 : Number(square); - circle = isNaN(Number(circle)) ? 0 : Number(circle); - - let r = circle / (Math.PI * 2); - return r <= Math.sqrt(square) / 2 ? ["fitted"] : ["not fitted"]; -}; - -functions.push({ - name: "Homework 2 task #1", - func: hw2_f1, - params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], -}); -functions.push({ name: "Homework 2 task #2", func: hw2_f2, params: [] }); -functions.push({ - name: "Homework 2 task #3", - func: hw2_f3, - params: [ - [18.85, 18][(Math.random() * 2) >>> 0], - [36.1, 25][(Math.random() * 2) >> 0], - ], -}); diff --git a/src/script/homework_2.test.js b/src/script/homework_2.test.js deleted file mode 100644 index c2b4a73..0000000 --- a/src/script/homework_2.test.js +++ /dev/null @@ -1,85 +0,0 @@ -import { hw2_f1, hw2_f2, hw2_f3 } from "./homework_2.js"; - -describe("homework 2, function 1 test", () => { - it("is function exists", () => { - expect(hw2_f1).toBeDefined(); - }); - - it("param 5 and 4", () => { - expect(hw2_f1(5, 4)).toEqual([5]); - }); - - it("param 7 and 8", () => { - expect(hw2_f1(7, 8)).toEqual([8]); - }); - - it("param null and null", () => { - expect(hw2_f1(null, null)).toEqual([0]); - }); - it("param undefined and undefined", () => { - expect(hw2_f1(undefined, undefined)).toEqual([0]); - }); - it("param empty string and empty string", () => { - expect(hw2_f1("", "")).toEqual([0]); - }); -}); - -describe("homework 2, function 2 test", () => { - it("is function exists", () => { - expect(hw2_f2).toBeDefined(); - }); - - it("prompted 1", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "1"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted 6", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "6"); - expect(hw2_f2()).toEqual(["Jun"]); - }); - - it("prompted 12", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "12"); - expect(hw2_f2()).toEqual(["Dec"]); - }); - - it("prompted 13", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "13"); - expect(hw2_f2()).toEqual(["Dec"]); - }); - - it("prompted 0", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "0"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted null", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "null"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted undefined", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); - expect(hw2_f2()).toEqual(["Jan"]); - }); - - it("prompted empty string", () => { - jest.spyOn(window, "prompt").mockImplementation(() => ""); - expect(hw2_f2()).toEqual(["Jan"]); - }); -}); - -describe("homework 2, function 3 test", () => { - it("is function exists", () => { - expect(hw2_f3).toBeDefined(); - }); - - it("param 16 36", () => { - expect(hw2_f3(16, 36)).toEqual(["fitted"]); - }); - - it("param 36 16", () => { - expect(hw2_f3(36, 16)).toEqual(["not fitted"]); - }); -}); diff --git a/src/script/homework_2/homework_2.js b/src/script/homework_2/homework_2.js new file mode 100644 index 0000000..1881a85 --- /dev/null +++ b/src/script/homework_2/homework_2.js @@ -0,0 +1,40 @@ +//Даны два числа, вывести в консоль максимальное из них +export let hw2_f1 = (a = 0, b = 0) => { + console.log(Math.max(a, b)); +}; + +//пользователь вводит номер месяца от 1 до 12, вывести в консоль его название +export let hw2_f2 = () => { + let m = window.prompt("Please enter the number of month (1..12)"); + m = Number(m); + if (m > 12) { + m = 12; + } + if (m < 1) { + m = 1; + } + let months = [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec", + ]; + console.log(months[m - 1]); +}; + +//даны площадь круга и квадрата, определить можно ли вписать круг в квадрат +export let hw2_f3 = (circle = 1, square = 1) => { + square = Number(square); + circle = Number(circle); + + let r = circle / (Math.PI * 2); + console.log(r <= Math.sqrt(square) / 2 ? "fitted" : "not fitted"); +}; diff --git a/src/script/homework_2/homework_2.test.js b/src/script/homework_2/homework_2.test.js new file mode 100644 index 0000000..cffdfbb --- /dev/null +++ b/src/script/homework_2/homework_2.test.js @@ -0,0 +1,95 @@ +import { hw2_f1, hw2_f2, hw2_f3 } from "./homework_2.js"; + +describe("homework 2, function 1 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw2_f1).toBeDefined(); + }); + + it("param 5 and 4", () => { + hw2_f1(5, 4); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(5); + }); + + it("param 7 and 8", () => { + hw2_f1(7, 8); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(8); + }); + + it("w/o params", () => { + hw2_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(0); + }); +}); + +describe("homework 2, function 2 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw2_f2).toBeDefined(); + }); + + it("prompted 1", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "1"); + hw2_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("Jan"); + }); + + it("prompted 6", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "6"); + hw2_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("Jun"); + }); + + it("prompted 12", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "12"); + hw2_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("Dec"); + }); + + it("prompted 13", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "13"); + hw2_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("Dec"); + }); + + it("prompted 0", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "0"); + hw2_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("Jan"); + }); +}); + +describe("homework 2, function 3 test", () => { + console.log = jest.fn(); + it("is function exists", () => { + expect(hw2_f3).toBeDefined(); + }); + + it("param 16 36", () => { + hw2_f3(16, 36); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("fitted"); + }); + + it("param 36 16", () => { + hw2_f3(36, 16); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("not fitted"); + }); + + it("w/o params", () => { + hw2_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("fitted"); + }); +}); diff --git a/src/script/homework_3.js b/src/script/homework_3.js deleted file mode 100644 index 19bc050..0000000 --- a/src/script/homework_3.js +++ /dev/null @@ -1,41 +0,0 @@ -import { functions } from "./const.js"; - -export let hw3_f1 = () => { - let sum = 0; - for (let i = 50; i <= 100; i++) { - sum += i; - } - return [sum]; -}; - -export let hw3_f2 = () => { - let result = []; - for (let i = 1; i < 10; i++) { - result.push("
"); - result.push(`7 x ${i} = ${7 * i}`); - } - return result; -}; - -export let hw3_f3 = () => { - let n = prompt("Please enter the number N"); - n = isNaN(Number(n)) ? 0 : Number(n); - - if (n < 1) { - return [0]; - } - - let sum = 0; - let cnt = 0; - for (let i = 1; i <= n; i++) { - if (i & 0x01) { - cnt++; - sum += i; - } - } - return [sum / cnt]; -}; - -functions.push({ name: "Homework 3 task #1", func: hw3_f1, params: [] }); -functions.push({ name: "Homework 3 task #2", func: hw3_f2, params: [] }); -functions.push({ name: "Homework 3 task #3", func: hw3_f3, params: [] }); diff --git a/src/script/homework_3/homework_3.js b/src/script/homework_3/homework_3.js new file mode 100644 index 0000000..7fa07f0 --- /dev/null +++ b/src/script/homework_3/homework_3.js @@ -0,0 +1,35 @@ +//вывести в консоль сумму чисел от 50 до 100 +export let hw3_f1 = () => { + let sum = 0; + for (let i = 50; i <= 100; i++) { + sum += i; + } + console.log(sum); +}; + +//вывести в консоль таблицу умножения на 7 +export let hw3_f2 = () => { + for (let i = 1; i < 10; i++) { + console.log(`7 x ${i} = ${7 * i}`); + } +}; + +//запросить у пользователя число N и вывести среднее арифметическое суммы всех нечетных чисел от 1 до N +export let hw3_f3 = () => { + let n = window.prompt("Please enter the number N"); + + if (n < 1) { + console.log(0); + return; + } + + let sum = 0; + let cnt = 0; + for (let i = 1; i <= n; i++) { + if (i & 0x01) { + cnt++; + sum += i; + } + } + console.log(sum / cnt); +}; diff --git a/src/script/homework_3.test.js b/src/script/homework_3/homework_3.test.js similarity index 54% rename from src/script/homework_3.test.js rename to src/script/homework_3/homework_3.test.js index ba2a028..59d525b 100644 --- a/src/script/homework_3.test.js +++ b/src/script/homework_3/homework_3.test.js @@ -1,6 +1,7 @@ import { hw3_f1, hw3_f2, hw3_f3 } from "./homework_3.js"; describe("homework 3, function 1 test", () => { + console.log = jest.fn(); it("is function exists", () => { expect(hw3_f1).toBeDefined(); }); @@ -10,11 +11,14 @@ describe("homework 3, function 1 test", () => { for (let i = 100; i >= 50; i--) { sum += i; } - expect(hw3_f1()).toEqual([sum]); + hw3_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(sum); }); }); describe("homework 3, function 2 test", () => { + console.log = jest.fn(); it("is function exists", () => { expect(hw3_f2).toBeDefined(); }); @@ -24,45 +28,37 @@ describe("homework 3, function 2 test", () => { for (let i = 1; i < 10; i++) { check.push(i * 7); } - let rslt = hw3_f2() - .join("") - .split("
") - .map((el) => { - return Number(el.split("=").pop().trim()); - }) - .slice(1); - expect(check).toEqual(rslt); + hw3_f2(); + for (let i = 0; i < check.length; i++) { + expect(console.log).toHaveBeenNthCalledWith( + i + 1, + `7 x ${i + 1} = ${check[i]}` + ); + } + expect(console.log).toHaveBeenCalledTimes(9); }); }); describe("homework 3, function 3 test", () => { + console.log = jest.fn(); it("prompted 3", () => { jest.spyOn(window, "prompt").mockImplementation(() => "3"); - expect(hw3_f3()).toEqual([2]); + hw3_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(2); }); it("prompted 9", () => { jest.spyOn(window, "prompt").mockImplementation(() => "9"); - expect(hw3_f3()).toEqual([5]); + hw3_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(5); }); it("prompted 0", () => { jest.spyOn(window, "prompt").mockImplementation(() => "0"); - expect(hw3_f3()).toEqual([0]); - }); - - it("prompted null", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "null"); - expect(hw3_f3()).toEqual([0]); - }); - - it("prompted undefined", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); - expect(hw3_f3()).toEqual([0]); - }); - - it("prompted empty string", () => { - jest.spyOn(window, "prompt").mockImplementation(() => ""); - expect(hw3_f3()).toEqual([0]); + hw3_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(0); }); }); diff --git a/src/script/homework_4.js b/src/script/homework_4.js deleted file mode 100644 index c101734..0000000 --- a/src/script/homework_4.js +++ /dev/null @@ -1,41 +0,0 @@ -import { functions } from "./const.js"; - -let user = {}; -let admin = {}; - -export let hw4_f1 = () => { - let n = prompt("Please enter the age of user"); - n = isNaN(Number(n)) ? 0 : Number(n); - user.name = "John"; - user.age = n; - - let rslt = []; - rslt.push(user); - rslt.push("
"); - rslt.push(`user.name = ${user.name}, user.age = ${user.age}`); - return rslt; -}; - -export let hw4_f2 = () => { - admin = Object.assign({}, user); - admin.role = "admin"; - - let rslt = []; - rslt.push(admin); - rslt.push("
"); - rslt.push( - `admin.name = ${user.name}, admin.age = ${user.age}, admin.role = ${admin.role}` - ); - return rslt; -}; - -export let hw4_f3 = () => { - for (let prop in admin) { - window[prop] = admin[prop]; - } - return ["Variables are created in Window object"]; -}; - -functions.push({ name: "Homework 4 task #1", func: hw4_f1, params: [] }); -functions.push({ name: "Homework 4 task #2", func: hw4_f2, params: [] }); -functions.push({ name: "Homework 4 task #3", func: hw4_f3, params: [] }); diff --git a/src/script/homework_4/homework_4.js b/src/script/homework_4/homework_4.js new file mode 100644 index 0000000..f7f4369 --- /dev/null +++ b/src/script/homework_4/homework_4.js @@ -0,0 +1,25 @@ +export let user = {}; +export let admin = {}; + +//завести объект с полем name = John, запросить у пользователя число и присвоить +//введенное значение полю age этого объекта +export let hw4_f1 = () => { + let n = window.prompt("Please enter the age of user"); + user.name = "John"; + user.age = n; +}; + +//создать объект admin, копию обьекта user и расширить его полем role +//со значением admin +export let hw4_f2 = () => { + admin = Object.assign({}, user); + admin.role = "admin"; +}; + +//записать все значения полей обьекта admin в переменные с такими же +//названиями, переменные предварительно создать +export let hw4_f3 = () => { + for (let prop in admin) { + window[prop] = admin[prop]; + } +}; diff --git a/src/script/homework_4.test.js b/src/script/homework_4/homework_4.test.js similarity index 56% rename from src/script/homework_4.test.js rename to src/script/homework_4/homework_4.test.js index ff101f5..c7ee5ac 100644 --- a/src/script/homework_4.test.js +++ b/src/script/homework_4/homework_4.test.js @@ -1,4 +1,4 @@ -import { hw4_f1, hw4_f2, hw4_f3 } from "./homework_4.js"; +import { hw4_f1, hw4_f2, hw4_f3, user, admin } from "./homework_4.js"; describe("homework 4, function 1 test", () => { jest.spyOn(window, "prompt").mockImplementation(() => "23"); @@ -7,88 +7,82 @@ describe("homework 4, function 1 test", () => { expect(hw4_f1).toBeDefined(); }); + hw4_f1(); + + it("is user object exists", () => { + expect(user).toBeDefined(); + }); + it("check user object", () => { - expect(typeof hw4_f1()[0]).toEqual("object"); + expect(typeof user).toEqual("object"); }); it("check user name exists", () => { - expect(hw4_f1()[0].name).toBeDefined(); + expect(user.name).toBeDefined(); }); it("check user name value", () => { - expect(hw4_f1()[0].name).toEqual("John"); + expect(user.name).toEqual("John"); }); it("check user age exists", () => { - expect(hw4_f1()[0].age).toBeDefined(); + expect(user.age).toBeDefined(); }); it("check user age value", () => { - expect(hw4_f1()[0].age).toEqual(23); + expect(user.age).toEqual("23"); }); it("check user age modified", () => { jest.spyOn(window, "prompt").mockImplementation(() => "21"); - expect(hw4_f1()[0].age).toEqual(21); - }); - - it("check user age null prompted", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "null"); - expect(hw4_f1()[0].age).toEqual(0); - }); - - it("check user age undefined prompted", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "undefined"); - expect(hw4_f1()[0].age).toEqual(0); - }); - - it("check user age empty string prompted", () => { - jest.spyOn(window, "prompt").mockImplementation(() => ""); - expect(hw4_f1()[0].age).toEqual(0); + hw4_f1(); + expect(user.age).toEqual("21"); }); }); describe("homework 3, function 2 test", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "23"); - let user = hw4_f1()[0]; - it("is function exists", () => { expect(hw4_f2).toBeDefined(); }); + it("is admin object exists", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "23"); + hw4_f1(); + hw4_f2(); + expect(admin).toBeDefined(); + }); + it("check admin object", () => { - expect(typeof hw4_f2()[0]).toEqual("object"); + expect(typeof admin).toEqual("object"); }); it("check admin name exists", () => { - expect(hw4_f2()[0].name).toBeDefined(); + expect(admin.name).toBeDefined(); }); it("check that admin name is equal to user name", () => { - expect(hw4_f2()[0].name).toEqual(user.name); + expect(admin.name).toEqual(user.name); }); it("check admin age exists", () => { - expect(hw4_f2()[0].age).toBeDefined(); + expect(admin.age).toBeDefined(); }); it("check admin age value", () => { - expect(hw4_f2()[0].age).toEqual(user.age); + expect(admin.age).toEqual(user.age); }); it("check admin role exists", () => { - expect(hw4_f2()[0].role).toBeDefined(); + expect(admin.role).toBeDefined(); }); it("check admin role value", () => { - expect(hw4_f2()[0].role).toEqual("admin"); + expect(admin.role).toEqual("admin"); }); }); describe("homework 3, function 3 test", () => { - jest.spyOn(window, "prompt").mockImplementation(() => "23"); - - let admin = hw4_f2()[0]; + hw4_f2(); it("is function exists", () => { expect(hw4_f3).toBeDefined(); diff --git a/src/script/homework_5.js b/src/script/homework_5.js deleted file mode 100644 index 4c4f746..0000000 --- a/src/script/homework_5.js +++ /dev/null @@ -1,52 +0,0 @@ -import { functions } from "./const.js"; - -export let hw5_f1 = (a) => { - let sum = a.reduce((prev, curr) => { - return prev + (isNaN(Number(curr)) ? 0 : Number(curr)); - }, 0); - return [sum]; -}; - -export let hw5_f2 = (a) => { - let rslt = a.map((el) => { - return (isNaN(Number(el)) ? 0 : Number(el)) * 2; - }); - return rslt; -}; - -export let hw5_f3 = (a) => { - a = a.map((el) => { - return isNaN(Number(el)) ? 0 : Number(el); - }); - let rslt = [Math.max(...a)]; - rslt.push(Math.min(...a)); - return rslt; -}; - -functions.push({ - name: "Homework 5 task #1", - func: hw5_f1, - params: [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { - return (Math.random() * 100).toFixed(); - }), - ], -}); -functions.push({ - name: "Homework 5 task #2", - func: hw5_f2, - params: [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { - return (Math.random() * 10).toFixed(); - }), - ], -}); -functions.push({ - name: "Homework 5 task #3", - func: hw5_f3, - params: [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map(() => { - return (Math.random() * 100).toFixed(); - }), - ], -}); diff --git a/src/script/homework_5.test.js b/src/script/homework_5.test.js deleted file mode 100644 index cb379d3..0000000 --- a/src/script/homework_5.test.js +++ /dev/null @@ -1,79 +0,0 @@ -import { hw5_f1, hw5_f2, hw5_f3 } from "./homework_5.js"; - -describe("homework 5, function 1 test", () => { - it("is function exists", () => { - expect(hw5_f1).toBeDefined(); - }); - - it("check array elements summary", () => { - expect(hw5_f1([1, 2, 3])[0]).toEqual(6); - }); - - it("check wrong array paramater (null)", () => { - expect(hw5_f1([null, null, null])[0]).toEqual(0); - }); - - it("check wrong array paramater (undefined)", () => { - expect(hw5_f1([undefined, undefined, undefined])[0]).toEqual(0); - }); - - it("check wrong array paramater (empty strings)", () => { - expect(hw5_f1(["", "", ""])[0]).toEqual(0); - }); - - it("check wrong array paramater (NaN)", () => { - expect(hw5_f1([NaN, NaN, NaN])[0]).toEqual(0); - }); -}); - -describe("homework 3, function 2 test", () => { - it("is function exists", () => { - expect(hw5_f2).toBeDefined(); - }); - - it("check new array generation", () => { - expect(hw5_f2([1, 2, 3])).toEqual([2, 4, 6]); - }); - - it("check wrong array paramater (null)", () => { - expect(hw5_f2([null, null, null])).toEqual([0, 0, 0]); - }); - - it("check wrong array paramater (undefined)", () => { - expect(hw5_f2([undefined, undefined, undefined])).toEqual([0, 0, 0]); - }); - - it("check wrong array paramater (empty strings)", () => { - expect(hw5_f2(["", "", ""])).toEqual([0, 0, 0]); - }); - - it("check wrong array paramater (NaN)", () => { - expect(hw5_f2([NaN, NaN, NaN])).toEqual([0, 0, 0]); - }); -}); - -describe("homework 3, function 3 test", () => { - it("is function exists", () => { - expect(hw5_f3).toBeDefined(); - }); - - it("check array elements summary", () => { - expect(hw5_f3([1, 2, 3])).toEqual([3, 1]); - }); - - it("check wrong array (null)", () => { - expect(hw5_f3([null, null, null])).toEqual([0, 0]); - }); - - it("check wrong array (undefined)", () => { - expect(hw5_f3([undefined, undefined])).toEqual([0, 0]); - }); - - it("check wrong array (empty strings)", () => { - expect(hw5_f3(["", ""])).toEqual([0, 0]); - }); - - it("check wrong array (NaN)", () => { - expect(hw5_f3([NaN, NaN])).toEqual([0, 0]); - }); -}); diff --git a/src/script/homework_5/homework_5.js b/src/script/homework_5/homework_5.js new file mode 100644 index 0000000..e152952 --- /dev/null +++ b/src/script/homework_5/homework_5.js @@ -0,0 +1,21 @@ +//вывести в консоль сумму всех элементов исходного массива +export let hw5_f1 = (a = [0]) => { + let sum = a.reduce((prev, curr) => { + return prev + curr; + }, 0); + console.log(sum); +}; + +//создать массив на основе исходного с элементами в два раза больше, чем в +//исходном по соответствующему индексу +export let hw5_f2 = (a = [0]) => { + let rslt = a.map((el) => { + return el * 2; + }); + return rslt; +}; + +//вывести в консоль максимальный и минимальный элемент массива +export let hw5_f3 = (a = [0, 1]) => { + console.log(Math.max(...a), Math.min(...a)); +}; diff --git a/src/script/homework_5/homework_5.test.js b/src/script/homework_5/homework_5.test.js new file mode 100644 index 0000000..f4939a7 --- /dev/null +++ b/src/script/homework_5/homework_5.test.js @@ -0,0 +1,54 @@ +import { hw5_f1, hw5_f2, hw5_f3 } from "./homework_5.js"; + +console.log = jest.fn(); + +describe("homework 5, function 1 test", () => { + it("is function exists", () => { + expect(hw5_f1).toBeDefined(); + }); + + it("check array elements summary", () => { + hw5_f1([1, 2, 3]); + expect(console.log).toBeCalledWith(6); + }); + + it("check w/0 parameters)", () => { + hw5_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(0); + }); +}); + +describe("homework 3, function 2 test", () => { + it("is function exists", () => { + expect(hw5_f2).toBeDefined(); + }); + + it("check new array generation", () => { + expect(hw5_f2([1, 2, 3])).toEqual([2, 4, 6]); + }); + + it("check new array generation w/o params", () => { + expect(hw5_f2()).toEqual([0]); + }); +}); + +describe("homework 3, function 3 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw5_f3).toBeDefined(); + }); + + it("check array elements summary", () => { + hw5_f3([1, 2, 3]); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(3, 1); + }); + + it("w/o params", () => { + hw5_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(1, 0); + }); +}); diff --git a/src/script/homework_6.js b/src/script/homework_6.js deleted file mode 100644 index 3f9256c..0000000 --- a/src/script/homework_6.js +++ /dev/null @@ -1,43 +0,0 @@ -import { functions } from "./const.js"; - -export let hw6_f1 = (a, b) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); - return [Math.abs(a - b)]; -}; - -export let hw6_f2 = (str) => { - str = String(str); - return [str.split(" ").length !== 1 ? "false" : "true"]; -}; - -export let hw6_f3 = (a, b) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); - - return [Math.pow(a, b)]; -}; - -functions.push({ - name: "Homework 6 task #1", - func: hw6_f1, - params: [+(Math.random() * 100).toFixed(), +(Math.random() * 100).toFixed()], -}); -functions.push({ - name: "Homework 6 task #2", - func: hw6_f2, - params: [ - Math.random() - .toFixed(Math.random() * 10) - .toString() + - ["", " "][(Math.random() * 2) >>> 0] + - Math.random() - .toFixed(Math.random() * 10) - .toString(), - ], -}); -functions.push({ - name: "Homework 6 task #3", - func: hw6_f3, - params: [+(Math.random() * 10).toFixed(), +(Math.random() * 4).toFixed()], -}); diff --git a/src/script/homework_6.test.js b/src/script/homework_6.test.js deleted file mode 100644 index ecda99a..0000000 --- a/src/script/homework_6.test.js +++ /dev/null @@ -1,92 +0,0 @@ -import { hw6_f1, hw6_f2, hw6_f3 } from "./homework_6.js"; - -describe("homework 6, function 1 test", () => { - it("is function exists", () => { - expect(hw6_f1).toBeDefined(); - }); - - it("param 5 and 4", () => { - expect(hw6_f1(51, 45)).toEqual([6]); - }); - - it("param 4 and 5", () => { - expect(hw6_f1(4, 5)).toEqual([1]); - }); - - it("param 9 and 9", () => { - expect(hw6_f1(9, 9)).toEqual([0]); - }); - it("param null and null", () => { - expect(hw6_f1(null, null)).toEqual([0]); - }); - it("param undefined and undefined", () => { - expect(hw6_f1(undefined, undefined)).toEqual([0]); - }); - it("param empty string and empty string", () => { - expect(hw6_f1("", "")).toEqual([0]); - }); - - it("param empty NaN and NaN", () => { - expect(hw6_f1(NaN, NaN)).toEqual([0]); - }); -}); - -describe("homework 6, function 2 test", () => { - it("is function exists", () => { - expect(hw6_f2).toBeDefined(); - }); - - it("param '123 1234", () => { - expect(hw6_f2("123 1234")).toEqual(["false"]); - }); - - it("param 123", () => { - expect(hw6_f2("123")).toEqual(["true"]); - }); - - it("param null", () => { - expect(hw6_f2(null)).toEqual(["true"]); - }); - - it("param undefined", () => { - expect(hw6_f2(undefined)).toEqual(["true"]); - }); - - it("param empty string", () => { - expect(hw6_f2("")).toEqual(["true"]); - }); - - it("param NaN", () => { - expect(hw6_f2(NaN)).toEqual(["true"]); - }); -}); - -describe("homework 6, function 3 test", () => { - it("is function exists", () => { - expect(hw6_f3).toBeDefined(); - }); - - it("param 3 and 3", () => { - expect(hw6_f3(3, 3)).toEqual([27]); - }); - - it("param 4 and 2", () => { - expect(hw6_f3(4, 2)).toEqual([16]); - }); - - it("param null and null", () => { - expect(hw6_f3(null, null)).toEqual([1]); - }); - - it("param undefined and undefined", () => { - expect(hw6_f3(undefined, undefined)).toEqual([1]); - }); - - it("param empty string and empty string", () => { - expect(hw6_f3("", "")).toEqual([1]); - }); - - it("param NaN and NaN", () => { - expect(hw6_f3(NaN, NaN)).toEqual([1]); - }); -}); diff --git a/src/script/homework_6/homework_6.js b/src/script/homework_6/homework_6.js new file mode 100644 index 0000000..1eab776 --- /dev/null +++ b/src/script/homework_6/homework_6.js @@ -0,0 +1,15 @@ +//функция возвращает разницу чисел между наибольшим и наименьшим +export let diff = (a = 0, b = 0) => { + return Math.abs(a - b); +}; + +//функция возвращает true если строка состоит из одного слова и +//false, если из нескольких +export let isWord = (str = "test") => { + return str.split(" ").length !== 1 ? false : true; +}; + +//написать функцию возведения в степень +export let pow = (a = 0, b = 0) => { + return Math.pow(a, b); +}; diff --git a/src/script/homework_6/homework_6.test.js b/src/script/homework_6/homework_6.test.js new file mode 100644 index 0000000..8514f27 --- /dev/null +++ b/src/script/homework_6/homework_6.test.js @@ -0,0 +1,59 @@ +import { diff, isWord, pow } from "./homework_6.js"; + +describe("homework 6, function 1 test", () => { + it("is function exists", () => { + expect(diff).toBeDefined(); + }); + + it("param 5 and 4", () => { + expect(diff(51, 45)).toEqual(6); + }); + + it("param 4 and 5", () => { + expect(diff(4, 5)).toEqual(1); + }); + + it("param 9 and 9", () => { + expect(diff(9, 9)).toEqual(0); + }); + + it("w/o params", () => { + expect(diff()).toEqual(0); + }); +}); + +describe("homework 6, function 2 test", () => { + it("is function exists", () => { + expect(isWord).toBeDefined(); + }); + + it("param '123 1234", () => { + expect(isWord("123 1234")).toEqual(false); + }); + + it("param 123", () => { + expect(isWord("123")).toEqual(true); + }); + + it("w/o params", () => { + expect(isWord()).toEqual(true); + }); +}); + +describe("homework 6, function 3 test", () => { + it("is function exists", () => { + expect(pow).toBeDefined(); + }); + + it("param 3 and 3", () => { + expect(pow(3, 3)).toEqual(27); + }); + + it("param 4 and 2", () => { + expect(pow(4, 2)).toEqual(16); + }); + + it("w/o params", () => { + expect(pow()).toEqual(1); + }); +}); diff --git a/src/script/homework_7.js b/src/script/homework_7/homework_7.js similarity index 81% rename from src/script/homework_7.js rename to src/script/homework_7/homework_7.js index 805bd71..0967134 100644 --- a/src/script/homework_7.js +++ b/src/script/homework_7/homework_7.js @@ -1,9 +1,6 @@ -import { - functions, - TASK7_BTN_CLASS_NAME, - TASK7_EDIT_CLASS_NAME, - TASK7_MAIN_CONTAINER_ID, -} from "./const.js"; +const TASK7_MAIN_CONTAINER_ID = "task7id"; +const TASK7_BTN_CLASS_NAME = "btns"; +const TASK7_EDIT_CLASS_NAME = "edits"; export let hw7_f1 = (mainContainer) => { let task7Div = document.getElementById(TASK7_MAIN_CONTAINER_ID); @@ -50,10 +47,4 @@ export let hw7_f1 = (mainContainer) => { } mainContainer.appendChild(task7Div); - return ["All need elements are created"]; }; -functions.push({ - name: "Homework 7 task #1", - func: hw7_f1, - params: [document.body], -}); diff --git a/src/script/homework_7.test.js b/src/script/homework_7/homework_7.test.js similarity index 90% rename from src/script/homework_7.test.js rename to src/script/homework_7/homework_7.test.js index 4cd98b2..1d93133 100644 --- a/src/script/homework_7.test.js +++ b/src/script/homework_7/homework_7.test.js @@ -64,4 +64,10 @@ describe("homework 7, function 1 test", () => { btn.click(); expect(container.querySelectorAll("p")).toHaveLength(5); }); + + it("check inputs creation in component full of childs", () => { + hw7_f1(container); + expect(container.querySelectorAll("input")).toHaveLength(2); + expect(container.querySelectorAll("p")).toHaveLength(3); + }); }); diff --git a/src/script/homework_8.js b/src/script/homework_8.js deleted file mode 100644 index db3d8ad..0000000 --- a/src/script/homework_8.js +++ /dev/null @@ -1,52 +0,0 @@ -import { functions } from "./const.js"; - -export let hw8_f1 = (strDate) => { - let date = String(strDate).split(".").reverse(); - if (date.length !== 3) { - return ["Error date in input string"]; - } - date[1]--; - - let day = new Intl.DateTimeFormat("en-US", { weekday: "long" }).format( - new Date(...date) - ); - return [day]; -}; - -export let hw8_f2 = () => { - let date = new Date(); - return [date.getHours() * 60 + date.getMinutes()]; -}; - -export let hw8_f3 = (strDate1, strDate2) => { - let date1 = String(strDate1).split(".").reverse(); - let date2 = String(strDate2).split(".").reverse(); - if (date1.length !== 3 || date2.length !== 3) { - return ["Error date in input string"]; - } - if (date1 > date2) { - return ["first is yonger"]; - } - return date1 < date2 ? ["second is yonger"] : ["both is same age"]; -}; - -functions.push({ - name: "Homework 8 task #1", - func: hw8_f1, - params: [ - `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.2022`, - ], -}); -functions.push({ name: "Homework 8 task #2", func: hw8_f2, params: [] }); -functions.push({ - name: "Homework 8 task #3", - func: hw8_f3, - params: [ - `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ - 1980 + ((Math.random() * 30) >>> 0) - }`, - `${(Math.random() * 28) >>> 0}.${(Math.random() * 10 + 1) >>> 0}.${ - 1980 + ((Math.random() * 30) >>> 0) - }`, - ], -}); diff --git a/src/script/homework_8.test.js b/src/script/homework_8.test.js deleted file mode 100644 index 800fb8d..0000000 --- a/src/script/homework_8.test.js +++ /dev/null @@ -1,83 +0,0 @@ -import { hw8_f1, hw8_f2, hw8_f3 } from "./homework_8.js"; - -describe("homework 8, function 1 test", () => { - it("is function exists", () => { - expect(hw8_f1).toBeDefined(); - }); - - it("param 26.05.2022", () => { - expect(hw8_f1("26.05.2022")).toEqual(["Thursday"]); - }); - it("param 24.05.2022", () => { - expect(hw8_f1("24.05.2022")).toEqual(["Tuesday"]); - }); - it("param null", () => { - expect(hw8_f1(null).join("").split(" ")).toContain("Error"); - }); - it("param undefined", () => { - expect(hw8_f1(undefined).join("").split(" ")).toContain("Error"); - }); - it("param empty string and empty string", () => { - expect(hw8_f1("").join("").split(" ")).toContain("Error"); - }); -}); - -describe("homework 8, function 2 test", () => { - it("is function exists", () => { - expect(hw8_f2).toBeDefined(); - }); - - it("is correct working", () => { - let m1 = 0; - let m2 = 1; - let m; - - while (m1 !== m2) { - m1 = new Date().getMinutes() + new Date().getHours() * 60; - m = hw8_f2(); - m2 = new Date().getMinutes() + new Date().getHours() * 60; - if (m1 === m2) { - expect(m).toEqual([m1]); - break; - } - } - }); -}); - -describe("homework 8, function 3 test", () => { - it("is function exists", () => { - expect(hw8_f3).toBeDefined(); - }); - - it("param 21.01.2000 and 23.01.2000", () => { - expect(hw8_f3("21.01.2000", "23.01.2000").join("").split(" ")).toContain( - "second" - ); - }); - - it("param 23.01.2000 and 21.01.2000", () => { - expect(hw8_f3("23.01.2000", "21.01.2000").join("").split(" ")).toContain( - "first" - ); - }); - - it("param 21.01.2000 and 21.01.2000", () => { - expect(hw8_f3("21.01.2000", "21.01.2000").join("").split(" ")).toContain( - "same" - ); - }); - - it("param null and 21.01.2000", () => { - expect(hw8_f3(null, "21.01.2000").join("").split(" ")).toContain("Error"); - }); - - it("param 21.01.2000 and undefined", () => { - expect(hw8_f3("21.01.2000", undefined).join("").split(" ")).toContain( - "Error" - ); - }); - - it("param empty string and empty string", () => { - expect(hw8_f3("", "").join("").split(" ")).toContain("Error"); - }); -}); diff --git a/src/script/homework_8/homework_8.js b/src/script/homework_8/homework_8.js new file mode 100644 index 0000000..fc60699 --- /dev/null +++ b/src/script/homework_8/homework_8.js @@ -0,0 +1,29 @@ +//запросить у пользователя дату дд.мм.гггг и вывести день недели +export let hw8_f1 = () => { + let strDate = window.prompt("Input date in dd.mm.yyyy format"); + let date = strDate.split(".").reverse(); + date[1]--; + + let day = new Intl.DateTimeFormat("en-US", { weekday: "long" }).format( + new Date(...date) + ); + console.log(day); +}; + +//написать программу, которая выводит в консоль количество минут с начала дня +export let hw8_f2 = () => { + let date = new Date(); + console.log(date.getHours() * 60 + date.getMinutes()); +}; + +//в двух переменных хранятся даты рождения двух человек в формате "дд.мм.гггг", +//определить какрй из людей моложе +export let hw8_f3 = (strDate1 = "21.01.2000", strDate2 = "21.02.2000") => { + let date1 = strDate1.split(".").reverse(); + let date2 = strDate2.split(".").reverse(); + if (date1 > date2) { + console.log("first is yonger"); + return; + } + console.log(date1 < date2 ? "second is yonger" : "both is same age"); +}; diff --git a/src/script/homework_8/homework_8.test.js b/src/script/homework_8/homework_8.test.js new file mode 100644 index 0000000..eec328e --- /dev/null +++ b/src/script/homework_8/homework_8.test.js @@ -0,0 +1,81 @@ +import { hw8_f1, hw8_f2, hw8_f3 } from "./homework_8.js"; + +describe("homework 8, function 1 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw8_f1).toBeDefined(); + }); + + it("param 26.05.2022", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "26.05.2022"; + }); + hw8_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("Thursday"); + }); + it("param 24.05.2022", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return "24.05.2022"; + }); + hw8_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("Tuesday"); + }); +}); + +describe("homework 8, function 2 test", () => { + console.log = jest.fn(); + it("is function exists", () => { + expect(hw8_f2).toBeDefined(); + }); + + it("is correct working", () => { + let m1 = 0; + let m2 = 1; + + while (m1 !== m2) { + m1 = new Date().getMinutes() + new Date().getHours() * 60; + hw8_f2(); + m2 = new Date().getMinutes() + new Date().getHours() * 60; + if (m1 === m2) { + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(m1); + break; + } + } + }); +}); + +describe("homework 8, function 3 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw8_f3).toBeDefined(); + }); + + it("param 21.01.2000 and 23.01.2000", () => { + hw8_f3("21.01.2000", "23.01.2000"); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("second is yonger"); + }); + + it("param 23.01.2000 and 21.01.2000", () => { + hw8_f3("23.01.2000", "21.01.2000"); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("first is yonger"); + }); + + it("param 21.01.2000 and 21.01.2000", () => { + hw8_f3("21.01.2000", "21.01.2000"); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("both is same age"); + }); + + it("w/o params", () => { + hw8_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("second is yonger"); + }); +}); diff --git a/src/script/homework_9.js b/src/script/homework_9.js deleted file mode 100644 index caa53b5..0000000 --- a/src/script/homework_9.js +++ /dev/null @@ -1,71 +0,0 @@ -import { functions } from "./const.js"; - -export let hw9_f1 = (a, b, c) => { - a = isNaN(Number(a)) ? -1 : Number(a); - b = isNaN(Number(b)) ? -1 : Number(b); - c = isNaN(Number(c)) ? -1 : Number(c); - - if (a < 0 || b < 0 || c < 0) { - return ["Error in input parameters"]; - } - - if ( - Math.pow(a, 2) === Math.pow(b, 2) + Math.pow(c, 2) || - Math.pow(b, 2) === Math.pow(c, 2) + Math.pow(a, 2) || - Math.pow(c, 2) === Math.pow(a, 2) + Math.pow(b, 2) - ) { - return ["right triangle"]; - } - - return ["not right tirangle"]; -}; - -export let hw9_f2 = (r) => { - r = isNaN(Number(r)) ? -1 : Number(r); - - if (r < 0) { - return ["Error in input parameters"]; - } - return [Math.PI * r * 2, Math.PI * Math.pow(2 * r, 2)]; -}; - -export let hw9_f3 = (a, b, c) => { - a = isNaN(Number(a)) ? 0 : Number(a); - b = isNaN(Number(b)) ? 0 : Number(b); - c = isNaN(Number(c)) ? 0 : Number(c); - - let d = Math.pow(b, 2) - 4 * a * c; - if (d < 0) { - return ["no solution"]; - } else { - if (d > 0) { - return [(Math.sqrt(d) - b) / (2 * a), (-Math.sqrt(d) - b) / (2 * a)]; - } - } - - return [-b / (2 * a)]; -}; - -functions.push({ - name: "Homework 9 task #1", - func: hw9_f1, - params: [ - [3, 4, 5][(Math.random() * 3) >>> 0], - [3, 4, 5][(Math.random() * 3) >>> 0], - [3, 4, 5][(Math.random() * 3) >>> 0], - ], -}); -functions.push({ - name: "Homework 9 task #2", - func: hw9_f2, - params: [(Math.random() * 10) >>> 0], -}); -functions.push({ - name: "Homework 9 task #3", - func: hw9_f3, - params: [ - [3, -4, 54][(Math.random() * 3) >>> 0], - [3, 28, 6][(Math.random() * 3) >>> 0], - [3, -49, 0][(Math.random() * 3) >>> 0], - ], -}); diff --git a/src/script/homework_9.test.js b/src/script/homework_9.test.js deleted file mode 100644 index 5890083..0000000 --- a/src/script/homework_9.test.js +++ /dev/null @@ -1,79 +0,0 @@ -import { hw9_f1, hw9_f2, hw9_f3 } from "./homework_9.js"; - -describe("homework 9, function 1 test", () => { - it("is function exists", () => { - expect(hw9_f1).toBeDefined(); - }); - - it("param 4, 5, 3", () => { - expect(hw9_f1(4, 5, 3).join("").split(" ")).not.toContain("not"); - }); - - it("param 3, 4, 5", () => { - expect(hw9_f1(3, 4, 5).join("").split(" ")).not.toContain("not"); - }); - - it("param 5, 4, 3", () => { - expect(hw9_f1(5, 4, 3).join("").split(" ")).not.toContain("not"); - }); - - it("param 6, 5, 3", () => { - expect(hw9_f1(6, 5, 3).join("").split(" ")).toContain("not"); - }); - - it("param 6, 4, 5", () => { - expect(hw9_f1(6, 4, 5).join("").split(" ")).toContain("not"); - }); - - it("param 6, 4, 3", () => { - expect(hw9_f1(6, 4, 3).join("").split(" ")).toContain("not"); - }); - - it("param -4, 5, 3", () => { - expect(hw9_f1(-4, 5, 3).join("").split(" ")).toContain("Error"); - }); - - it("param 3, -4, 5", () => { - expect(hw9_f1(3, -4, 5).join("").split(" ")).toContain("Error"); - }); - - it("param 3, 4, -5", () => { - expect(hw9_f1(3, 4, -5).join("").split(" ")).toContain("Error"); - }); -}); - -describe("homework 8, function 2 test", () => { - it("is function exists", () => { - expect(hw9_f2).toBeDefined(); - }); - - it("param 3", () => { - expect(hw9_f2(3)).toEqual([Math.PI * 3 * 2, Math.PI * Math.pow(3 * 2, 2)]); - }); - - it("param 5", () => { - expect(hw9_f2(5)).toEqual([Math.PI * 5 * 2, Math.PI * Math.pow(5 * 2, 2)]); - }); -}); - -describe("homework 8, function 3 test", () => { - it("is function exists", () => { - expect(hw9_f3).toBeDefined(); - }); - - it("param 4, -1, -5", () => { - expect(hw9_f3(4, -1, -5)).toEqual([1.25, -1]); - }); - - it("param 5, -6, -32", () => { - expect(hw9_f3(5, -6, -32)).toEqual([3.2, -2]); - }); - - it("param 8, 0, 5", () => { - expect(hw9_f3(8, 0, 5).join("").split(" ")).toContain("no"); - }); - - it("param -4, 28, -49", () => { - expect(hw9_f3(-4, 28, -49)).toEqual([3.5]); - }); -}); diff --git a/src/script/homework_9/homework_9.js b/src/script/homework_9/homework_9.js new file mode 100644 index 0000000..08090b4 --- /dev/null +++ b/src/script/homework_9/homework_9.js @@ -0,0 +1,40 @@ +//даны стороны треугольника, определить, является ли он прямоугольным +export let hw9_f1 = (a = 1, b = 1, c = 1) => { + if ( + Math.pow(a, 2) === Math.pow(b, 2) + Math.pow(c, 2) || + Math.pow(b, 2) === Math.pow(c, 2) + Math.pow(a, 2) || + Math.pow(c, 2) === Math.pow(a, 2) + Math.pow(b, 2) + ) { + console.log("right triangle"); + return; + } + + console.log("not right tirangle"); +}; + +//пользователь вводит число r, вывести длину окружности и площадь круга с радиусом r +export let hw9_f2 = () => { + let r = window.prompt("please input r"); + console.log(Math.PI * r * 2, Math.PI * Math.pow(2 * r, 2)); +}; + +//пользователь вводит числа a, b, c. Вывести корни квадратного уравнения с коэфициентами +//a, b, c +export let hw9_f3 = () => { + let a = window.prompt("please input a"); + let b = window.prompt("please input b"); + let c = window.prompt("please input c"); + + let d = Math.pow(b, 2) - 4 * a * c; + if (d < 0) { + console.log("no solution"); + return; + } else { + if (d > 0) { + console.log((Math.sqrt(d) - b) / (2 * a), (-Math.sqrt(d) - b) / (2 * a)); + return; + } + } + + console.log(-b / (2 * a)); +}; diff --git a/src/script/homework_9/homework_9.test.js b/src/script/homework_9/homework_9.test.js new file mode 100644 index 0000000..74fa56a --- /dev/null +++ b/src/script/homework_9/homework_9.test.js @@ -0,0 +1,129 @@ +import { hw9_f1, hw9_f2, hw9_f3 } from "./homework_9.js"; + +describe("homework 9, function 1 test", () => { + console.log = jest.fn(); + it("is function exists", () => { + expect(hw9_f1).toBeDefined(); + }); + + it("param 4, 5, 3", () => { + hw9_f1(4, 5, 3); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("right triangle"); + }); + + it("param 3, 4, 5", () => { + hw9_f1(3, 4, 5); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("right triangle"); + }); + + it("param 5, 4, 3", () => { + hw9_f1(5, 4, 3); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("right triangle"); + }); + + it("param 6, 5, 3", () => { + hw9_f1(6, 5, 3); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("not right tirangle"); + }); + + it("param 6, 4, 5", () => { + hw9_f1(6, 4, 5); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("not right tirangle"); + }); + + it("param 6, 4, 3", () => { + hw9_f1(6, 4, 3); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("not right tirangle"); + }); + + it("w/o params", () => { + hw9_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("not right tirangle"); + }); +}); + +describe("homework 8, function 2 test", () => { + console.log = jest.fn(); + + it("is function exists", () => { + expect(hw9_f2).toBeDefined(); + }); + + it("param 3", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return 3; + }); + hw9_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + Math.PI * 3 * 2, + Math.PI * Math.pow(3 * 2, 2) + ); + }); + + it("param 5", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return 5; + }); + hw9_f2(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith( + Math.PI * 5 * 2, + Math.PI * Math.pow(5 * 2, 2) + ); + }); +}); + +describe("homework 8, function 3 test", () => { + console.log = jest.fn(); + let promptIdx; + beforeEach(() => { + promptIdx = 0; + }); + it("is function exists", () => { + expect(hw9_f3).toBeDefined(); + }); + + it("param 4, -1, -5", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return [4, -1, -5][promptIdx++]; + }); + hw9_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(1.25, -1); + }); + + it("param 5, -6, -32", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return [5, -6, -32][promptIdx++]; + }); + hw9_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(3.2, -2); + }); + + it("param 8, 0, 5", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return [8, 0, 5][promptIdx++]; + }); + hw9_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith("no solution"); + }); + + it("param -4, 28, -49", () => { + jest.spyOn(window, "prompt").mockImplementation(() => { + return [-4, 28, -49][promptIdx++]; + }); + hw9_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(3.5); + }); +}); diff --git a/src/script/ui.js b/src/script/ui.js deleted file mode 100644 index 2f2f924..0000000 --- a/src/script/ui.js +++ /dev/null @@ -1,99 +0,0 @@ -import { - CHECKBOX_ID, - CHECKBOX_TEXT, - CHECKBOX_CLASS, - TASKS_CONTAINER_ID, - INFO_DIVS_CONTAINER_CLASS, - CONSOLE_CONTAINER_ID, - functions, -} from "./const.js"; - -let fakeConsole; -let outputConsole; - -export let addTask = (containerId, task, func) => { - if (containerId && task && func) { - let tasksDiv = document.getElementById(containerId); - let a = document.createElement("a"); - a.href = "#"; - a.text = task; - a.onclick = func; - tasksDiv.appendChild(a); - tasksDiv.appendChild(document.createElement("br")); - } -}; - -export function FakeConsole(containerId) { - this.container = document.getElementById(containerId); - this.clear = () => { - this.container.innerHTML = ""; - }; - this.log = (str) => { - this.container.innerHTML += str + "
"; - }; - this.isFake = () => { - return true; - }; - this.clear(); -} - -export let createUI = (containerId) => { - let mainDiv = document.getElementById(containerId); - - let cbxConsole = document.createElement("input"); - cbxConsole.type = "checkbox"; - cbxConsole.checked = true; - cbxConsole.id = CHECKBOX_ID; - cbxConsole.addEventListener("click", (e) => { - outputConsole = e.currentTarget.checked ? fakeConsole : console; - outputConsole.log("!"); - }); - - let lblConsole = document.createElement("span"); - lblConsole.innerHTML = CHECKBOX_TEXT; - - let cbxDiv = document.createElement("div"); - cbxDiv.appendChild(cbxConsole); - cbxDiv.appendChild(lblConsole); - cbxDiv.className = CHECKBOX_CLASS; - - let tasksDiv = document.createElement("div"); - tasksDiv.id = TASKS_CONTAINER_ID; - tasksDiv.className = INFO_DIVS_CONTAINER_CLASS; - - let consoleDiv = document.createElement("div"); - consoleDiv.id = CONSOLE_CONTAINER_ID; - consoleDiv.className = INFO_DIVS_CONTAINER_CLASS; - - mainDiv.appendChild(cbxDiv); - mainDiv.appendChild(tasksDiv); - mainDiv.appendChild(consoleDiv); - - fakeConsole = new FakeConsole(CONSOLE_CONTAINER_ID); - outputConsole = fakeConsole; - - functions.forEach((el) => { - addTask(TASKS_CONTAINER_ID, el.name, () => { - outputConsole.clear(); - if (el.params.length) { - let str = "Parameters: "; - el.params.forEach((el) => { - str += typeof el === "string" ? `"${el}" ` : `${el} `; - }); - outputConsole.log(str); - } - let str = "Results: "; - let fake = "isFake" in outputConsole; - el.func(...el.params).forEach((el) => { - if (!fake && el === "
") { - el = "\n"; - } - str += - typeof el === "string" && el !== "
" && el !== "\n" - ? `"${el}" ` - : `${el} `; - }); - outputConsole.log(str); - }); - }); -}; diff --git a/src/script/ui.test.js b/src/script/ui.test.js deleted file mode 100644 index f138f26..0000000 --- a/src/script/ui.test.js +++ /dev/null @@ -1,84 +0,0 @@ -import { createUI, addTask, FakeConsole } from "./ui.js"; -import { functions } from "./const.js"; - -describe("task adding test", () => { - document.body.innerHTML = "
"; - - let container = document.getElementById("testDiv"); - let func = () => {}; - - addTask("testDiv", "task0", func); - addTask("testDiv", "task1", func); - addTask("testDiv", "task2", func); - - let tasks = container.querySelectorAll("a"); - - it("check added tasks", () => { - expect(container.querySelectorAll("a")).toHaveLength(3); - }); - - it("check tasks captions", () => { - expect(tasks[0].text).toEqual("task0"); - expect(tasks[1].text).toEqual("task1"); - expect(tasks[2].text).toEqual("task2"); - }); - - it("check tasks functions", () => { - expect(tasks[0].onclick).toEqual(func); - expect(tasks[1].onclick).toEqual(func); - expect(tasks[2].onclick).toEqual(func); - }); -}); - -describe("FakeConsole working test", () => { - document.body.innerHTML = "
"; - - let container = document.getElementById("testDiv"); - let myConsole = new FakeConsole("testDiv"); - - it("check console creating", () => { - expect(myConsole).toBeDefined(); - }); - - it("check console container choosing", () => { - expect(myConsole.container).toEqual(container); - }); - - it("check console printing", () => { - myConsole.log("test1!"); - myConsole.log("test2!"); - expect(container.innerHTML).toEqual("test1!
test2!
"); - }); - - it("check console erasing", () => { - myConsole.clear(); - expect(container.innerHTML).toEqual(""); - }); -}); - -describe("UI creation test", () => { - document.body.innerHTML = "
"; - - functions[0] = { name: "task0", func: () => {}, params: [] }; - functions[1] = { name: "task1", func: () => {}, params: [] }; - - createUI("testDiv"); - - let container = document.getElementById("testDiv"); - - it("check divs", () => { - expect(container.querySelectorAll("div")).toHaveLength(3); - }); - - it("check inputs", () => { - expect(container.querySelectorAll("input")).toHaveLength(1); - }); - - it("check spans", () => { - expect(container.querySelectorAll("span")).toHaveLength(1); - }); - - it("check links, created from tasks", () => { - expect(container.querySelectorAll("a")).toHaveLength(2); - }); -}); From 8f1756a14af411a3d359ed015a02fb0a01bb1403 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 3 Jun 2022 07:18:08 +0300 Subject: [PATCH 4/4] change tests for codesandbox --- src/script/homework_1/homework_1.test.js | 12 +++++++++--- src/script/homework_10/homework_10.test.js | 4 +++- src/script/homework_2/homework_2.test.js | 13 ++++++++++--- src/script/homework_3/homework_3.test.js | 13 ++++++++++--- src/script/homework_5/homework_5.test.js | 10 +++++++--- src/script/homework_8/homework_8.test.js | 13 ++++++++++--- src/script/homework_9/homework_9.test.js | 14 +++++++++++--- 7 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/script/homework_1/homework_1.test.js b/src/script/homework_1/homework_1.test.js index 405b7fa..f981282 100644 --- a/src/script/homework_1/homework_1.test.js +++ b/src/script/homework_1/homework_1.test.js @@ -1,7 +1,9 @@ import { hw1_f1, hw1_f2, hw1_f3 } from "./homework_1.js"; describe("homework 1, function 1 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw1_f1).toBeDefined(); @@ -27,7 +29,9 @@ describe("homework 1, function 1 test", () => { }); describe("homework 1, function 2 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw1_f2).toBeDefined(); @@ -53,7 +57,9 @@ describe("homework 1, function 2 test", () => { }); describe("homework 1, function 3 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw1_f3).toBeDefined(); diff --git a/src/script/homework_10/homework_10.test.js b/src/script/homework_10/homework_10.test.js index 5a1f80c..7bffa64 100644 --- a/src/script/homework_10/homework_10.test.js +++ b/src/script/homework_10/homework_10.test.js @@ -1,7 +1,9 @@ import { hw10_f1 } from "./homework_10.js"; describe("homework 10, function 1 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw10_f1).toBeDefined(); diff --git a/src/script/homework_2/homework_2.test.js b/src/script/homework_2/homework_2.test.js index cffdfbb..7094ea9 100644 --- a/src/script/homework_2/homework_2.test.js +++ b/src/script/homework_2/homework_2.test.js @@ -1,7 +1,9 @@ import { hw2_f1, hw2_f2, hw2_f3 } from "./homework_2.js"; describe("homework 2, function 1 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw2_f1).toBeDefined(); @@ -27,7 +29,9 @@ describe("homework 2, function 1 test", () => { }); describe("homework 2, function 2 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw2_f2).toBeDefined(); @@ -70,7 +74,10 @@ describe("homework 2, function 2 test", () => { }); describe("homework 2, function 3 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); + it("is function exists", () => { expect(hw2_f3).toBeDefined(); }); diff --git a/src/script/homework_3/homework_3.test.js b/src/script/homework_3/homework_3.test.js index 59d525b..75e3385 100644 --- a/src/script/homework_3/homework_3.test.js +++ b/src/script/homework_3/homework_3.test.js @@ -1,7 +1,10 @@ import { hw3_f1, hw3_f2, hw3_f3 } from "./homework_3.js"; describe("homework 3, function 1 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); + it("is function exists", () => { expect(hw3_f1).toBeDefined(); }); @@ -18,7 +21,9 @@ describe("homework 3, function 1 test", () => { }); describe("homework 3, function 2 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw3_f2).toBeDefined(); }); @@ -40,7 +45,9 @@ describe("homework 3, function 2 test", () => { }); describe("homework 3, function 3 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("prompted 3", () => { jest.spyOn(window, "prompt").mockImplementation(() => "3"); hw3_f3(); diff --git a/src/script/homework_5/homework_5.test.js b/src/script/homework_5/homework_5.test.js index f4939a7..e1f828f 100644 --- a/src/script/homework_5/homework_5.test.js +++ b/src/script/homework_5/homework_5.test.js @@ -1,8 +1,10 @@ import { hw5_f1, hw5_f2, hw5_f3 } from "./homework_5.js"; -console.log = jest.fn(); - describe("homework 5, function 1 test", () => { + beforeEach(() => { + console.log = jest.fn(); + }); + it("is function exists", () => { expect(hw5_f1).toBeDefined(); }); @@ -34,7 +36,9 @@ describe("homework 3, function 2 test", () => { }); describe("homework 3, function 3 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw5_f3).toBeDefined(); diff --git a/src/script/homework_8/homework_8.test.js b/src/script/homework_8/homework_8.test.js index eec328e..83daef7 100644 --- a/src/script/homework_8/homework_8.test.js +++ b/src/script/homework_8/homework_8.test.js @@ -1,7 +1,9 @@ import { hw8_f1, hw8_f2, hw8_f3 } from "./homework_8.js"; describe("homework 8, function 1 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw8_f1).toBeDefined(); @@ -26,7 +28,10 @@ describe("homework 8, function 1 test", () => { }); describe("homework 8, function 2 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); + it("is function exists", () => { expect(hw8_f2).toBeDefined(); }); @@ -49,7 +54,9 @@ describe("homework 8, function 2 test", () => { }); describe("homework 8, function 3 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw8_f3).toBeDefined(); diff --git a/src/script/homework_9/homework_9.test.js b/src/script/homework_9/homework_9.test.js index 74fa56a..4581e4e 100644 --- a/src/script/homework_9/homework_9.test.js +++ b/src/script/homework_9/homework_9.test.js @@ -1,7 +1,10 @@ import { hw9_f1, hw9_f2, hw9_f3 } from "./homework_9.js"; describe("homework 9, function 1 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); + it("is function exists", () => { expect(hw9_f1).toBeDefined(); }); @@ -50,7 +53,9 @@ describe("homework 9, function 1 test", () => { }); describe("homework 8, function 2 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); it("is function exists", () => { expect(hw9_f2).toBeDefined(); @@ -82,7 +87,10 @@ describe("homework 8, function 2 test", () => { }); describe("homework 8, function 3 test", () => { - console.log = jest.fn(); + beforeEach(() => { + console.log = jest.fn(); + }); + let promptIdx; beforeEach(() => { promptIdx = 0;