function moreHTML(title, startNumber) {
// ...
return HTML;
}
li.innerHTML = moreHTML('Третий список', 9);
li.innerHTML = moreHTML('Четвертый список', 12);
return `
<span class="title">${title}</span>
<ul class="extra-list">
<li class="extra-item">${startNumber} элемент списка</li>'
<li class="extra-item">${startNumber + 1} элемент списка</li>
<li class="extra-item">${startNumber + 2} элемент списка</li>
</ul>
`;
function present () {
if(visitinMagazine.length < 25)
visitinMagazine.push(true);
else
{
// что-то сделать при переполнении
}
}
function absent() {
if(visitinMagazine.length < 25)
visitinMagazine.push(false);
else
{
// что-то сделать при переполнении
}
}
const arrayOfObjects = [
{ student: 'Dmitriy', runLab () {return 1;}},
{ student: 'Andrew', runLab () {return 2;}},
{ student: 'Mellisa', runLab () {return 3;}},
{ student: 'Ann', XrunLab () {return 4;}},
];
function gradeLabs(labs, expectedResult = 1) {
labs.forEach(({ student, runLab }) => {
try {
const result = runLab();
if (result === expectedResult) {
console.log('Student %s has PASSED the test', student);
} else {
console.error('Student %s has FAILED the test', student);
}
} catch (e) {
console.error('Student: %s has FAILED the test. Error thrown: %o', student, e);
}
});
}
gradeLabs(arrayOfObjects, 1);
arrayOfObjects.forEach((element) => element.runLab(), () => {
if (element.runLab !== 1) {
console.log(element.runLab());
}
})
arrayOfObjects.forEach((element) => {
const resultLab = element.runLab();
if (resultLab !== 1) {
console.log(resultLab);
}
});
class Hamburger {
constructor(size, stuffing) {
this.size = size;
this.stuffing = stuffing;
this.toppings = [];
}
static get SIZE_SMALL() {
return { name: 'SIZE_SMALL', price: 50, calories: 20 };
}
static get SIZE_HUGE() {
return { name: 'SIZE_HUGE', price: 100, calories: 40 };
}
static get STUFFING_CHEESE() {
return { name: 'STUFFING_CHEESE', price: 10, calories: 20 };
}
static get STUFFING_SALAD() {
return { name: 'STUFFING_SALAD', price: 20, calories: 5 };
}
static get STUFFING_POTATOE() {
return { name: 'STUFFING_POTATOE', price: 15, calories: 10 };
}
static get TOPPING_SAUCE() {
return { name: 'TOPPING_SAUCE', price: 15, calories: 0 };
}
static get TOPPING_MAYO() {
return { name: 'TOPPING_MAYO', price: 20, calories: 5 };
}
addTopping(topping) {
this.toppings.push(topping);
return this;
}
calculateCalories() {
const toppingCalories = this.toppings.reduce((total, topping) => total + topping.calories, 0);
return this.size.calories + this.stuffing.calories + toppingCalories;
}
calculatePrice() {
const toppingPrice = this.toppings.reduce((total, topping) => total + topping.price, 0);
return this.size.price + this.stuffing.price + toppingPrice;
}
}
const hamburger = new Hamburger(Hamburger.SIZE_SMALL, Hamburger.STUFFING_CHEESE);
hamburger.addTopping(Hamburger.TOPPING_MAYO);
console.log('Calories:', hamburger.calculateCalories());
hamburger.addTopping(Hamburger.TOPPING_SAUCE);
console.log('Calories:', hamburger.calculateCalories());
console.log('Price with sauce:', hamburger.calculatePrice());
age
, и возвращать всегда валидное значение.length
у массива reasons
, чтобы он всегда был "пустой". for (let key in objectEmployee) {
что будет в key?objectEmployee.key['age']
есть ли у objectEmployee
свойство key
? (нет)const filters = {
age: v => v >= 25,
education: v => v === 'higher',
experience: v => v >= 1,
};
const employee = {};
const defaultDescriptor = { writable: true, enumerable: true, configurable: true };
Object.defineProperties(employee, {
name: { ...defaultDescriptor, value: 'Dmitriy', writable: false },
age: { ...defaultDescriptor, value: 21 },
education: { ...defaultDescriptor, value: 'higher' },
experience: { ...defaultDescriptor, value: 0 },
});
const hireNewEmployee = (employee, filters) => {
const errors = [];
Object.entries(filters).forEach(([name, func]) => {
if (!func(employee[name])) {
errors.push(name);
}
});
return errors.length
? `Not hired: sorry we cannot hire you. Here is why: ${errors.join(', ')}`
: 'You are Hired! Congrats!';
};
hireNewEmployee(employee, filters);
// "Not hired: sorry we cannot hire you. Here is why: age, experience"
const isEmpty = obj => Object.keys(obj).length <= 2;
console.log( isEmpty({ a: "AAA" }) ? 'object is empty' : 'object is full of properties' );
// выведет "object is empty"
console.log( isEmpty({ a: "AAA", b: "BB", c: "C" }) ? 'object is empty' : 'object is full of properties' );
// выведет "object is full of properties"
array.forEach((element, index, array) => {}) // 2 и 3 необязательные
array.forEach((element) => element.balance.startsWith("$1") && console.log(element.balance))
array.forEach(({ balance }) => balance.startsWith("$1") && console.log(balance))
function findBalance (array) {
return array.filter(({ balance }) => balance.startsWith("$1"))
}
console.log(findBalance(users))
// Вместо
{ balance: "$1,000.57" }
// Сделать так
{
balanceAmount: 1000.57,
balanceCurrency: '$'
}
// Или так
{
balance: {
amount: 1000.57,
currency: '$'
}
}
const balance = {
amount: 1768.67,
currency: '$'
}
console.log(`${balance.currency}${balance.amount.toLocaleString('en-EN')}`);
const balance = {
amount: 1768.67,
currency: 'USD'
}
console.log(balance.amount.toLocaleString('en-EN', { style: 'currency', currency: balance.currency }));
i
уже начинает указывать на следующий,for()
увеличит i
ещё на 1, и получится, что 1 элемент пропустили. Чинится уменьшением i
на 1 сразу после удаления 1 элемента.i
должно быть не меньше-или-равно, а меньше, чем длина массива.indexOf(value)
const duplicateArray = ['a', 'b', 'c', 'a', 'b', 'c', 'q'];
function funcDeleteDuplicate(array) {
for (let i = 0; i < array.length; i++) {
if (array.indexOf(array[i]) !== i) {
array.splice(i, 1);
i--;
}
}
return array;
}
console.log(funcDeleteDuplicate(duplicateArray));
// [ "a", "b", "c", "q" ]
const funcDeleteDuplicate = array => [...new Set(array)];
const duplicateArray = ['a', 'b', 'c', 'a', 'b', 'c', 'q'];
console.log(
Array.from(new Set(duplicateArray))
);
const duplicateArray = ['a', 'b', 'c', 'a', 'b', 'c', 'q'];
console.log(
duplicateArray.reduce(function (carry, item) {
if (!carry.includes(item)) carry.push(item);
return carry;
}, [])
);
const duplicateArray = ['a', 'b', 'c', 'a', 'b', 'c', 'q'];
console.log(
[...duplicateArray.reduce((carry, item) => [...carry].includes(item) ? carry : carry + item, '')]
);
letterElement.startsWith('A')
'Andrew'.startsWith('A') // true
'Dmitriy'.startsWith('A') // false