Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework for lection #9, all functions in one commit #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
coverage
2 changes: 1 addition & 1 deletion package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Homework for lection #9</title>

<script type="module" src="script/homework_1/homework_1.js"></script>
<script type="module" src="script/homework_2/homework_2.js"></script>
<script type="module" src="script/homework_3/homework_3.js"></script>
<script type="module" src="script/homework_4/homework_4.js"></script>
<script type="module" src="script/homework_5/homework_5.js"></script>
<script type="module" src="script/homework_6/homework_6.js"></script>
<script type="module" src="script/homework_7/homework_7.js"></script>
<script type="module" src="script/homework_8/homework_8.js"></script>
<script type="module" src="script/homework_9/homework_9.js"></script>
<script type="module" src="script/homework_10/homework_10.js"></script>
</head>
<body></body>
</html>
15 changes: 15 additions & 0 deletions src/script/homework_1/homework_1.js
Original file line number Diff line number Diff line change
@@ -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]));
};
85 changes: 85 additions & 0 deletions src/script/homework_1/homework_1.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
24 changes: 24 additions & 0 deletions src/script/homework_10/homework_10.js
Original file line number Diff line number Diff line change
@@ -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");
};
140 changes: 140 additions & 0 deletions src/script/homework_10/homework_10.test.js
Original file line number Diff line number Diff line change
@@ -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 [email protected]", () => {
jest.spyOn(window, "prompt").mockImplementation(() => {
return "[email protected]";
});
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 [email protected]", () => {
jest.spyOn(window, "prompt").mockImplementation(() => {
return "[email protected]";
});
hw10_f1();
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toBeCalledWith("mail");
});

it("param [email protected]", () => {
jest.spyOn(window, "prompt").mockImplementation(() => {
return "[email protected]";
});
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"
);
});
});
40 changes: 40 additions & 0 deletions src/script/homework_2/homework_2.js
Original file line number Diff line number Diff line change
@@ -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");
};
Loading