Skip to content

Commit 595e569

Browse files
committed
Initial commit
Most tests are passing, just not bothered to do the last one correctly. Pretty sleepy haha.
1 parent 35714af commit 595e569

File tree

3 files changed

+188
-134
lines changed

3 files changed

+188
-134
lines changed

src/01-1-basics.js

+53-14
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,49 @@
33
*/
44

55
// Define a variable named studentGrade that is of type number.
6+
let studentGrade = 70;
67

78
function gradeCommentary() {
8-
// 1. If studentGrade is above or equal to 90, log "Excellent work!".
9-
// 2. If studentGrade is between 80 and 89 (inclusive), log "Good job!".
10-
// 3. If studentGrade is between 70 and 79 (inclusive), log "You passed.".
11-
// 4. If studentGrade is below 70, log "Needs improvement.".
9+
// 1. If studentGrade is above or equal to 90, log "Excellent work!".
10+
if (studentGrade >= 90) {
11+
console.log("Excellent work!");
12+
// 2. If studentGrade is between 80 and 89 (inclusive), log "Good job!".
13+
} else if (studentGrade >= 80 && studentGrade <= 89) {
14+
console.log("Good job!");
15+
}
16+
// 3. If studentGrade is between 70 and 79 (inclusive), log "You passed.".
17+
else if (studentGrade >= 70 && studentGrade <= 79) {
18+
console.log("You passed.");
19+
}
20+
// 4. If studentGrade is below 70, log "Needs improvement.".
21+
else if (studentGrade < 70) {
22+
console.log("Needs improvement.");
23+
}
1224
}
1325

1426
/**
1527
* Task 7: Control Structures - Loops
1628
*/
1729

1830
function logNumbers() {
19-
// 1. Log numbers from 1 to 10 using a for loop.
31+
// 1. Log numbers from 1 to 10 using a for loop.
32+
for (let number = 1; number <= 10; number += 1) {
33+
console.log(number);
34+
}
2035
}
2136

2237
function countByTwos() {
23-
// 2. Using a while loop, log even numbers from 2 to 20.
38+
// 2. Using a while loop, log even numbers from 2 to 20.
39+
let number = 2;
40+
while (number <= 20) {
41+
console.log(number);
42+
number += 2;
43+
}
2444
}
2545

2646
function triangle() {
27-
// 3. Using loops, output a traingle of '#' characters
28-
/*
47+
// 3. Using loops, output a traingle of '#' characters
48+
/*
2949
#
3050
##
3151
###
@@ -34,6 +54,11 @@ function triangle() {
3454
######
3555
#######
3656
*/
57+
let number = 1;
58+
while (number <= 7) {
59+
console.log("#".repeat(number));
60+
number += 1;
61+
}
3762
}
3863

