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/.gitignore b/.gitignore index 3c3629e..ba2a97b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +coverage 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/index.html b/src/index.html new file mode 100644 index 0000000..1682605 --- /dev/null +++ b/src/index.html @@ -0,0 +1,20 @@ + + + + + + Homework for lection #9 + + + + + + + + + + + + + + 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..f981282 --- /dev/null +++ b/src/script/homework_1/homework_1.test.js @@ -0,0 +1,85 @@ +import { hw1_f1, hw1_f2, hw1_f3 } from "./homework_1.js"; + +describe("homework 1, function 1 test", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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/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..7bffa64 --- /dev/null +++ b/src/script/homework_10/homework_10.test.js @@ -0,0 +1,140 @@ +import { hw10_f1 } from "./homework_10.js"; + +describe("homework 10, function 1 test", () => { + beforeEach(() => { + 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/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..7094ea9 --- /dev/null +++ b/src/script/homework_2/homework_2.test.js @@ -0,0 +1,102 @@ +import { hw2_f1, hw2_f2, hw2_f3 } from "./homework_2.js"; + +describe("homework 2, function 1 test", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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/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/homework_3.test.js b/src/script/homework_3/homework_3.test.js new file mode 100644 index 0000000..75e3385 --- /dev/null +++ b/src/script/homework_3/homework_3.test.js @@ -0,0 +1,71 @@ +import { hw3_f1, hw3_f2, hw3_f3 } from "./homework_3.js"; + +describe("homework 3, function 1 test", () => { + beforeEach(() => { + console.log = jest.fn(); + }); + + it("is function exists", () => { + expect(hw3_f1).toBeDefined(); + }); + + it("check sum", () => { + let sum = 0; + for (let i = 100; i >= 50; i--) { + sum += i; + } + hw3_f1(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(sum); + }); +}); + +describe("homework 3, function 2 test", () => { + beforeEach(() => { + console.log = jest.fn(); + }); + 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); + } + 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", () => { + beforeEach(() => { + console.log = jest.fn(); + }); + it("prompted 3", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "3"); + hw3_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(2); + }); + + it("prompted 9", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "9"); + hw3_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(5); + }); + + it("prompted 0", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "0"); + hw3_f3(); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toBeCalledWith(0); + }); +}); 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/homework_4.test.js b/src/script/homework_4/homework_4.test.js new file mode 100644 index 0000000..c7ee5ac --- /dev/null +++ b/src/script/homework_4/homework_4.test.js @@ -0,0 +1,116 @@ +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"); + + it("is function exists", () => { + expect(hw4_f1).toBeDefined(); + }); + + hw4_f1(); + + it("is user object exists", () => { + expect(user).toBeDefined(); + }); + + it("check user object", () => { + expect(typeof user).toEqual("object"); + }); + + it("check user name exists", () => { + expect(user.name).toBeDefined(); + }); + + it("check user name value", () => { + expect(user.name).toEqual("John"); + }); + + it("check user age exists", () => { + expect(user.age).toBeDefined(); + }); + + it("check user age value", () => { + expect(user.age).toEqual("23"); + }); + + it("check user age modified", () => { + jest.spyOn(window, "prompt").mockImplementation(() => "21"); + hw4_f1(); + expect(user.age).toEqual("21"); + }); +}); + +describe("homework 3, function 2 test", () => { + 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 admin).toEqual("object"); + }); + + it("check admin name exists", () => { + expect(admin.name).toBeDefined(); + }); + + it("check that admin name is equal to user name", () => { + expect(admin.name).toEqual(user.name); + }); + + it("check admin age exists", () => { + expect(admin.age).toBeDefined(); + }); + + it("check admin age value", () => { + expect(admin.age).toEqual(user.age); + }); + + it("check admin role exists", () => { + expect(admin.role).toBeDefined(); + }); + + it("check admin role value", () => { + expect(admin.role).toEqual("admin"); + }); +}); + +describe("homework 3, function 3 test", () => { + hw4_f2(); + + 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/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..e1f828f --- /dev/null +++ b/src/script/homework_5/homework_5.test.js @@ -0,0 +1,58 @@ +import { hw5_f1, hw5_f2, hw5_f3 } from "./homework_5.js"; + +describe("homework 5, function 1 test", () => { + beforeEach(() => { + console.log = jest.fn(); + }); + + 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", () => { + beforeEach(() => { + 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/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/homework_7.js b/src/script/homework_7/homework_7.js new file mode 100644 index 0000000..0967134 --- /dev/null +++ b/src/script/homework_7/homework_7.js @@ -0,0 +1,50 @@ +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); + 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); +}; diff --git a/src/script/homework_7/homework_7.test.js b/src/script/homework_7/homework_7.test.js new file mode 100644 index 0000000..1d93133 --- /dev/null +++ b/src/script/homework_7/homework_7.test.js @@ -0,0 +1,73 @@ +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); + }); + + 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/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..83daef7 --- /dev/null +++ b/src/script/homework_8/homework_8.test.js @@ -0,0 +1,88 @@ +import { hw8_f1, hw8_f2, hw8_f3 } from "./homework_8.js"; + +describe("homework 8, function 1 test", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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/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..4581e4e --- /dev/null +++ b/src/script/homework_9/homework_9.test.js @@ -0,0 +1,137 @@ +import { hw9_f1, hw9_f2, hw9_f3 } from "./homework_9.js"; + +describe("homework 9, function 1 test", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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", () => { + beforeEach(() => { + 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); + }); +});