Programming for Complete Beginners Code Examples

var text = 
    'And this is Dorlcote Mill. I must '
  + 'stand a minute or two here on the bridge '
  + 'and look at it, though the clouds are '
  + 'threatening, and it is far on in the afternoon. '
  + 'Even in this leafless time of departing February '
  + 'it is pleasant to look at,–perhaps the chill, '
  + 'damp season adds a charm to the trimly kept, '
  + 'comfortable dwelling-house, as old as the elms '
  + 'and chestnuts that shelter it from the '
  + 'northern blast. ';
  
var text_analyzer = {
    current_text : text,
    get_words_array : function() {
        var text = this.current_text;
        var split_text = text.split(' ');
        return split_text;
    },
    count_words : function() {
        var words_array = this.get_words_array();
        var length = words_array.length;
        return length;
    },
    get_average_word_length : function() {
      var all_word_lengths = 0;
      var words_array = this.get_words_array();
      for(var i in words_array) {
          var current_word = words_array[i];
          all_word_lengths = all_word_lengths +
              current_word.length;
      }
      return all_word_lengths / words_array.length;
    },
    get_longest_word : function() {
        var longest_length_seen_so_far = 0;
        var longest_word = '';
        var words_array = this.get_words_array();
        for(var i in words_array) {
            var current_word = words_array[i];
            if(current_word.length > 
                longest_length_seen_so_far) {
                longest_word = current_word;
                longest_length_seen_so_far =
                    current_word.length;
            }
        }
        return longest_word;
    },
    get_word_frequencies : function() {
        var words_array = this.get_words_array();
        var word_frequencies = {};
        for(var i in words_array) {
            var current_word = words_array[i];
            if(! (current_word in word_frequencies)) {
                word_frequencies[current_word] = 1;
            }
            else {
                var previous_frequency = 
                    word_frequencies[current_word];
                var new_frequency = previous_frequency
                    + 1;
                word_frequencies[current_word] =
                    new_frequency;
            }
        }
        return word_frequencies;
    },
};

function print_object(the_object) {
    document.write('{
'); for(var i in the_object) { var key = i; var value = the_object[i]; document.write('"' + key + '"'); document.write(' : '); if(Array.isArray(value)) { print_array(value); } else if(typeof value === 'object') { print_object(value); } else { document.write(value); } document.write('
'); } document.write('}
'); } function print_array(the_array) { document.write('[
'); for(var i in the_array) { var value = the_array[i]; if(Array.isArray(value)) { print_array(value); } else if(typeof value === 'object') { print_object(value); } else { document.write(value + ','); } } document.write(']
'); } document.write(text_analyzer.get_word_frequencies()['on']);

Chapter 12 “Program” starting code:


 
 
 
 
 
 

 

 

Commenting rules: Politeness is the only rule. We respect your right to disagree with anything we say. But comments with profanity and insults will be deleted.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments