2

OKAY, so I have a bunch of numbers in a div, lets say something like...

<div id="countme">7, 5, 6, 0, 3, 0, 5, 3, 3, 2, 8</div>

And I want to use JavaScript to return...

  • The specific number, and
  • The number of times that number occurred in the div

Example Output: "(2,0),(1,2),(3,3),(2,5),(1,6),(1,7),(1,8)"

Explained: Zero appears two times, two appears one time, three appears three times, etc...

I've tried the following...

var str = document.getElementById('countme').innerText;
var match = str.match(/7/g);
var match1 = str.match(/5/g);
alert(match.length);
alert(match1.length);

But I need it to display the number it searched for, and I need everything to be in one alert.

Any thoughts?

Thanks! :)

4 Answers 4

1

JSBIN: https://jsbin.com/tesezoz/1/edit?js,console

var str = "7, 5, 6, 0, 3, 0, 5, 3, 3, 2, 8";

// first get the numbers
var m = str.split(', ').map(Number);

// turn it into an object with counts for each number:

var c = m.reduce(function(a, b) {
  a[b] = ++a[b] || 1;
  return a;
}, {});

// now you have an object that you can check for the count
// which you can alert... its the c variable
Sign up to request clarification or add additional context in comments.

1 Comment

That's logically the same as the solution I was about to post; however, as a style critique, I try to avoid the pre-increment operator (e.g. ++a[b]). It tends towards poor readability, as do meaningless variable names such as 'a' and 'b' (except for where it's more or less idiomatic, such as sorting functions, but even then I'd probably prefer more descriptive names).
0

Try this...

var str = "7, 5, 6, 0, 3, 0, 5, 3, 3, 2, 8";
str = str.replace(/\s/g, "");
str = str.split(",");

var result = {};

str.forEach(function(value) {
    if (result[value]) {
        result[value]++;
    }
    else {
        result[value] = 1;
    }
});

var output = "";

for(value in result) {
    output += (output == "" ? "" : ",") + "(" + value + "," + result[value] +")";
}

alert(output);

It splits the string and removes any whitespace, so you're left with an array (and no assumption that the delimiter is consistent).

It then creates an object representing each value and the count.

It finally converts that into an output, similar to the one in your example.

Comments

0

Here is the Answer

var str    = document.getElementById('countme').innerText;
var array  = JSON.parse("[" + str+ "]");
var counts = {};
array.forEach(function(x) { counts[x] = (counts[x] || 0) + 1; });
console.log(counts);

Comments

0

I think this is almost as efficient as you can get. It also serves as a general count unique matches method:

var testString = document.getElementById('countme').innerText;
count = {}; 
var regX = /(\d+)/g;
var res;
while (res = regX.exec(testString )) {
    count[res[0]] = (count[res[0]] !== undefined ? ++count[res[0]] : 1)
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.