- Add a function named
findByArtist. This function should:- Take an input parameter for a string artist
- Create an array to hold any results, empty to start
- Loop through the
collectionand add any objects with a matching title to the array. - Return the array with the matching results. If no results are found, return an empty array.
Collection is my an arrray with multiple objects in it like this:
0: {TITLE: "Welcome Home", ARTIST: "Aries", YEAR: "2020"}
1: {TITLE: "Appeal To Reason", ARTIST: "Rise Against", YEAR: "2008"}
2: {TITLE: "Endgame", ARTIST: "Rise Against", YEAR: "2011"}
3: {TITLE: "Wolves", ARTIST: "Rise Against", YEAR: "2017"}
4: {TITLE: "Sleepy Little One", ARTIST: "Kupla", YEAR: "2020"}
5: {TITLE: "Kingdom in Blue", ARTIST: "Kupla", YEAR: "2020"}
length: 6
PROBLEM: I can't get the function work as it always returns 'artist not found in collection' even when I type the artist string when I run the function. For example:
console.log(findByArtist('Rise Against'));
function findByArtist(artist) {
let searchArtist = [];
for (let i = 0; i < collection.length; i++) {
const element = collection[i];
if (artist == element.ARTIST) {
searchArtist.push(element);
return searchArtist;
} else {
console.log('Artist not found in collection');
return searchArtist;
}//End if else
}//End for loop
}//End function