Не корректно работает код
Проблема заключается в том что либо добавляются все ключи в массив reasons либо ни один. А должны добавляться те которые прошли проверку
const filters = {
age: function (element) {
return element >= 25;
},
education: function (element) {
return element === 'higher'
},
experience: function (element) {
return element >= 1
}
}
const employee = {};
const defaultDescriptors = {
writable: true,
enumerable: true,
configurable: true
}
Object.defineProperties(employee, {
name: {
...defaultDescriptors,
value: 'Dmitriy',
writable: false
},
age: {
...defaultDescriptors,
value: 21
},
education: {
...defaultDescriptors,
value: 'higher'
},
experience: {
...defaultDescriptors,
value: 0
}
})
function hireNewEmployee (employee,filters) {
const reasons = [];
Object.entries(filters).forEach(([name]) => {
if (filters.experience(employee.experience) === false) {
reasons.push(name);
} else if (filters.education(employee.education) === false) {
reasons.push(name);
} else if (filters.experience(employee.experience) === false) {
reasons.push(name);
}
})
if (reasons.length > 0) {
return `Not hired: sorry we cannot hire you. Here is why: ${reasons.join(', ')}`
}
else {
return 'You are Hired! Congrats!'
}
}
console.log(hireNewEmployee(employee,filters))
Ответы (1 шт):
Автор решения: Smile
→ Ссылка
Решил вот так:
function hireNewEmployee (employee,filters) {
const reasons = [];
Object.entries(filters).forEach(([name,func]) => {
if (func(employee[name]) === false) {
reasons.push(name);
}
})
if (reasons.length > 0) {
return `Not hired: sorry we cannot hire you. Here is why: ${reasons.join(', ')}`
}
else {
return 'You are Hired! Congrats!'
}
}
console.log(hireNewEmployee(employee,filters));