-1

I'm trying to create a program that condenses a string to how many characters it has. Just to give my non-programming friends an idea of how compression can work.

Unfortunately, I can't seem to put the same alphabets into a single arrays and other same sets of alphabets in other respective arrays.

The code is not complete, as I lost a lot of data from my HDD and this was all I had backed up on Google Drive. Can anyone please help me with this? Thanks!

var string = "twinkle twinkle little star how i wonder what you are up above the world so high like a diamond in the sky twinkle twinkle little star"

var brokenString = string.split("")

var counter = 0;
for (i = 1; i < brokenString.length; i++) {
  while (brokenString[counter] === brokenString[i]) {
    //var eval("array" + counter) = new Array(brokenString[i])
    var array = {
      counter:  brokenString[i]
    }
  counter++
  }
}

console.log(array)
9
  • I don't think new Array(brokenString) does what you think it does. It creates a new array with one element, and that element is the brokenString array. Commented Jun 24, 2016 at 0:05
  • Actually everything till that line works exactly as I intended it to. It's the loop that I can't get to work. It creates an element out of every single character of the original string and puts it into an array called brokenString Commented Jun 24, 2016 at 0:06
  • Never mind, now I see that you're not assigning new Array(brokenString) to anything. What's the purpose of that line? brokenString is already an array, calling new Array() doesn't do anything to it. Commented Jun 24, 2016 at 0:08
  • What's the purpose of var array? You're creating that object, but not doing anything with it. Commented Jun 24, 2016 at 0:10
  • Yes, I realized that the new Array(brokenString) is useless. And now I understand your second comment as well. Thank you for that! Commented Jun 24, 2016 at 0:12

1 Answer 1

1

Use an object whose keys are the characters and values are the count of those characters.

var string = "twinkle twinkle little star how i wonder what you are up above the world so high like a diamond in the sky twinkle twinkle little star"

var letters = {};
for (var i = 0; i < string.length; i++) {
  var char = string[i];
  if (char in letters) {
    letters[char]++;
  } else {
    letters[char] = 1;
  }
}

console.log(letters);

Sign up to request clarification or add additional context in comments.

1 Comment

Sir @Barmar, thank you so much. I'm sorry that you had to work through an improperly structured question and its code but thank you for your help! If I may ask, is there any resource in particular that you can refer me to so that I can get better at JS? I've already done online practices at codecademy and SoloLearn, which don't really teach much. Would really appreciate your input in this matter. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.