Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
How to create a reusable array.reduce function

Im working on solving an Anagram problem wanted to get clever with Javascript methods

here is my code

const stringA = ["hello"]
const stringB = ["olhle"]
let cleanStrA = stringA.replace(/[^\w]/g, '').toLowerCase().split('')
let cleanStrB = stringB.replace(/[^\w]/g, '').toLowerCase().split('')

function charMap (str) {
    str.reduce((acc, cur) => {
        acc[cur] = acc[cur] + 1 || 1 
        return acc
    },{})
    return str
}

let buildCharMapA = charMap(cleanStrA)
let buildCharMapB = charMap(cleanStrB)
console.log(buildCharMapA)

result = ("hello")

When i console log this it returns the original array, but when i remove the function encompassing the reduce method it create the intended object

    let reduceFn = cleanStrA.reduce((acc, cur) => {
        acc[cur] = acc[cur] + 1 || 1 
        return acc
    },{})
console.log(reduceFn)
result = {h:1, e:1, etc.}

What gives?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As the comments pointed out i mistakenly returned str instead of the entire reduce function

function charMap (str) {
    return str.reduce((acc, cur) => {
        acc[cur] = acc[cur] + 1 || 1 
        return acc
    },{})

}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda