0

The code below is the code that I have written:

function singer(artist) {
    var songs = [];
    for(var i = 0; i < music.length;i++ ){
        if(music[i].artist.indexOf(artist) > -1) {
            songs.push(music[i].name);
        }
    }
    return songs;
}

The code that I want to look similar to the function singer(artist) code is this:

const genreCount = () => {
    const genres =  music.reduce((result, cur) => {
        cur.genres.forEach(g => {
            if (result.hasOwnProperty(g)) {
                result[g] += 1;
            }
            else
                result[g] = 1;
        });

        return result;
    }, {});
    return genres;
}

I am unfamiliar with this type of format in Javascript, how would I change it so that const genreCount will look like function singer(artist).

waldo233
  • 61
  • 7
  • Is there a specific purpose of changing the format? If you want to learn about the syntax see any JS tutorial, e.g. [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780/4642212) and the documentation on MDN about [expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators) and [statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements). – Sebastian Simon Nov 13 '19 at 05:46
  • Keep in mind the readability of both the formats – Krishna Prashatt Nov 13 '19 at 05:52
  • `const singer=artist=>music.filter(({artist:a})=>a.includes(artist)).map(({name:n})=>n);` – Bravo Nov 13 '19 at 05:53
  • I am still fairly new to Javascript and I'm unsure of how to really understand the second format. – waldo233 Nov 13 '19 at 05:53
  • Use [babel REPL](https://babeljs.io/en/repl#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=MYewdgzgLgBA5gUzAJwQYRAVzLAvDACgEoZcA-GAbwCgYZRJZEUEJSYBbTCAS2ADpUAE0zAEBAqgiYANlAA09TMhLkqtOkuT9mU_gDMQyAKIBDYAAsCcUhRqbNPfYSmyo_C6YgB5AO5gABWQQAAcEZCgAT2siIg0HOlc5AG04AF0YAGp8AEYAbnjNBBkIBEKHJKhUjNyChwBfIgLC1ChlMBhKuph6xUpG7tb2-CQpAvqgA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2&targets=&version=7.7.3&externalPlugins=) to transpile – adiga Nov 13 '19 at 06:48

2 Answers2

0

this style is called functional programming

const singer = artist => music.filter(m => artist.indexOf(m.artist) > -1).map(m => m.name)

here is a good interactive tutorial if you are interested Functional Programming in Javascript

UPDATE: oops, sorry for misunderstanding your questions here is genreCount rewritten with for-loop:

function genreCount(){
  const genres = {};
  for(var i=0; i<music.length; i++){
    var g = music[i]
    if (genres.hasOwnProperty(g)) {
      genres[g] += 1;
    }
    else{
      genres[g] = 1;
    }
  }
  return genres;
}
R Pasha
  • 641
  • 5
  • 19
  • I think you may have misunderstood the question that I asked. I want the genreCount() function to look similar to the code of function singer(artist). The reason why I want that is because I don't really understand the Javascript format for genreCount(). – waldo233 Nov 13 '19 at 06:43
0

This is what you will get if you want to change that function:

function genreCount() {
    const genres =  music.reduce(function(result, cur) {
        cur.genres.forEach(function(g) {
            if (result.hasOwnProperty(g)) {
                result[g] += 1;
            }
            else
                result[g] = 1;
        });

        return result;
    }, {});
    return genres;
}

or (if you want to assign that fucntion to a const anyway):

const genreCount = function() {
    const genres =  music.reduce(function(result, cur) {
        cur.genres.forEach(function(g) {
            if (result.hasOwnProperty(g)) {
                result[g] += 1;
            }
            else
                result[g] = 1;
        });

        return result;
    }, {});
    return genres;
}

You just should replace arrow functins with the regular function expressions. But I don't know why do you need that.

Daniyal Lukmanov
  • 969
  • 6
  • 19