Replace Arrow Function in ES5

2

Because the page where I want to insert the following line of code, how can I replace the arrow function so that I can run in the ES5 version?

const frutas = ["Banana", "Orange", "Apple", "Mango","Orange"];

const contarFrutas = (valor, listaDeFrutas) => (
 listaDeFrutas.filter(fruta => fruta === valor).length
); 

 console.log(
 contarFrutas('Banana', frutas)
);

console.log(
 contarFrutas('Orange', frutas)
);
    
asked by Rafael Pereira 30.07.2018 в 22:18
source

2 answers

1

Although that's fine, the substitution would really be:

function contarFrutas(valor, listaDeFrutas) {
  return listaDeFrutas.filter(function (fruta) {
    return fruta === valor;
  }).length;
};

Since it is not necessary to assign the function to a variable, simply simply declare the function without more.

And you would call her as they have put you in the other comment:

console.log(contarFrutas('Banana', frutas));
console.log(contarFrutas('Orange', frutas));
    
answered by 31.07.2018 / 14:18
source
2

I leave it as executable so you can review it

"use strict";

var frutas = ["Banana", "Orange", "Apple", "Mango", "Orange"];

var contarFrutas = function contarFrutas(valor, listaDeFrutas) {
  return listaDeFrutas.filter(function (fruta) {
    return fruta === valor;
  }).length;
};

console.log(contarFrutas('Banana', frutas));

console.log(contarFrutas('Orange', frutas));
  

If you mention the ES5 standard, pass the declaration of variables   to the type var , likewise the declaration of the arrow functions was passed to > the declaration of habitual functions

    
answered by 30.07.2018 в 22:27