Im trying to match two people whoses sum (sum relates to the values added up in the questions they were asked) equals each other or find someone who is closest.
Used .map to return whats up above except a sum of scores instead of individual ones I expected the output to be result[2] which is (Mia Vobos) because her score of three is closest to the total of 2, but i get element.reduce is not a function.
My array of people and their scores
var peopleArray = [
{
name: "Hector Valdes",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "1",
"2", "5",
"4", "1"
]
}, {
name: "Tyler Williams",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "2",
"2", "5",
"4", "1"
]
}, {
name: "Mia Vobos",
photo: "",
scores: [
"2", "1",
]
}
]
var total = 2
const result = peopleArray.map((value) => {
return {
name: value.name,
score: value.scores.reduce((total, score) => total + Number(score), 0)
}
});
console.log(result);
for (let i = 0; i < result.length; i++) {
const element = result[i].score;
if (total == result[i].score) {
console.log(result[i])
} else {
var closest = element.reduce(function(prev, curr) {
return (Math.abs(curr - total) < Math.abs(prev - total) ? curr : total);
});
}
}
console.log(closest);
First, element.reduce will not work because element is not an array. See mdn docs for reduce: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
Secondly, here's a start/general outline. Be aware that this is probably more advanced than you are used to, but I left it open for you to edit and added my comments there. Full disclosure: You probably don't want to use typescript if it's for a class that doesn't use typescript.
https://codepen.io/anon/pen/MMKEvg?editors=1012
const peopleArray = [
{
name: "Hector Valdes",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "1",
"2", "5",
"4", "1"
]
}, {
name: "Tyler Williams",
photo: "",
scores: [
"5", "1",
"4", "4",
"5", "2",
"2", "5",
"4", "1"
]
}, {
name: "Mia Vobos",
photo: "",
scores: [
"2", "1",
]
}
]
function getClosestMatch(total:number) {
// First add the totals to each person
peopleArray = peopleArray.map((person) => {
person = {
...person,// This is called destructuring (look it up)
total: // Insert your code here, and make sure to do parseInt(stringNumberValue, 10)
}
return person;
})
// Then just get the closest match
var closestMatchingPerson = peopleArray.reduce(function(prev, curr) {
return // Calculate the difference here, and return either previous or current
});
return closestMatchingPerson;
}
getClosestMatch(31);
Additional comments: Did you notice the ': number' part in the function getClosestMatch? You can remove that part if you're not using typescript. I do recommend learning typescript if you want to be a frontend / javascript engineer though!
How we can insert header and footer in google docs with google docs api using PHP code
Writing a new and appending a file in PHP without erasing contents
How to combine 2 arrays with the condition that 2 and 5 values will be from the second array?
How to keep the ?error on url if page extension not included?
Cannot find the answer after my research, help me with mysql [duplicate]
Can someone describe the purpose of [thisprops
So I made a chrome extension to blur and unblur (when hovered) some profanity words that I'm storing in a json fileScript works fine for almost all sites I tested but makes the page loading slow for some sites like facebook and twitter (very poor perfomance...
I'm trying to draw a multi-line line graph in d3, and then update it on every tickThe problem I'm experiencing is that I end up with either an error a graph with zero data
I'm creating a little financial calculatorThis is a first time thing