Skip to content

Commit 159369d

Browse files
committed
completed main portions of unit testing exercises
1 parent db1f866 commit 159369d

File tree

12 files changed

+214
-8
lines changed

12 files changed

+214
-8
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.DS_Store
1+
**/node_modules
2+
**/package-lock.json
3+
**/.DS_Store
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
let dash = "-".repeat(75);
2+
function makeLine(size) {
3+
let line = '';
4+
for (let i = 0; i < size; i++) {
5+
line += '#';
6+
}
7+
return line;
8+
}
9+
console.log(makeLine(5));
10+
console.log(dash);
11+
12+
// function makeSquare(size) {
13+
// let square = '';
14+
// for (let i = 0; i < size; i++) {
15+
// square += makeLine(size) + '\n';
16+
// }
17+
// return square.slice(0, -1);
18+
// }
19+
// console.log(makeSquare(5));
20+
// console.log(dash);
21+
22+
23+
24+
function makeRectangle(width, height) {
25+
let rectangle = '';
26+
for (let i = 0; i < height; i++) {
27+
rectangle += (makeLine(width)) + "\n";
28+
}
29+
return rectangle.slice(0, -1);
30+
}
31+
32+
console.log(makeRectangle(5,3))
33+
console.log(dash);

functions/try-it/isPalindrome.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ function reverse(str) {
55
function isPalindrome(str) {
66
return reverse(str) === str;
77
}
8+
9+
reverse('bob');
10+
isPalindrome('racecar');

modules/exporting-modules/practiceExports.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,4 @@ module.exports = {
1919
isPalindrome: isPalindrome,
2020
evenOrOdd: evenOrOdd,
2121
randomArrayElement: randomArrayElement
22-
}
23-
24-
// module.exports = isPalindrome;
25-
// module.exports = evenOrOdd;
26-
// module.exports = randomArrayElement;
22+
}

more-on-functions/chapter-examples/receieve-function-arguments/getValidInput.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ function getValidInput(prompt, isValid) {
1111

1212
return userInput;
1313
}
14+
// let isValid = function(userInput) {
15+
// console.log("Invalid input. Try again. ");
16+
// userInput = input.question[0] === 'a';
17+
// }
18+
let isValid = function(userInput) {
19+
if (userInput.question[0] === 'a' || 'A') {
20+
return true;
21+
}
22+
return false;
23+
};
1424

25+
console.log(getValidInput('Select a vowel.'))
1526
// TODO 1: write a validator
1627
// that ensures input starts with "a"
1728

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
//1) Create a function with an array of numbers as its parameter. The function should iterate through the array and return the minimum value from the array. Hint: Use what you know about if statements to identify and store the smallest value within the array.
22

3+
4+
35
//Sample arrays for testing:
46
let nums1 = [5, 10, 2, 42];
57
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
68
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
79

10+
function minimumNum(unsortedArray) {
11+
let sortedArr = [];
12+
let lowestValue = 0;
13+
for (let i = 0; i < unsortedArray.length - 1; i++) {
14+
//console.log(unsortedArray[i]);
15+
if (unsortedArray[i] < unsortedArray[i + 1]) {
16+
lowestValue = unsortedArray[i];
17+
18+
}
19+
sortedArr.push(lowestValue);
20+
unsortedArray.splice(lowestValue);
21+
}
22+
return sortedArr;
23+
}
24+
25+
console.log(minimumNum(nums1));
26+
27+
28+
29+
30+
31+
832
//Using one of the test arrays as the argument, call your function inside the console.log statement below.
933

1034
console.log(/* your code here */);

objects-and-math/exercises/ObjectExercises.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,53 @@ let salamander = {
1212
age: 5
1313
};
1414

15+
let superChimpTwo = {
16+
name: "Brad",
17+
species: "Chimpanzee",
18+
mass: 11,
19+
age: 6
20+
};
21+
22+
let beagle = {
23+
name: "LeRoy",
24+
species: "Beagle",
25+
mass: 14,
26+
age: 5
27+
};
28+
29+
let tardigrade = {
30+
name: "Almina",
31+
species: "tardigrade",
32+
mass: 0.0000000001,
33+
age: 1
34+
};
1535

1636
// After you have created the other object literals, add the astronautID property to each one.
37+
superChimpOne["astronautID"] = 1;
38+
salamander["astronautID"] = 2;
39+
superChimpTwo["astronautID"] = 3;
40+
beagle["astronautID"] = 4;
41+
tardigrade["astronautID"] = 5;
1742

