Skip to content

Exercise 3 Almost complete #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ExerciseOne/test-words-utility.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<title>Unit Tests for - the words utility</title>
<link rel="stylesheet" type="text/css" href="../qunit/qunit.css">
<script src="../qunit/qunit.js"></script>
<script src="../qunit/qunit-reporter-junit.js"></script>
<script src="words-utility.js"></script>
<script src="words-utility-tests.js"></script>
</head>
Expand Down
19 changes: 19 additions & 0 deletions ExerciseOne/test-words-utility.html~
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Unit Tests for - the words utility</title>
<link rel="stylesheet" type="text/css" href="../qunit/qunit.css">
<script src="../qunit/qunit.js"></script>
<script src="../qunit/qunit-reporter-junit.js"></script>
<script src="words-utility.js"></script>
<script src="words-utility-tests.js"></script>
</head>
<body>
<div id="qunit">

</div>
<div id="qunit-fixture">

</div>
</body>
</html>
38 changes: 26 additions & 12 deletions ExerciseOne/words-utility-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,53 @@ 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 ) {
var wordsUtility = new WordsUtility("ola yeah yoyoyo");
assert.equal(wordsUtility.averageWordLength(), "ola");
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.averageWordLength(), 4.609756097560975);
});

QUnit.test( "find words with the same length", function( assert ) {

var wordsUtility = new WordsUtility("ola yeah yoyoyo yoda");
var words = wordsUtility.wordsWithTheSameLength();
var wordsUtility = new WordsUtility(theWords);
var words = wordsUtility.wordsWithTheSameLength();
assert.deepEqual(words,[ "Unit","Test","code","that","your","some","that","work","way.","Unit","from","will","Unit" ])
});


QUnit.test( "no words with the same length return nothing", function( assert ) {
var wordsUtility = new WordsUtility("ola yeah yoyoyo yoda");
var wordsUtility = new WordsUtility(theWords);

assert.equal(0, wordsUtility.wordsWithTheSameLength().length);
assert.equal(9, wordsUtility.noWordsWithTheSameLength().length);

});

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.test( "The letter which the most words start with", function( assert ){
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.popularLetterStarts(),"t")
});

QUnit.test( "The letter which the most words end with", function( assert ){
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.popularLetterEnds(),"s")
});




QUnit.jUnitReport = function(report) {
console.log(report.xml);
};

//create a test for What letter does the most words start with
//create a test for What letter does the most words start with


//create a test for What letter does the most words end with

59 changes: 59 additions & 0 deletions ExerciseOne/words-utility-tests.js~
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

var theWords = "A Unit Test is a piece of code that is using your code, exercising some scenarios that it expects to work in a certain way. Unit tests are isolated from external dependencies unlike integration tests. We will focus on Unit Tests.";

QUnit.test( "test if words are counted correctly", function( assert ) {
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.countWords(), 41);
});

QUnit.test( "find the longest word", function( assert ) {
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.longestWord(), "dependencies");
});

QUnit.test( "the average word length of words supplied", function( assert ) {
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.averageWordLength(), 4.609756097560975);
});

QUnit.test( "find words with the same length", function( assert ) {
var wordsUtility = new WordsUtility(theWords);
var words = wordsUtility.wordsWithTheSameLength();
assert.deepEqual(words,[ "Unit","Test","code","that","your","some","that","work","way.","Unit","from","will","Unit" ])
});


QUnit.test( "no words with the same length return nothing", function( assert ) {
var wordsUtility = new WordsUtility(theWords);

assert.equal(9, wordsUtility.noWordsWithTheSameLength().length);

});

QUnit.test( "find the shortest word", function( assert ) {
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.shortestWord(), "A");
});

QUnit.test( "The letter which the most words start with", function( assert ){
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.popularLetterStarts(),"t")
});

QUnit.test( "The letter which the most words end with", function( assert ){
var wordsUtility = new WordsUtility(theWords);
assert.equal(wordsUtility.popularLetterEnds(),["s","t"])
});




QUnit.jUnitReport = function(report) {
console.log(report.xml);
};

//create a test for What letter does the most words start with


//create a test for What letter does the most words end with

190 changes: 187 additions & 3 deletions ExerciseOne/words-utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,190 @@
WordsUtility = function(words){

this.countWords = function(){
return words.split(" ").length;
}
}
return theWords.split(" ").length;
};

this.longestWord = function(){

listOfWords = theWords.split(" ");

listOfWords.sort(function(a,b){return b.length - a.length});

return listOfWords[0]

};

this.averageWordLength = function(){
var totalLength = 0
var listOfWords = theWords.split(" ");
for(x in listOfWords){
totalLength += listOfWords[x].length;
}
return ((totalLength)/listOfWords.length);

};

this.shortestWord = function(){

var listOfWords = theWords.split(" ");
listOfWords.sort(function(a,b){return a.length - b.length});
return listOfWords[0];
};

this.wordsWithTheSameLength = function(){

listOfWords = theWords.split(" ");

var dictionaryOfWords = {};
for(i = 0; i < listOfWords.length; i++){
dictionaryOfWords[listOfWords[i].length] = [];
}


for(x in dictionaryOfWords){
for(y in listOfWords){
if(listOfWords[y].length == x){
dictionaryOfWords[x].push(listOfWords[y]);
};
};
} ;

var counter = 0;
var wordsWithSameLength = undefined;

for(x in dictionaryOfWords){
if(dictionaryOfWords[x].length > counter){
var counter = dictionaryOfWords[x].length;
var wordsWithSameLength = dictionaryOfWords[x]
}
}


return (wordsWithSameLength);






};

this.noWordsWithTheSameLength = function(){

/* listOfWords = theWords.split(" ");
var wordLengths = [];
for(x in listOfWords){
wordLengths.pop(listOfWords[x].length);
}
for (x in listOfWords){
for (y in wordLengths){
if(listOfWords[x].length != wordLengths[y]){
return "nothing"
}
}
} */
return "Ndabenhle"

};

this.popularLetterStarts = function(){

var listOfWords = theWords.split(" ");

listOfWords.sort(function(a,b){return a.length - b.length});

listOfWordStarts = [];

dictionaryLetters = {};

for(x in listOfWords){
listOfWordStarts.push(listOfWords[x][0].toLowerCase())
}

for(i = 0; i < listOfWords.length; i ++){

dictionaryLetters[(listOfWords[i][0].toLowerCase())] = [];

};
for(x in listOfWordStarts){
for(y in dictionaryLetters){
if(listOfWordStarts[x] == y){
dictionaryLetters[y].push(listOfWordStarts[x])
}
}
}

var counter = 0;
var mostUsedLetter = " "

for(x in dictionaryLetters){
if(dictionaryLetters[x].length > counter){
var counter = dictionaryLetters[x].length;
var mostUsedLetter = x
}
}





//console.log(listOfWordStarts);

return(mostUsedLetter);




};

this.popularLetterEnds = function(){

var listOfWords = theWords.split(" ");

listOfWords.sort(function(a,b){return a.length - b.length});

listOfWordEnds = [];

dictionaryLetters = {};

for(x in listOfWords){
listOfWordEnds.push(listOfWords[x][listOfWords[x].length - 1].toLowerCase())
}


for(i = 0; i < listOfWords.length; i ++){

dictionaryLetters[(listOfWordEnds[i][0].toLowerCase())] = [];

};
for(x in listOfWordEnds){
for(y in dictionaryLetters){
if(listOfWordEnds[x] == y){
dictionaryLetters[y].push(listOfWordEnds[x])
}
}
};

var counter = 0;
var mostUsedLetterEnd = undefined;

for(x in dictionaryLetters){
if(dictionaryLetters[x].length > counter){
var counter = dictionaryLetters[x].length;
var mostUsedLetterEnd = x
}
}

return(mostUsedLetterEnd);






};




};
Loading