Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (commandArr) {
- class Farmer {
- constructor (name, location) {
- this.name = name;
- this.location = location;
- this.skills = []
- }
- }
- let farmerArr = [];
- let numFarmers = Number(commandArr[0]);
- for (let idx = 1; idx <= numFarmers; idx++) {
- let farmerStr = commandArr[idx];
- let [name, location, skills] = farmerStr.split(' ');
- let farmer = new Farmer(name, location);
- let skillArr = skills.split(',');
- for (let skill of skillArr) {
- farmer.skills.push(skill);
- }
- farmerArr.push(farmer);
- }
- for (let idx = numFarmers + 1; idx < commandArr.length; idx++) {
- currentCommandTokens = commandArr[idx].split(' / ');
- if (currentCommandTokens[0] === "End") {
- break;
- } else if (currentCommandTokens[0] === "Execute") {
- let [_, name, location, task] = currentCommandTokens;
- let currentFarmer = farmerArr.find(p => p.name === name);
- if (!currentFarmer) {
- continue;
- }
- if (currentFarmer.location !== location) {
- console.log(`${currentFarmer.name} cannot execute the task: ${task}.`);
- continue;
- }
- if (!currentFarmer.skills.includes(task)) {
- console.log(`${currentFarmer.name} cannot execute the task: ${task}.`);
- continue;
- }
- console.log(`${currentFarmer.name} has executed the task: ${task}!`)
- } else if (currentCommandTokens[0] === "Change Area") {
- let [_, name, location] = currentCommandTokens;
- let currentFarmer = farmerArr.find(p => p.name === name);
- if (!currentFarmer) {
- continue;
- }
- if (currentFarmer.location !== location) {
- currentFarmer.location = location;
- console.log(`${currentFarmer.name} has changed their work area to: ${location}`);
- }
- } else if (currentCommandTokens[0] === "Learn Task") {
- let [_, name, task] = currentCommandTokens;
- let currentFarmer = farmerArr.find(p => p.name === name);
- if (!currentFarmer) {
- continue;
- }
- if (currentFarmer.skills.includes(task)) {
- console.log(`${currentFarmer.name} already knows how to perform ${task}.`);
- } else {
- currentFarmer.skills.push(task);
- console.log(`${currentFarmer.name} has learned a new task: ${task}.`);
- }
- }
- }
- for (let farmer of farmerArr) {
- console.log(`Farmer: ${farmer.name}, Area: ${farmer.location}, Tasks: ${farmer.skills.sort().join(', ')}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement