/
NyutaNecoder
/
tasks
Обзор
Документация
Войти
/
NyutaNecoder
/
tasks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main2.js
166 строк
3 KB
NyutaNecoder
Create: main2.js
10 авг 2026, 09:33
Верифицирован
10 авг 2026, 09:33
5683d9d
Код
Авторство
О чём код?
const person = { name: "Иван", age: 23, city: "Москва", } // 1. Объект с данными о человеке function getPersonInfo({ name, age, city }) { return `Имя: ${name}, Возраст: ${age}, Город: ${city}` } console.log(getPersonInfo(person)) // 2. Добавление свойства в объект person.isStudent = person.age < 23 ? true : false console.log(person) // 3. Удаление свойства из объекта function removeCity(obj) { delete obj.city return obj } function removeCity2(obj) { "city" in obj && delete obj.city return obj } console.log(removeCity(person)) console.log(removeCity2(person)) // 4. Объект с методами машины const car = { start() { return 'Машина поехала' }, stop() { return 'Машина остановилась' } } console.log(car.start()) console.log(car.stop()) // 5. Получение всех ключей объекта function getKeys(obj) { return Object.keys(obj) // const keys = [] // for (let key in obj) { // keys.push(key) // } // return keys } console.log(getKeys(person)) // 6. Получение всех значений объекта function getValues(obj) { return Object.values(obj) // const values = [] // for (let key in obj) { // values.push(obj[key]) // } // return values } console.log(getValues(person)) // 7. Метод для форматирования информации о книге const book = { title: "Евгений Онегин", author: "Пушкин", year: 1833, getInfo() { return `${this.title} — ${this.author} (${this.year})` }, } console.log(book.getInfo()) // 8. Объект-калькулятор const calculator = { add(a, b) { return a + b }, sub(a, b) { return a - b }, mul(a, b) { return a * b }, div(a, b) { if (b === 0) { // throw new Error("Деление на ноль невозможно") return "Деление на ноль невозможно" } return a / b }, } console.log(calculator.add(5, 3)) console.log(calculator.sub(10, 3)) console.log(calculator.mul(5, 3)) console.log(calculator.div(7, 3)) console.log(calculator.div(5, 0)) // 9. Клонирование объекта function cloneObject(obj) { return Object.assign({}, obj) // return { ...obj } } console.log(cloneObject(person)) // 10. Студент и средняя оценка const student = { name: "Мария", grades: [5, 4, 3, 5], getAverage() { const sum = this.grades.reduce((acc, grade) => acc + grade, 0) return sum / this.grades.length }, } console.log(student.getAverage())