From 4bc8630465cce0570642fa4906c015cbaffe51f2 Mon Sep 17 00:00:00 2001 From: sanele Date: Thu, 23 Oct 2014 11:19:42 +0200 Subject: [PATCH] done --- .gitignore | 1 + ExerciseOne/test-words-utility.html | 1 - ExerciseOne/words-utility-tests.js | 8 ++--- ExerciseOne/words-utility.js | 47 +++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c2658d7..a33a71f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +*~ diff --git a/ExerciseOne/test-words-utility.html b/ExerciseOne/test-words-utility.html index cbe60b4..433969b 100644 --- a/ExerciseOne/test-words-utility.html +++ b/ExerciseOne/test-words-utility.html @@ -4,7 +4,6 @@ Unit Tests for - the words utility - diff --git a/ExerciseOne/words-utility-tests.js b/ExerciseOne/words-utility-tests.js index 1f0c35a..0ab3186 100644 --- a/ExerciseOne/words-utility-tests.js +++ b/ExerciseOne/words-utility-tests.js @@ -7,8 +7,8 @@ QUnit.test( "test if words are counted correctly", function( assert ) { }); QUnit.test( "find the longest word", function( assert ) { - var wordsUtility = new WordsUtility("ola yeah yoyoyo yoda"); - assert.equal(wordsUtility.longestWord(), "yoyoyo"); + var wordsUtility = new WordsUtility(theWords); + assert.equal(wordsUtility.longestWord(), "dependencies"); }); QUnit.test( "the average word length of words supplied", function( assert ) { @@ -31,8 +31,8 @@ QUnit.test( "no words with the same length return nothing", function( assert ) { }); QUnit.test( "find the shortest word", function( assert ) { - var wordsUtility = new WordsUtility("ola yeah yoyoyo"); - assert.equal(wordsUtility.shortestWord(), "ola"); + var wordsUtility = new WordsUtility(theWords); + assert.equal(wordsUtility.shortestWord(), "A"); }); QUnit.jUnitReport = function(report) { diff --git a/ExerciseOne/words-utility.js b/ExerciseOne/words-utility.js index 661ba0f..ed54896 100644 --- a/ExerciseOne/words-utility.js +++ b/ExerciseOne/words-utility.js @@ -1,7 +1,48 @@ -WordsUtility = function(words){ +WordsUtility = function(theWords){ this.countWords = function(){ - return words.split(" ").length; + return theWords.split(" ").length; + + } + + + this.longestWord = function(){ + var list1 = theWords.split(" "); + var longestWord = " " + for (i=0; i < list1.length; i++){ + if (list1[i].length > longestWord.length){ + longestWord = list1[i]; + + } + } + return(longestWord); } -} \ No newline at end of file + + + this.averageWordLength = function(){ + + var list1 = theWords.split(" "); + + var list1 = thewords.length; + var totalCharacters = 0; + for(var i = 0; i < theWords; i++) + totalCharacters = totalCharacters + words[i].length(); + + return (theWords); + + } + this.shortestWord =function(){ + var list1 = theWords.split(" "); + var shortestWord = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" + for (i=0; i < list1.length; i++){ + if (list1[i].length < shortestWord.length){ + shortestWord = list1[i]; + + } + } + return(shortestWord); + + + } +}; \ No newline at end of file