2

Hello(brand new to Javascript), I have a simple program that displays a prompt pop up asking for input from several different visiting users.

ex: <script type="text/javascript> var spectrum = prompt:", ""); </script>

see..Pretty simple code right lol (im new)

I'm trying to figure out a code that'll search through input of users to display the most common word.

Is there a program i can write that'll select the most common word and document.write it?

any help?

Its okay if there are multiple answers to this question because I'm stumped right now and would like to hear anyone's advice on the subject.

Mousey
  • 1,855
  • 19
  • 34
clsv
  • 21
  • 3
  • 2
    Java or Javaccript? I think you meant to say "brand new to JavaScript". Java and Javascript are two different languages. – John Giotta Sep 09 '15 at 14:13
  • @JohnGiotta it's clearly javascript from the code – Mousey Sep 09 '15 at 15:43
  • 1
    Are you looking for a pseudo solution? e.g. emulating the visits and a prompt, storing it in array and getting the most occurring element from that array. Or an actual live implementation? The latter depends on more than code, you'd have to store the values in some kind of store (e.g. mysql/ cloud ) – Me.Name Sep 09 '15 at 16:02
  • @Me.Name How could I connect the input values (whatever the users might enter into the prompt box) to a storage? ......Then have a code that'll sort through the storage for the most common input and display it on a site. – clsv Sep 09 '15 at 17:52

1 Answers1

0

Using pure Javascript, let's say hypothetically you prompt on the web page 3 times. With each prompt storing to an array.

var prompts = [];
(function(){
    for (var i=0; i<3; i++) {
        prompts.push(prompt("What is your favorite color?"))
    }
    //...
}());

Then you would want a method to loop through each response and determine the "matching" words used.

function collateWords () {
    // join all the colors
    var sWords = prompts.join(" ")
        .toLowerCase().trim().replace(/[,;.]/g,'')
        .split(/[\s\/]+/g).sort();
    var iWordsCount = sWords.length; // count w/ duplicates

    var counts = {}; // object for math
    for (var i=0; i<iWordsCount; i++) {
        var sWord = sWords[i];
        counts[sWord] = counts[sWord] || 0;
        counts[sWord]++;
    }

    var arr = []; // an array of objects to return
    for (sWord in counts) {
        arr.push({
            text: sWord,
            frequency: counts[sWord]
        });
    }

    // [sort array by descending frequency|http://stackoverflow.com/a/8837505]
    return arr.sort(function(a,b){
        return (a.frequency > b.frequency) ?
            -1 : ((a.frequency < b.frequency) ? 1 : 0);
    });
};

Call the new method and render to page.

//...
var collected = collateWords();
var iWordsCount = collected.length; // count w/o duplicates
for (var i=0; i<iWordsCount; i++) {
    var word = collected[i];
    document.write(word.frequency + ", " + word.text);
}
//...

Full example http://jsfiddle.net/jdgiotta/n26j5xf0/

John Giotta
  • 16,432
  • 7
  • 52
  • 82