function TotalAwards(fullName){
var medals = getColumn("Olympic Medals", "Medal");
var firstName = getColumn("Olympic Medals", "Athlete First Name");
var lastName = getColumn("Olympic Medals", "Athlete Last Name");
for(var i=0; i<medals.length; i++){
if (firstName[i] + " " + lastName[i] == fullName){
return medals[i];
}
}
return "Not found";
}
So this code is to call on the awards won by a particular winner. What's wrong here is that it only gets the first award of the winner. the second , third , fourth awards are not included. Can anyone help me to fix this?
console.log (TotalAwards(" Mathew Helm", true));
when I tried with console log to list down the awards of mathew helm, it only prints out his first medal which is silver. The others awards he received in the later years are'nt printed put
You are only getting the first award of the winner because you call return if the first condition is met. The solution is to add the values to an array and return the result.
function totalAwards(fullName) {
var medals = getColumn("Olympic Medals", "Medal");
var firstName = getColumn("Olympic Medals", "Athlete First Name");
var lastName = getColumn("Olympic Medals", "Athlete Last Name");
var result = [];
for (var i = 0; i < medals.length; i++) {
if (firstName[i] + " " + lastName[i] === fullName) {
result.push(medals[i]);
}
}
return result;
}
One alternative to the for loop is the filter function. You can read more about it in the mdn docs. It would be something like this:
function totalAwards(fullName) {
const medals = getColumn("Olympic Medals", "Medal");
const firstName = getColumn("Olympic Medals", "Athlete First Name");
const lastName = getColumn("Olympic Medals", "Athlete Last Name");
return medals.filter((medal, index) => {
return firstName[index] + " " + lastName[index] === fullName
})
}
I'm using the const variable declaration because const and let are the recommended way of declaring variables in JavaScript. This JavaScript Info chapter explains it very well and in this one they go deep into the differences.
This is because you exit the loop on first match. What you need is load the results into an array and return it instead:
function TotalAwards(fullName) {
var medals = getColumn("Olympic Medals", "Medal");
var firstName = getColumn("Olympic Medals", "Athlete First Name");
var lastName = getColumn("Olympic Medals", "Athlete Last Name");
let result = [];
for (var i = 0; i < medals.length; i++) {
if (firstName[i] + " " + lastName[i] == fullName) {
result.push(medals[i]);
}
}
return result;
}
function TotalAwards(fullName){
const medals = getColumn("Olympic Medals", "Medal");
const firstName = getColumn("Olympic Medals", "Athlete First Name");
const lastName = getColumn("Olympic Medals", "Athlete Last Name");
let result = []
medals?.map((medal, index) => {
firstName[index] + " " + lastName[index] == fullName && result.push(medal)
})
if (result.length === 0) {
return "Not found";
}
return result;
}
I Think it works properly, it's better use const instead of var and let when the variable value could change its value during the progress. The conditional operator '?' check that medals exists, otherwise won't loop.