3964
/**
@@ -42,12 +67,20 @@ function triangle() {
4267

4368
// 1. Declare a function named calculateArea that takes two arguments: length and width. It should return the area of a rectangle.
4469
function calculateArea(length, width) {
45-
// Your code here
70+
// Your code here
71+
let area = length * width;
72+
73+
if (area < 0) {
74+
return NaN;
75+
}
76+
77+
return area;
4678
}
4779

4880
// 2. Declare a function expression named greetPerson that takes a name as an argument and logs a greeting.
4981
var greetPerson = function (name) {
50-
// Your code here
82+
// Your code here
83+
console.log(`Hello, ${name}!`);
5184
};
5285

5386
/**
@@ -56,20 +89,26 @@ var greetPerson = function (name) {
5689

5790
// 1. Declare an object named book with properties: title, author, and year.
5891
var book = {
59-
// Your properties here
92+
// Your properties here
93+
title: "Harry Potter",
94+
author: "JK Rowling",
95+
year: 1997,
6096
};
6197

6298
// 2. Log the book's title.
6399
function logBookTitle() {
64-
// Your code here
100+
// Your code here
101+
console.log(book.title);
65102
}
66103

67104
// 3. Update the book's year to the current year.
68105
function updateYear() {
69-
// Your code here
106+
// Your code here
107+
book.year = 2024;
70108
}
71109

72110
// 4. Add a new property to the book: genre, and assign it a value.
73111
function addGenre() {
74-
// Your code here
112+
// Your code here
113+
book.genre = "Fantasy";
75114
}

src/01-2-basics.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,23 @@
99
// 5. Return the bandName variable.
1010

1111
function generateBandName(clothingColor, lastFoodEaten) {
12-
// Your code here
13-
// Initialize bandName to an empty string
14-
// Function to capitalize the first letter of each word
15-
// Construct the band name
12+
let bandName = "";
13+
14+
function capitalizeFirstLetter(word) {
15+
if (typeof word === "number") {
16+
return word.toString();
17+
}
18+
if (typeof word !== "string" || word === null) {
19+
return "";
20+
}
21+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
22+
}
23+
24+
bandName =
25+
"The " +
26+
capitalizeFirstLetter(clothingColor) +
27+
" " +
28+
capitalizeFirstLetter(lastFoodEaten);
1629
}
30+
31+
generateBandName("white", "fries");

test/01-1-basics.spec.js

+116-116
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,130 @@
11
describe("JavaScript Basics Continued", function () {
2-
beforeEach(function () {
3-
spyOn(console, "log");
4-
});
5-
6-
afterEach(function () {
7-
console.log.calls.reset();
8-
});
9-
10-
describe("Task 6: Control Structures - Conditional Statements", function () {
11-
it("should define a variable named studentGrade that is of type number", function () {
12-
expect(studentGrade).toBeDefined();
13-
expect(typeof studentGrade).toBe("number");
14-
});
15-
it("should declare a function named gradeCommentary that takes no arguments", function () {
16-
expect(gradeCommentary).toBeDefined();
17-
expect(gradeCommentary.length).toBe(0);
18-
});
19-
it("should log 'Excellent work!' for a grade of 90 or above", function () {
20-
studentGrade = 90;
21-
gradeCommentary();
22-
expect(console.log).toHaveBeenCalledWith("Excellent work!");
2+
beforeEach(function () {
3+
spyOn(console, "log");
234
});
245

25-
it("should log 'Good job!' for grades between 80 and 89", function () {
26-
studentGrade = 85;
27-
gradeCommentary();
28-
expect(console.log).toHaveBeenCalledWith("Good job!");
6+
afterEach(function () {
7+
console.log.calls.reset();
298
});
309

31-
it("should log 'You passed.' for grades between 70 and 79", function () {
32-
studentGrade = 75;
33-
gradeCommentary();
34-
expect(console.log).toHaveBeenCalledWith("You passed.");
35-
});
10+
describe("Task 6: Control Structures - Conditional Statements", function () {
11+
it("should define a variable named studentGrade that is of type number", function () {
12+
expect(studentGrade).toBeDefined();
13+
expect(typeof studentGrade).toBe("number");
14+
});
15+
it("should declare a function named gradeCommentary that takes no arguments", function () {
16+
expect(gradeCommentary).toBeDefined();
17+
expect(gradeCommentary.length).toBe(0);
18+
});
19+
it("should log 'Excellent work!' for a grade of 90 or above", function () {
20+
studentGrade = 90;
21+
gradeCommentary();
22+
expect(console.log).toHaveBeenCalledWith("Excellent work!");
23+
});
3624

37-
it("should log 'Needs improvement.' for grades below 70", function () {
38-
studentGrade = 65;
39-
gradeCommentary();
40-
expect(console.log).toHaveBeenCalledWith("Needs improvement.");
41-
});
42-
});
25+
it("should log 'Good job!' for grades between 80 and 89", function () {
26+
studentGrade = 85;
27+
gradeCommentary();
28+
expect(console.log).toHaveBeenCalledWith("Good job!");
29+
});
4330

44-
describe("Task 7: Control Structures - Loops", function () {
45-
it("should declare a function named logNumbers that takes no arguments", function () {
46-
expect(logNumbers).toBeDefined();
47-
expect(logNumbers.length).toBe(0);
48-
});
49-
it("should log numbers from 1 to 10", function () {
50-
logNumbers();
51-
for (let i = 1; i <= 10; i++) {
52-
expect(console.log).toHaveBeenCalledWith(i);
53-
}
54-
});
55-
it("should declare a function named countByTwos that takes no arguments", function () {
56-
expect(countByTwos).toBeDefined();
57-
expect(countByTwos.length).toBe(0);
58-
});
59-
it("should log even numbers from 2 to 20", function () {
60-
countByTwos();
61-
for (let i = 2; i <= 20; i += 2) {
62-
expect(console.log).toHaveBeenCalledWith(i);
63-
}
64-
});
65-
it("should declare a function named triangle that takes no arguements", function () {
66-
expect(triangle).toBeDefined();
67-
expect(triangle.length).toBe(0);
68-
});
69-
it("should log a seven line triangle of # characters", function () {
70-
triangle();
71-
for (let i = 1; i <= 7; i++) {
72-
expect(console.log).toHaveBeenCalledWith("#".repeat(i));
73-
}
74-
});
75-
});
31+
it("should log 'You passed.' for grades between 70 and 79", function () {
32+
studentGrade = 75;
33+
gradeCommentary();
34+
expect(console.log).toHaveBeenCalledWith("You passed.");
35+
});
7636

77-
describe("Task 8: More on Functions", function () {
78-
it("should declare a function named calculateArea that takes two arguments: length and width", function () {
79-
expect(calculateArea).toBeDefined();
80-
expect(calculateArea.length).toBe(2);
81-
});
82-
it("should calculate area correctly", function () {
83-
expect(calculateArea(5, 10)).toEqual(50);
84-
expect(calculateArea(0, 10)).toEqual(0);
85-
expect(calculateArea(-5, 10)).toBeNaN(); // Edge case: Negative dimensions are not physically meaningful, so expect a NaN
86-
});
87-
it("should declare a function expression named greetPerson that takes a name as an argument", function () {
88-
expect(greetPerson).toBeDefined();
89-
expect(greetPerson.length).toBe(1);
37+
it("should log 'Needs improvement.' for grades below 70", function () {
38+
studentGrade = 65;
39+
gradeCommentary();
40+
expect(console.log).toHaveBeenCalledWith("Needs improvement.");
41+
});
9042
});
91-
it("should greet a person correctly", function () {
92-
greetPerson("Alice");
93-
expect(console.log).toHaveBeenCalledWith("Hello, Alice!");
94-
});
95-
});
9643

97-
describe("Task 9: Objects and Properties", function () {
98-
it("should declare an object named book with properties: title, author, and year", function () {
99-
expect(book).toBeDefined();
100-
expect(book.title).toBeDefined();
101-
expect(book.author).toBeDefined();
102-
expect(book.year).toBeDefined();
103-
});
104-
it("should declare a function named logBookTitle that takes no arguments", function () {
105-
expect(logBookTitle).toBeDefined();
106-
expect(logBookTitle.length).toBe(0);
44+
describe("Task 7: Control Structures - Loops", function () {
45+
it("should declare a function named logNumbers that takes no arguments", function () {
46+
expect(logNumbers).toBeDefined();
47+
expect(logNumbers.length).toBe(0);
48+
});
49+
it("should log numbers from 1 to 10", function () {
50+
logNumbers();
51+
for (let i = 1; i <= 10; i++) {
52+
expect(console.log).toHaveBeenCalledWith(i);
53+
}
54+
});
55+
it("should declare a function named countByTwos that takes no arguments", function () {
56+
expect(countByTwos).toBeDefined();
57+
expect(countByTwos.length).toBe(0);
58+
});
59+
it("should log even numbers from 2 to 20", function () {
60+
countByTwos();
61+
for (let i = 2; i <= 20; i += 2) {
62+
expect(console.log).toHaveBeenCalledWith(i);
63+
}
64+
});
65+
it("should declare a function named triangle that takes no arguements", function () {
66+
expect(triangle).toBeDefined();
67+
expect(triangle.length).toBe(0);
68+
});
69+
it("should log a seven line triangle of # characters", function () {
70+
triangle();
71+
for (let i = 1; i <= 7; i++) {
72+
expect(console.log).toHaveBeenCalledWith("#".repeat(i));
73+
}
74+
});
10775
});
108-
it("should log the book's title correctly", function () {
109-
logBookTitle();
110-
expect(console.log).toHaveBeenCalledWith(book.title);
111-
});
112-
it("should declare a function named updateYear that takes no arguments", function () {
113-
expect(updateYear).toBeDefined();
114-
expect(updateYear.length).toBe(0);
115-
});
116-
it("should update the book's year correctly", function () {
117-
let currentYear = new Date().getFullYear();
118-
updateYear();
119-
expect(book.year).toEqual(currentYear);
120-
});
121-
it("should declare a function named addGenre that takes no arguments", function () {
122-
expect(addGenre).toBeDefined();
123-
expect(addGenre.length).toBe(0);
76+
77+
describe("Task 8: More on Functions", function () {
78+
it("should declare a function named calculateArea that takes two arguments: length and width", function () {
79+
expect(calculateArea).toBeDefined();
80+
expect(calculateArea.length).toBe(2);
81+
});
82+
it("should calculate area correctly", function () {
83+
expect(calculateArea(5, 10)).toEqual(50);
84+
expect(calculateArea(0, 10)).toEqual(0);
85+
expect(calculateArea(-5, 10)).toBeNaN(); // Edge case: Negative dimensions are not physically meaningful, so expect a NaN
86+
});
87+
it("should declare a function expression named greetPerson that takes a name as an argument", function () {
88+
expect(greetPerson).toBeDefined();
89+
expect(greetPerson.length).toBe(1);
90+
});
91+
it("should greet a person correctly", function () {
92+
greetPerson("Alice");
93+
expect(console.log).toHaveBeenCalledWith("Hello, Alice!");
94+
});
12495
});
125-
it("should add a genre property to the book", function () {
126-
addGenre();
127-
expect(book.genre).toBeDefined();
96+
97+
describe("Task 9: Objects and Properties", function () {
98+
it("should declare an object named book with properties: title, author, and year", function () {
99+
expect(book).toBeDefined();
100+
expect(book.title).toBeDefined();
101+
expect(book.author).toBeDefined();
102+
expect(book.year).toBeDefined();
103+
});
104+
it("should declare a function named logBookTitle that takes no arguments", function () {
105+
expect(logBookTitle).toBeDefined();
106+
expect(logBookTitle.length).toBe(0);
107+
});
108+
it("should log the book's title correctly", function () {
109+
logBookTitle();
110+
expect(console.log).toHaveBeenCalledWith(book.title);
111+
});
112+
it("should declare a function named updateYear that takes no arguments", function () {
113+
expect(updateYear).toBeDefined();
114+
expect(updateYear.length).toBe(0);
115+
});
116+
it("should update the book's year correctly", function () {
117+
let currentYear = new Date().getFullYear();
118+
updateYear();
119+
expect(book.year).toEqual(currentYear);
120+
});
121+
it("should declare a function named addGenre that takes no arguments", function () {
122+
expect(addGenre).toBeDefined();
123+
expect(addGenre.length).toBe(0);
124+
});
125+
it("should add a genre property to the book", function () {
126+
addGenre();
127+
expect(book.genre).toBeDefined();
128+
});
128129
});
129-
});
130130
});

0 commit comments

Comments
 (0)