1843
// Add a move method to each animal object
44+
superChimpOne.move = function() {return Math.floor(Math.random()*11)};
45+
salamander.move = function() {return Math.floor(Math.random()*11)};
46+
superChimpTwo.move = function() {return Math.floor(Math.random()*11)};
47+
beagle.move = function() {return Math.floor(Math.random()*11)};
48+
tardigrade.move = function() {return Math.floor(Math.random()*11)};
1949

2050
// Create an array to hold the animal objects.
51+
let crew = [superChimpOne, salamander, superChimpTwo, beagle, tardigrade];
2152

2253
// Print out the relevant information about each animal.
54+
console.log(superChimpOne);
55+
console.log(salamander);
56+
console.log(superChimpTwo);
57+
console.log(beagle);
58+
console.log(tardigrade);
2359

2460
// Start an animal race!
61+
function crewReports(object){
62+
return (`${this.name}is a ${this.species}. They are ${this.age} years old and ${this.mass} kilograms. Their ID is ${this.astronautID}.`);
63+
};
64+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const isPalindrome = require('../palindrome.js');
2+
3+
describe("testing isPalindrome", function () {
4+
5+
test("should return true for a single letter", function () {
6+
expect(isPalindrome("a")).toBe(true);
7+
});
8+
9+
test("should return true for a single letter repeated", function () {
10+
expect(isPalindrome("aaa")).toBe(true);
11+
});
12+
13+
test("should return true for a simple palindrome", function () {
14+
expect(isPalindrome("aba")).toBe(true);
15+
});
16+
17+
test("should return true for a longer palindrome", function () {
18+
expect(isPalindrome("racecar")).toBe(true);
19+
});
20+
21+
test("should return false for a longer non-palindrome", function () {
22+
expect(isPalindrome("launchcode")).toBe(false);
23+
});
24+
25+
test("should return false for a simple non-palindrome", function () {
26+
expect(isPalindrome("ab")).toBe(false);
27+
});
28+
29+
test("should be case-sensitive", function () {
30+
expect(isPalindrome("abA")).toBe(false);
31+
});
32+
33+
test("should consider whitespace", function () {
34+
expect(isPalindrome("so many dynamos")).toBe(false);
35+
});
36+
37+
test("should consider the empty string a palindrome", function() {
38+
expect(isPalindrome("")).toBe(true);
39+
});
40+
});

unit-testing/exercises/RPS.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ function whoWon(player1,player2){
1717
}
1818

1919
return 'Player 1 wins!';
20-
}
20+
}
21+
22+
module.exports = {
23+
whoWon: whoWon
24+
};

unit-testing/exercises/checkFive.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ function checkFive(num){
88
result = num + " is greater than 5.";
99
}
1010
return result;
11-
}
11+
}
12+
13+
module.exports = checkFive;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const rpsTest = require('../RPS.js');
2+
3+
describe("whoWon", function(){
4+
5+
test("should respond that there is a tie when both players choose the same option", function(){
6+
let output = rpsTest.whoWon('paper', 'paper');
7+
expect(output).toBe('TIE!');
8+
});
9+
10+
test("should respond that player 2 wins when playing paper to player 1's rock", function(){
11+
let output = rpsTest.whoWon('rock', 'paper');
12+
expect(output).toBe('Player 2 wins!');
13+
});
14+
15+
test("should respond that player 2 wins when playing scissor to player 1's paper", function(){
16+
let output = rpsTest.whoWon('paper', 'scissors');
17+
expect(output).toBe('Player 2 wins!');
18+
});
19+
20+
test("should respond that player 1 wins when playing paper to player 2's rock", function(){
21+
let output = rpsTest.whoWon('paper', 'rock');
22+
});
23+
24+
test("should respond that player 1 wins when playing scissors to player 2's paper", function(){
25+
let output = rpsTest.whoWon('scissors', 'paper');
26+
});
27+
28+
test("should respond that player 1 winds when playing rock to player 2's scissors", function(){
29+
let output = rpsTest.whoWon('rock', 'scissors');
30+
});
31+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const checkFive = require('../checkFive.js');
2+
3+
describe("checkFive", function(){
4+
5+
test("should produce response that number is less than 5 when number is less than 5", function() {
6+
let output = checkFive(2);
7+
expect(output).toBe("2 is less than 5.");
8+
});
9+
10+
test("should produce response that number is greater than 5 when number is greater than 5", function() {
11+
let output = checkFive(7);
12+
expect(output).toBe("7 is greater than 5.");
13+
});
14+
15+
test("should produce response that number is equal to 5 when number is equal to 5.", function() {
16+
let output = checkFive(5);
17+
expect(output).toBe("5 is equal to 5.");
18+
});
19+
20+
});

0 commit comments

Comments
 (0)