These are chat archives for angular/material

13th
Apr 2018
Kaushik Dhameliya
@dkaushikl
Apr 13 2018 04:45
Hello, how to use multiple themes in angular material
rk
@rajkumart08_twitter
Apr 13 2018 15:17

Hi,

  • I am new to js.
  • I am analysing a simple code.
  • not sure how it works.
  • for the first time this line console.log("alphabet--->" + alphabet); is printing undefined counts[alphabet]--->undefined
  • can you tell me why its printing undefined.
  • for the second time its printing 3 at this line console.log(counts[alphabet] + 1);
  • but it should print two right since 1+ 1.
  • can you guys explain so that in future I can fix it myself.
  • providing my code changes below.

var str = "aaabbccccddd";
var arr = str.split("");
var counts = {};
for (var i = 0; i < arr.length; i++) {
    var alphabet = arr[i];
    console.log("alphabet--->" + alphabet);
        console.log("counts[alphabet]--->" + counts[alphabet]);

    if (counts[alphabet]) 
        {
            counts[alphabet] = counts[alphabet] + 1;
            console.log("inside if--->");
            //console.log("inside if--->" + counts[alphabet] + 1);
            console.log(counts[alphabet] + 1);
        }
    else
    { 
        counts[alphabet] = 1;
        console.log("inside else--->");
        console.log(counts[alphabet]);

        //console.log("inside else--->" + counts[alphabet] + 1);
    }
    //counts[alphabet] = counts[alphabet] ? counts[alphabet] + 1 : 1;
}

console.log('a: ' + counts['a'] + '\n b: ' + counts['b'] + '\n c: ' + counts['c'] + '\n d: ' + counts['d'])

output

alphabet--->a
counts[alphabet]--->undefined
inside else--->
1
alphabet--->a
counts[alphabet]--->1
inside if--->
3
alphabet--->a
counts[alphabet]--->2
inside if--->
4
alphabet--->b
counts[alphabet]--->undefined
inside else--->
1
alphabet--->b
counts[alphabet]--->1
inside if--->
3
alphabet--->c
counts[alphabet]--->undefined
inside else--->
1
alphabet--->c
counts[alphabet]--->1
inside if--->
3
alphabet--->c
counts[alphabet]--->2
inside if--->
4
alphabet--->c
counts[alphabet]--->3
inside if--->
5
alphabet--->d
counts[alphabet]--->undefined
inside else--->
1
alphabet--->d
counts[alphabet]--->1
inside if--->
3
alphabet--->d
counts[alphabet]--->2
inside if--->
4
a: 3
b: 2
c: 4
d: 3

Tim Burnell
@tmburnell
Apr 13 2018 15:31
first pass you set count[alphabet] = 1 second pass you add 1, but then add another one on display (but does not retain the second addition)