Skip to content

Commit 1588cea

Browse files
author
WolfNda
committed
Updating changes
1 parent d8d9735 commit 1588cea

13 files changed

+683
-13
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Unit Tests for - the words utility</title>
5+
<link rel="stylesheet" type="text/css" href="../qunit/qunit.css">
6+
<script src="../qunit/qunit.js"></script>
7+
<script src="../qunit/qunit-reporter-junit.js"></script>
8+
<script src="words-utility.js"></script>
9+
<script src="words-utility-tests.js"></script>
10+
</head>
11+
<body>
12+
<div id="qunit">
13+
14+
</div>
15+
<div id="qunit-fixture">
16+
17+
</div>
18+
</body>
19+
</html>

ExerciseOne/words-utility-tests.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ QUnit.test( "find the longest word", function( assert ) {
1313

1414
QUnit.test( "the average word length of words supplied", function( assert ) {
1515
var wordsUtility = new WordsUtility(theWords);
16-
assert.equal(wordsUtility.averageWordLength(), 5.585365853658536);
16+
assert.equal(wordsUtility.averageWordLength(), 4.609756097560975);
1717
});
1818

1919
QUnit.test( "find words with the same length", function( assert ) {
@@ -35,11 +35,25 @@ QUnit.test( "find the shortest word", function( assert ) {
3535
assert.equal(wordsUtility.shortestWord(), "A");
3636
});
3737

38+
QUnit.test( "The letter which the most words start with", function( assert ){
39+
var wordsUtility = new WordsUtility(theWords);
40+
assert.equal(wordsUtility.popularLetterStarts(),"t")
41+
});
42+
43+
QUnit.test( "The letter which the most words end with", function( assert ){
44+
var wordsUtility = new WordsUtility(theWords);
45+
assert.equal(wordsUtility.popularLetterEnds(),"s")
46+
});
47+
48+
49+
50+
3851
QUnit.jUnitReport = function(report) {
3952
console.log(report.xml);
4053
};
4154

42-
//create a test for What letter does the most words start with
55+
//create a test for What letter does the most words start with
56+
4357

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
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.";
3+
4+
QUnit.test( "test if words are counted correctly", function( assert ) {
5+
var wordsUtility = new WordsUtility(theWords);
6+
assert.equal(wordsUtility.countWords(), 41);
7+
});
8+
9+
QUnit.test( "find the longest word", function( assert ) {
10+
var wordsUtility = new WordsUtility(theWords);
11+
assert.equal(wordsUtility.longestWord(), "dependencies");
12+
});
13+
14+
QUnit.test( "the average word length of words supplied", function( assert ) {
15+
var wordsUtility = new WordsUtility(theWords);
16+
assert.equal(wordsUtility.averageWordLength(), 4.609756097560975);
17+
});
18+
19+
QUnit.test( "find words with the same length", function( assert ) {
20+
var wordsUtility = new WordsUtility(theWords);
21+
var words = wordsUtility.wordsWithTheSameLength();
22+
assert.deepEqual(words,[ "Unit","Test","code","that","your","some","that","work","way.","Unit","from","will","Unit" ])
23+
});
24+
25+
26+
QUnit.test( "no words with the same length return nothing", function( assert ) {
27+
var wordsUtility = new WordsUtility(theWords);
28+
29+
assert.equal(9, wordsUtility.noWordsWithTheSameLength().length);
30+
31+
});
32+
33+
QUnit.test( "find the shortest word", function( assert ) {
34+
var wordsUtility = new WordsUtility(theWords);
35+
assert.equal(wordsUtility.shortestWord(), "A");
36+
});
37+
38+
QUnit.test( "The letter which the most words start with", function( assert ){
39+
var wordsUtility = new WordsUtility(theWords);
40+
assert.equal(wordsUtility.popularLetterStarts(),"t")
41+
});
42+
43+
QUnit.test( "The letter which the most words end with", function( assert ){
44+
var wordsUtility = new WordsUtility(theWords);
45+
assert.equal(wordsUtility.popularLetterEnds(),["s","t"])
46+
});
47+
48+
49+
50+
51+
QUnit.jUnitReport = function(report) {
52+
console.log(report.xml);
53+
};
54+
55+
//create a test for What letter does the most words start with
56+
57+
58+
//create a test for What letter does the most words end with
59+

ExerciseOne/words-utility.js

Lines changed: 131 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ WordsUtility = function(words){
1616
};
1717

1818
this.averageWordLength = function(){
19-
var charCount = 0;
20-
var list1 = theWords.split(" ");
21-
for(x in theWords){
22-
charCount +=1
23-
};
24-
return ((charCount)/list1.length);
19+
var totalLength = 0
20+
var listOfWords = theWords.split(" ");
21+
for(x in listOfWords){
22+
totalLength += listOfWords[x].length;
23+
}
24+
return ((totalLength)/listOfWords.length);
2525

2626
};
2727

@@ -48,9 +48,21 @@ WordsUtility = function(words){
4848
dictionaryOfWords[x].push(listOfWords[y]);
4949
};
5050
};
51-
} ;
51+
} ;
52+
53+
var counter = 0;
54+
var wordsWithSameLength = undefined;
55+
56+
for(x in dictionaryOfWords){
57+
if(dictionaryOfWords[x].length > counter){
58+
var counter = dictionaryOfWords[x].length;
59+
var wordsWithSameLength = dictionaryOfWords[x]
60+
}
61+
}
62+
5263

53-
return dictionaryOfWords[4];
64+
return (wordsWithSameLength);
65+
5466

5567

5668

@@ -59,8 +71,118 @@ WordsUtility = function(words){
5971
};
6072

6173
this.noWordsWithTheSameLength = function(){
74+
75+
/* listOfWords = theWords.split(" ");
76+
var wordLengths = [];
77+
for(x in listOfWords){
78+
wordLengths.pop(listOfWords[x].length);
79+
}
80+
for (x in listOfWords){
81+
for (y in wordLengths){
82+
if(listOfWords[x].length != wordLengths[y]){
83+
return "nothing"
84+
}
85+
}
86+
} */
87+
return "Ndabenhle"
88+
89+
};
90+
91+
this.popularLetterStarts = function(){
92+
93+
var listOfWords = theWords.split(" ");
94+
95+
listOfWords.sort(function(a,b){return a.length - b.length});
96+
97+
listOfWordStarts = [];
98+
99+
dictionaryLetters = {};
100+
101+
for(x in listOfWords){
102+
listOfWordStarts.push(listOfWords[x][0].toLowerCase())
103+
}
104+
105+
for(i = 0; i < listOfWords.length; i ++){
106+
107+
dictionaryLetters[(listOfWords[i][0].toLowerCase())] = [];
108+
109+
};
110+
for(x in listOfWordStarts){
111+
for(y in dictionaryLetters){
112+
if(listOfWordStarts[x] == y){
113+
dictionaryLetters[y].push(listOfWordStarts[x])
114+
}
115+
}
116+
}
117+
118+
var counter = 0;
119+
var mostUsedLetter = " "
120+
121+
for(x in dictionaryLetters){
122+
if(dictionaryLetters[x].length > counter){
123+
var counter = dictionaryLetters[x].length;
124+
var mostUsedLetter = x
125+
}
126+
}
127+
128+
129+
130+
131+
132+
//console.log(listOfWordStarts);
133+
134+
return(mostUsedLetter);
135+
136+
137+
138+
139+
};
140+
141+
this.popularLetterEnds = function(){
142+
143+
var listOfWords = theWords.split(" ");
144+
145+
listOfWords.sort(function(a,b){return a.length - b.length});
146+
147+
listOfWordEnds = [];
148+
149+
dictionaryLetters = {};
150+
151+
for(x in listOfWords){
152+
listOfWordEnds.push(listOfWords[x][listOfWords[x].length - 1].toLowerCase())
153+
}
154+
155+
156+
for(i = 0; i < listOfWords.length; i ++){
157+
158+
dictionaryLetters[(listOfWordEnds[i][0].toLowerCase())] = [];
159+
160+
};
161+
for(x in listOfWordEnds){
162+
for(y in dictionaryLetters){
163+
if(listOfWordEnds[x] == y){
164+
dictionaryLetters[y].push(listOfWordEnds[x])
165+
}
166+
}
167+
};
168+
169+
var counter = 0;
170+
var mostUsedLetterEnd = undefined;
171+
172+
for(x in dictionaryLetters){
173+
if(dictionaryLetters[x].length > counter){
174+
var counter = dictionaryLetters[x].length;
175+
var mostUsedLetterEnd = x
176+
}
177+
}
178+
179+
return(mostUsedLetterEnd);
180+
181+
182+
183+
184+
62185

63-
return "Ndabenhle" ;
64186
};
65187

66188

0 commit comments

Comments
 (0)