1

I'm learning JavaScript and I came across the following situation:

My code:

alfabetoMadegues = "jngmclqskrzfvbwpxdht"

var listaLetras = Array.from(alfabetoMadegues);

dicionario_Madegues = {};

for (var i = 0 < listaLetras.length; i++;) {
    listaLetras.forEach(element => {

        dicionario_Madegues[element] = i;
    })
};

If "j" is the first element of the array Why "j" is receiving 2 instead 0?. Why am i getting this result?

The expected result should be:

"j" 0
"n" 1
"g" 2
"m" 3
"c" 4
"l" 5
Leonardo Lima
  • 586
  • 1
  • 6
  • 20
  • Can you please include the result you are getting? – CSSBurner Jun 19 '18 at 14:39
  • 4
    You are running it through two loops, the `for` and the `forEach`. What is the purpose of this? – dmgig Jun 19 '18 at 14:39
  • You should really first of all explain what your code is supposed to do, in your expectation, instead of letting people guess. – CBroe Jun 19 '18 at 14:40
  • What does this suppose to do `for (var i = 0 < listaLetras.length; i++;)` ? You are starting with `i === true` and then increment it – Yury Tarabanko Jun 19 '18 at 14:40
  • I'm basically want to get the value position of each element of the array listaLetras and set the result of the object dicionario_Madegues – Leonardo Lima Jun 19 '18 at 14:41
  • Possible duplicate of [Javascript: Process each letter of text](https://stackoverflow.com/questions/1966476/javascript-process-each-letter-of-text) – Liam Jun 19 '18 at 14:42
  • A "i" is missing before "< listaLetras". – Leonardo Lima Jun 19 '18 at 14:43

5 Answers5

6

ES6 Soluce aka ECMA2015 - see compatibility table


As second parameter of Array.forEach you can get the position of the element; as example :

const alfabetoMadegues = 'jngmclqskrzfvbwpxdht';

const listaLetras = Array.from(alfabetoMadegues);

const dicionario_Madegues = {};

listaLetras.forEach((x, xi) => {
  dicionario_Madegues[x] = xi;
});

console.log(dicionario_Madegues);

Also, the use of Array.reduce is more appropriate; as example

PS: We use of Spread operator ... and dynamic key naming [x].

const alfabetoMadegues = 'jngmclqskrzfvbwpxdht';

const listaLetras = Array.from(alfabetoMadegues);

const dicionario_Madegues = listaLetras.reduce((tmp, x, xi) => ({
  ...tmp,
  
  [x]: xi,
}), {});

console.log(dicionario_Madegues);


ES5 Soluce


var alfabetoMadegues = 'jngmclqskrzfvbwpxdht';

var listaLetras = Array.from(alfabetoMadegues);

var dicionario_Madegues = {};

for (var i = 0; i < listaLetras.length; i += 1) {
  dicionario_Madegues[listaLetras[i]] = i;
}

console.log(dicionario_Madegues);
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
4

You don't want that forEach inside the for. For the output you've described, you want to just remove that and replace it with dicionario_Madegues[listaLetras[i]] = i;.

You also have an error in your for loop. You have:

for (var i = 0 < listaLetras.length; i++;) {

...which will run forever. The initialization and test should be separated with ;:

for (var i = 0; i < listaLetras.length; i++) {
// -----------^^^--------------------------^ (no `;` after `i++`)

Live example:

var alfabetoMadegues = "jngmclqskrzfvbwpxdht";

var listaLetras = Array.from(alfabetoMadegues);

var dicionario_Madegues = {};

for (var i = 0; i < listaLetras.length; i++) {
    dicionario_Madegues[listaLetras[i]] = i;
}

console.log(dicionario_Madegues);

Also note the various places I've removed and added ;. It's probably worth reviewing the rules for them.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You are looping two times:

for (var i = 0 < listaLetras.length; i++;) { // HERE
    listaLetras.forEach(element => { //HERE
        dicionario_Madegues[element] = i;
    })
};

So you have to use either the for or the forEach:

alfabetoMadegues = "jngmclqskrzfvbwpxdht"

var listaLetras = Array.from(alfabetoMadegues);

var dicionario_Madegues_CON_FOR = {};
var dicionario_Madegues_CON_FOREACH = {};

for (var i = 0 ; i < listaLetras.length ; i++ ) {
  dicionario_Madegues_CON_FOR[listaLetras[i]] = i;
};

listaLetras.forEach((letra, indice) => dicionario_Madegues_CON_FOREACH[letra] = indice);

console.log(dicionario_Madegues_CON_FOR);
console.log(dicionario_Madegues_CON_FOREACH);

There are also sintax errors in your for:

for (var i = 0 < listaLetras.length; i++;)

That should be

for(var i = 0 ; i < listaLetras.length ; i++ )
J. Pichardo
  • 3,077
  • 21
  • 37
0

In addition to the provided answers you also can consider the following approach:

alfabetoMadegues = "jngmclqskrzfvbwpxdht"

var obj = {}

alfabetoMadegues.split('').forEach((element, index) => obj[element] = index)

console.log(obj)
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
0

There is a mistake on this line

for (var i = 0 < listaLetras.length; i++;) {

The for construct has three sections:

  • Initilisation
  • Condition
  • Afterthought

Each section is separated with a ;

You've smooshed your condition (i < listaLetras.length) and initialisation together. The afterthought is being left blank.

The correct code would be

for (var i = 0; i < listaLetras.length; i++) {

The reason j is 2 is for two reasons;

On the first pass of the loop i is getting set the the value of i < listaLetras.length - this evaluates to true.

Next the loop runs the comparision (to check whether it should run the body of the loop). The comparision here is .... i++ ! i is true so the comparison passes and then i is incremented.

i's value true is getting implicitly cast to a 1 for the increment.

When we hit the body of the loop it is now 2.

You are also looping two times with the foreach function - setting each value to i every iteration of i that occurs.

Capricorn
  • 2,061
  • 5
  • 24
  • 31
Ionsto
  • 1