-
Notifications
You must be signed in to change notification settings - Fork 0
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
Hw 6 #6
Open
VladyslavMartynov
wants to merge
3
commits into
main
Choose a base branch
from
HW_6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hw 6 #6
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>task7</title> | ||
</head> | ||
<header></header> | ||
<main></main> | ||
<footer></footer> | ||
<script src="js/main.js"></script> | ||
<body></body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
"use strict" | ||
// Создать функцию конструктор Animal c аргументами name, age, color. Написать логику для того, чтобы функцию можно было вызывать как с, так и без new: | ||
// При вызове без new новый обьект все равно должен создаться | ||
|
||
function Animal(name, age, colour) { | ||
|
||
if(!new.target){ | ||
return new Animal(name, age, colour); | ||
} | ||
|
||
this.name = name; | ||
this.age = age; | ||
this.colour = colour; | ||
} | ||
|
||
const animal = Animal('Name', 'Age', 'Colour'); | ||
console.log(animal); | ||
|
||
// Создайте функцию-конструктор Calculator, который создаёт объекты с такими методами: | ||
// read() запрашивает два значения при помощи prompt и сохраняет их значение в свойствах объекта. | ||
// setAction() запрашивает действие при помощи prompt, которые мы хотим сделать (+, -, / и т.д) | ||
// doAction() выполняет действие, которое юзер ввел (будет вызывать в себя методы sum, mul, min и т.д) | ||
// sum() возвращает сумму введённых свойств. | ||
// mul() возвращает произведение введённых свойств. | ||
// min() возвращает разницу введённых свойств. | ||
// другие методы можете добавит если хотите (метод для квадратного корня и т.д.) | ||
|
||
function Calculator() { | ||
|
||
} | ||
|
||
Calculator.prototype.read = function() { | ||
const val1 = +(prompt('Enter your 1 number')); | ||
const val2 = +(prompt('Enter your 2 number')); | ||
this.first = val1; | ||
this.second = val2; | ||
} | ||
|
||
Calculator.prototype.setAction = function() { | ||
const action = prompt('Enter a math operation'); | ||
this.action = action; | ||
} | ||
|
||
Calculator.prototype.doAction = function() { | ||
switch(this.action){ | ||
case '+': | ||
this.sum(); | ||
break; | ||
|
||
case '-': | ||
this.min(); | ||
break; | ||
|
||
case '/': | ||
this.divide(); | ||
break; | ||
|
||
case '*': | ||
this.mul(); | ||
break; | ||
|
||
case '**': | ||
this.pow(); | ||
break; | ||
} | ||
} | ||
|
||
Calculator.prototype.sum = function() { | ||
this.sum = this.first + this.second; | ||
} | ||
|
||
Calculator.prototype.min = function() { | ||
this.min = this.first - this.second; | ||
} | ||
|
||
Calculator.prototype.mul = function() { | ||
this.mul = this.first * this.second; | ||
} | ||
|
||
Calculator.prototype.divide = function() { | ||
this.divide = this.first / this.second; | ||
} | ||
|
||
Calculator.prototype.pow = function() { | ||
this.pow = this.first ** this.second; | ||
} | ||
|
||
const calculator = new Calculator(); | ||
calculator.read(); | ||
console.log(calculator); | ||
calculator.setAction(); | ||
calculator.doAction(); | ||
console.log(calculator); | ||
|
||
// Создать функцию конструктор Nums, которая принимает бесконечное множество аргументов, и они записываются в свойство args в виде массива | ||
// Добавить в прототип для всех объектов, которые создадим от конструктора Nums, 2 метода: | ||
|
||
// метод getSum должен вернуть сумму всех элементов (которые только целые числа) массива args | ||
// метод myFilterReverse должен отфильтровать массив и оставить только целые числа и развернуть массив (было [1, 2, 3] -> стало [3, 2, 1]) | ||
// Метод .reverse использовать нельзя :) | ||
|
||
// только целые числа -> Number.isInteger(1); // true Number.IsInteger(1.2); // false | ||
|
||
function Nums(...args){ | ||
this.args = [...args]; | ||
} | ||
|
||
Nums.prototype.getSum = function() { | ||
const arr = this.filter(); | ||
const result = arr.reduce((acc, item) => acc + item); | ||
return result; | ||
} | ||
|
||
Nums.prototype.filterReverse = function() { | ||
const arr = this.filter(); | ||
const reverseArr = arr.map((item, i) => { | ||
return arr[arr.length - i - 1]; | ||
}); | ||
return reverseArr; | ||
} | ||
|
||
Nums.prototype.filter = function() { | ||
const filterArr = this.args.filter(el => { | ||
return Number.isInteger(el); | ||
}); | ||
|
||
return filterArr; | ||
} | ||
|
||
const nums = new Nums(2.6, 1, 2, 3, null, [], {}); | ||
console.log(nums.getSum()); | ||
console.log(nums.filterReverse()); | ||
|
||
// Есть массив [1, 1, 2, 2, 3] | ||
// Создать свой метод getUnique для любых массивов, который отфильтрует массив и оставит в нем только уникальные значения | ||
// Подсказка: чтобы было легче почитайте про метод .includes() | ||
|
||
const arr = [1, 1, 2, 2, 3]; | ||
function getUnique(arr){ | ||
return Array.isArray(arr) ? | ||
[...new Set(arr)] : `Error ${arr} is not correct!`; | ||
} | ||
|
||
console.log(getUnique(arr)); | ||
|
||
// 5* Есть объект {a: 1, b: 2, c: 3, d: false, e: 0}; Нужно создать 2 метода для любых объектов: | ||
// метод getKeySum, который найдет сумму значений всех ключей, которые true. | ||
// метод reversKey который поменяет местави key и value (ключ и значение) | ||
// Пример Был объект {a: 1, b: 2}.reversKey() -> стало {1: 'b', 2: 'a'} | ||
|
||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
d: false, | ||
e: 0, | ||
|
||
getKeySum(){ | ||
const filter = Object.values(this).filter(el => el && typeof el !== 'function'); | ||
const totalsum = filter.reduce((acc, item) => acc + item); | ||
return totalsum; | ||
}, | ||
|
||
reverseKey(){ | ||
|
||
const newObj = Object.keys(this) | ||
.filter(el => typeof this[el] !== 'function') | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. что-то с отступами не то |
||
.map(el => { | ||
return {[this[el]]: el}; | ||
}); | ||
|
||
return newObj.reduce((acc, el) => { | ||
return {...acc , ...el}; | ||
}); | ||
|
||
} | ||
} | ||
|
||
console.log(obj.getKeySum()); | ||
console.log(obj.reverseKey()); | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А если будет действие, которого нет?