-1

I have the following code:

var str = "abcabcABCABC"
var chars = str.split("");
var lettersCount = {};
for (var i = 0; i < chars.length;i++)
{
    if (lettersCount[chars[i]] == undefined )
        lettersCount[chars[i]] = 0;
    lettersCount[chars[i]] ++;
}
for (var i in lettersCount)
{
    console.log(i + ' = ' + lettersCount[i]);
}

This code is counting how many same letters are in a word. What am I trying is to convert the uppercase letters to lowercase so it should show like this: a - 4, b -4, now it shows: a - 2, A - 2. I've just started with Js so please be good with me. :)

buft
  • 11
  • 1
  • 1
    [str.toLowerCase()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase)? – Patrick Evans Nov 19 '19 at 12:39
  • try assigning a variable and using it after changing its case `let c = chars[i].toLowerCase(); if (lettersCount[c] == undefined)` – shrys Nov 19 '19 at 12:40

1 Answers1

0

If you just need the string to be converted into lowercase letter then you can do it like this:-

var str = "abcabcABCABC";
var newStr = str.toLowerCase();
console.log(newStr);

Hope this helps.

Shivratna Kumar
  • 1,311
  • 1
  • 8
  • 18
  • Thanks. It worked! :) It was so simple, i don't know how I didn't figure it out. – buft Nov 19 '19 at 12:49
  • @buft Np, happy to help!! Please accept the answer, so that it can help other people as well. – Shivratna Kumar Nov 19 '19 at 12:51
  • For questions like those, please see if there is a duplicate first. The goal of Stack Overflow is to be a helpful resource to future visitors and [duplicates make sure that all the good and up-to-date content is in one place](https://stackoverflow.com/help/duplicates). In this case [there is already a post](https://stackoverflow.com/questions/154862/convert-javascript-string-to-be-all-lower-case) with 1200+ upvotes and 14 answers. – Ivar Nov 19 '19 at 14:10
  • 1
    @Ivar Noted, thanks. – Shivratna Kumar Nov 19 '19 at 14:12