These are the questions I was asked In a frontend interview for a ReactJS position.
If you ask me to clone Netflix UI I will do It in shortest possible time, but if you ask me evaluate and string expression without eval()
. I might not able to do this.
Hope you guys will find these questions useful.
BTW I failed miserably 👶👶
because I was desperate to get the job and It leads to nervousness which killed my performance.
I will say
Be eager but never be desperate.
let result = [{x: 1}, {x: 2}, {x: 3}];
reduce this array and result should be 6; You are suggested to use arr.reduce() function.
Input:
people = [
{ name: 'Alice', age: 21, gender: "female" },
{ name: 'Max', age: 20, gender: "male" },
{ name: 'Jane', age: 20, gender: "female" }
{ name: 'Jon', age: 21, gender: "male" },
{ name: 'Alex', age: 20, gender: "male" }
]
output:
{
male: [
{ name: 'Max', age: 20, },
{ name: 'Jon', age: 21, },
{ name: 'Alex', age: 20, }
],
female: [
{ name: 'Alice', age: 21 },
{ name: 'Jane', age: 20 }
]
}
Input:
let value = "5+8=x";
Output should be 13 (type should be number);
comment section is yours fellows. 🙏🙏🙏
Thanks.
Top comments (11)
people.reduce(...)
answer is a masterpiece 👏🏻Masterpiece.
Using .filter for the second task
A more generic solution to group by any property:
Didn't understand It can you explain a little more.
I may use the implicit variables so that you could read easily, see my comments for the explanations
Then we can use
people.groupBy('gender')
orpeople.groupBy('age')
people = [
// { name: 'Alice', age: 21, gender: "female" },
// { name: 'Max', age: 20, gender: "male" },
// { name: 'Jane', age: 20, gender: "female" },
// { name: 'Jon', age: 21, gender: "male" },
// { name: 'Alex', age: 20, gender: "male" }
// ]
// const male=people.filter((el)=>el.gender=="male")
// const female=people.filter((el)=>el.gender=="female")
// console.log({male:male,female:female})
// let result = [{x: 1}, {x: 2}, {x: 3}];
// res=result.reduce((acc,el)=>acc+el.x,0)
let value = "5+8=x";
let arr=value.split("=")
arr=arr[0].split("+").map(Number).reduce((acc,e)=>acc+e)
console.log(arr)