Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda