Работа с объектом и массивом?

По заданию мне нужно написать функцию hireNewEmployee, которая будет принимать в параметры объект employee и возвращать 'You're Hired! Congrats!' или 'No hired: sorry we cannot hire you. Here is why: ', reason). Где у reason будет массив свойств по которым не подошел работник.
К примеру:
ограничение в возрасте – не моложе 25 лет на эту должность
образование: высшее
прошлый опыт: минимум 1 год.
Примечание: создавать объект employee через defineProperty с указанием дескрипторов.
Пробовал решить, код не работает, в консоль выдает ошибку.
const employee = {};
const reason = [
    {
        age: 25,
        education: 'higher',
        experience: 1
    }
]
Object.defineProperties(employee, {
    name: {
        value: 'Dmitriy',
        writable: false,
        configurable: false,
        enumerable: false
    },
    age: {
        value: 21,
        writable: true,
        enumerable: true,
        configurable: true
    },
    education: {
        value: 'higher',
        writable: true,
        enumerable: true,
        configurable: true
    },
    experience: {
        value: 0,
        writable: true,
        enumerable: true,
        configurable: true
    }
})
function hireNewEmployee (objectEmployee,objectReason) {
    for (let key in objectEmployee) {
        if (objectEmployee.key['age'] >= objectReason.key['education']
            && objectEmployee.key['education'] === objectReason.key['education']
            &&  objectEmployee.key['experience'] >= 1) {
            return console.log('You are Hired! Congrats!');
        }
    }
    return console.log(`Not hired: sorry we cannot hire you. Here is why: ${objectReason}`);
}
hireNewEmployee(employee,reason);
  • Вопрос задан
  • 158 просмотров
Решения вопроса 1
sergiks
@sergiks Куратор тега JavaScript
♬♬
for (let key in objectEmployee) {
что будет в key?

objectEmployee.key['age']
есть ли у objectEmployee свойство key? (нет)

spoiler
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"
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы