1
  • 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 collection and 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

2 Answers 2

3

Lemme simplify your code. It will give you a clean code structure. Always try to go with ES6.

You can simply use Array.filter() instead of loops.

const data = [
 {TITLE: "Welcome Home", ARTIST: "Aries", YEAR: "2020"},
 {TITLE: "Appeal To Reason", ARTIST: "Rise Against", YEAR: "2008"},
 {TITLE: "Endgame", ARTIST: "Rise Against", YEAR: "2011"},
 {TITLE: "Wolves", ARTIST: "Rise Against", YEAR: "2017"},
 {TITLE: "Sleepy Little One", ARTIST: "Kupla", YEAR: "2020"},
 {TITLE: "Kingdom in Blue", ARTIST: "Kupla", YEAR: "2020"}
];

function findByArtist(artist) {
  return data.filter(record => record["TITLE"] === artist);
}

const filteredResults = findByArtist("Kingdom in Blue");

if ( !filteredResults.length ) {
  alert("Album not found in collection");
}

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the filter method in order to filter all the elements with the artist that was received as an input and then return the string if there were no elements with this artist name or the filtered list of elements with this artist name like so:


    function findByArtist(artist) {
      let searchArtist = collection.filter(element => element.ARTIST === artist);

      if(searchArtist.length === 0) return 'Artist not found in collection';
      return searchArtist;
    }//End function


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.