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

230
Visualizações
Unable to update Object property number (counter) for counting alphabet

I am new to JavaScript, and I am trying to count number of each alphabet in the bigLettersArr. I made a for loop to go through each element in bigLettersArr and update the counter, but somehow it doesn't work. Again, all I am trying to do is to update alphabet object counter to get number of each leters.

Also, I think there might be much smarter way to do this, I looked up other methods for array, but was not able to find and come up with better code.

Here's my code. Thank for reading this!

const bigLettersArr = [ "X","E","Q","M","L","L","L","B","X","C","A","F",
                        "X","W","B","W","A","F","D","Z","B","C","L","F",
                        "M","E","B","E","A","M","L","L","B","G","L","G",
                        "H","F","H","G","M","A","F","B","B","C","G","F",
                        "H","Z","B","L","L","H","J","L"]

//made an empty array with all alphabet A-Z 
const alphabet = {
                    'A': 0, 'B': 0, 'C': 0, 'D': 0, ... 'Z':0
                 }
             //Count how many each alphabet in the array.   
                for(let letter in bigLettersArr){
                    let targetLetter = bigLettersArr[letter]
                    // console.log(targetLetter)
                    alphabet.targetLetter += 1
                }


            console.log(alphabet)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Arrays are iterables in JS. so you can simply use for..of loop here

You should use for..in loop with objects and for..of with arrays

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.-MDN

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.-MDN

You can also use forEach instead of for..of as:

bigLettersArr.forEach(letter => alphabet[letter] += 1);

const bigLettersArr = ["X", "E", "Q", "M", "L", "L", "L", "B", "X", "C", "A", "F",
    "X", "W", "B", "W", "A", "F", "D", "Z", "B", "C", "L", "F",
    "M", "E", "B", "E", "A", "M", "L", "L", "B", "G", "L", "G",
    "H", "F", "H", "G", "M", "A", "F", "B", "B", "C", "G", "F",
    "H", "Z", "B", "L", "L", "H", "J", "L"]

const alphabet = Array
.from( { length: 26 }, ( _, i ) => String.fromCharCode( i + 65 ))
.reduce( ( acc, curr ) => ( ( acc[curr] = 0 ), acc ), {} );

for ( let letter of bigLettersArr ) {
    alphabet[letter] += 1
}

console.log( alphabet )
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

You are trying to increment the targetLetter property.

Instead, use bracket notation:

for (let letter in bigLettersArr) {
    let targetLetter = bigLettersArr[letter]
    alphabet[targetLetter] += 1
}
about 4 years ago · Juan Pablo Isaza Relatório

0

my way...

const bigLettersArr = 
  [ 'X', 'E', 'Q', 'M', 'L', 'L', 'L', 'B', 'X', 'C'
  , 'A', 'F', 'X', 'W', 'B', 'W', 'A', 'F', 'D', 'Z'
  , 'B', 'C', 'L', 'F', 'M', 'E', 'B', 'E', 'A', 'M'
  , 'L', 'L', 'B', 'G', 'L', 'G', 'H', 'F', 'H', 'G'
  , 'M', 'A', 'F', 'B', 'B', 'C', 'G', 'F', 'H', 'Z'
  , 'B', 'L', 'L', 'H', 'J', 'L'
  ] 

const alphabet = Object.fromEntries(Array.from({length:26},(_,i)=>[String.fromCharCode('A'.charCodeAt(0)+i),0]))

bigLettersArr.forEach( letter => { alphabet[letter]++ })

console.log( alphabet )
.as-console-wrapper { max-height: 100% !important; top: 0 }

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