'Evaluate and give the output in 2 arrays
const orderList = [["paul", "enter"],["jenny", "exit"],["paul", "exit"],["phil", "enter"],["shriya", "exit"],["shriya", "enter"],["shriya", "exit"],["paul", "enter"]["phil", "exit"],["Dan", "exit"],["Dan", "enter"]]
Following the above order pls evaluate and give the output in 2 arrays, one for wrong entry and other for wrong exit.
Order is important.
Example: Paul has entered and exited which is correct. Again he has entered but didn't exit. So he comes in wrong exited array.
Solution 1:[1]
If I guessed it correctly then this is what you are looking for.
function check(orderList) {
eventCount = {};
for(let i = 0; i < orderList.length; i++){
if(orderList[i]){
let person = orderList[i][0];
console.log(person, "--> ",orderList[i][1] );
if(!eventCount.hasOwnProperty(person)){
eventCount[person] = 0;
}
if(orderList[i][1] === "enter"){
eventCount[person] = eventCount[person]+1;
} else {
eventCount[person] = eventCount[person]-1;
}
}
}
let right = [];
let wrong = [];
for (const person in eventCount) {
if(eventCount[person] === 0) {
right.push(person);
}else {
wrong.push(person);
}
}
console.log("right -> ", right);
console.log("wrong -> ", wrong);
}
const orderList = [["paul", "enter"],
["jenny", "exit"],
["paul", "exit"],
["phil", "enter"],
["shriya", "exit"],
["shriya", "enter"],
["shriya", "exit"],
["paul", "enter"],["phil", "exit"],["Dan", "exit"],["Dan", "enter"]];
check(orderList);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TechnoTech |
