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

241
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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