Skip to content

Commit 418424b

Browse files
authored
Merge pull request #1 from UD-S25-CISC275-Tasks/solved-functions
Solved functions
2 parents 7e4a033 + 5722d6a commit 418424b

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

public/tasks/task-functions.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Task - Functions
2+
3+
Version: 0.0.1
4+
5+
Implement a bunch of functions that work on primitives.

src/functions.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {
2+
add3,
3+
fahrenheitToCelius,
4+
shout,
5+
isQuestion,
6+
convertYesNo,
7+
} from "./functions";
8+
9+
describe("Testing the basic functions", () => {
10+
test("(3 pts) Testing the fahrenheitToCelius function", () => {
11+
expect(fahrenheitToCelius(32)).toBe(0);
12+
expect(fahrenheitToCelius(-40)).toBe(-40);
13+
expect(fahrenheitToCelius(-22)).toBe(-30);
14+
expect(fahrenheitToCelius(14)).toBe(-10);
15+
expect(fahrenheitToCelius(68)).toBe(20);
16+
expect(fahrenheitToCelius(86)).toBe(30);
17+
expect(fahrenheitToCelius(212)).toBe(100);
18+
});
19+
20+
test("(3 pts) Testing the add3 function", () => {
21+
expect(add3(1, 2, 3)).toBe(6);
22+
expect(add3(9, 7, 4)).toBe(20);
23+
expect(add3(6, -3, 9)).toBe(15);
24+
expect(add3(10, 1, -9)).toBe(11);
25+
expect(add3(-9, -100, -4)).toBe(0);
26+
expect(add3(-1, -1, 1)).toBe(1);
27+
});
28+
29+
test("(3 pts) Testing the shout function", () => {
30+
expect(shout("Hello")).toBe("HELLO!");
31+
expect(shout("What?")).toBe("WHAT?!");
32+
expect(shout("oHo")).toBe("OHO!");
33+
expect(shout("AHHHH!!!")).toBe("AHHHH!!!!");
34+
expect(shout("")).toBe("!");
35+
expect(shout("Please go outside")).toBe("PLEASE GO OUTSIDE!");
36+
});
37+
38+
test("(3 pts) Testing the isQuestion function", () => {
39+
expect(isQuestion("Is this a question?")).toBe(true);
40+
expect(isQuestion("Who are you?")).toBe(true);
41+
expect(isQuestion("WHAT ARE YOU !?")).toBe(true);
42+
expect(isQuestion("WHAT IS THIS?!")).toBe(false);
43+
expect(isQuestion("OH GOD!")).toBe(false);
44+
expect(isQuestion("Oh nevermind, it's fine.")).toBe(false);
45+
expect(isQuestion("")).toBe(false);
46+
});
47+
48+
test("(3 pts) Testing the convertYesNo function", () => {
49+
expect(convertYesNo("yes")).toBe(true);
50+
expect(convertYesNo("YES")).toBe(true);
51+
expect(convertYesNo("NO")).toBe(false);
52+
expect(convertYesNo("no")).toBe(false);
53+
expect(convertYesNo("Apple")).toBe(null);
54+
expect(convertYesNo("Bananas")).toBe(null);
55+
expect(convertYesNo("Nope")).toBe(null);
56+
expect(convertYesNo("Yesterday")).toBe(null);
57+
expect(convertYesNo("Maybe")).toBe(null);
58+
});
59+
});

src/functions.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Consumes a single temperature in Fahrenheit (a number) and converts to Celsius
3+
* using this formula:
4+
* C = (F - 32) * 5/9
5+
*/
6+
export function fahrenheitToCelius(temperature: number): number {
7+
return ((temperature - 32) * 5) / 9;
8+
}
9+
10+
11+
/**
12+
* Consumes three numbers and produces their sum. BUT you should only add a number
13+
* if the number is greater than zero.
14+
*/
15+
export function add3(first: number, second: number, third: number): number {
16+
let sum = 0;
17+
if (first > 0) {
18+
sum += first;
19+
}
20+
if (second > 0) {
21+
sum += second;
22+
}
23+
if (third > 0) {
24+
sum += third;
25+
}
26+
return sum;
27+
}
28+
29+
30+
/**
31+
* Consumes a string and produces the same string in UPPERCASE and with an exclamation
32+
* mark added to the end.
33+
*/
34+
export function shout(message: string): string {
35+
let upper: "";
36+
upper = message.toUpperCase() + "!";
37+
return upper;
38+
}
39+
40+
41+
/**
42+
* Consumes a string (a message) and returns a boolean if the string ends in a question
43+
* mark. Do not use an `if` statement in solving this question.
44+
*/
45+
export function isQuestion(message: string): boolean {
46+
return message.endsWith("?") ? true : false;
47+
//return message.endsWith("?")
48+
}
49+
50+
51+
/**
52+
* Consumes a word (a string) and returns either `true`, `false`, or `null`. If the string
53+
* is "yes" (upper or lower case), then return `true`. If the string is "no" (again, either
54+
* upper or lower case), then return `false`. Otherwise, return `null`.
55+
*/
56+
export function convertYesNo(word: string): boolean | null {
57+
if (word === "yes" || word === "YES") {
58+
return true;
59+
} else if (word == "no" || word === "NO") {
60+
return false;
61+
} else {
62+
return null;
63+
}
64+
}
65+
66+
67+

0 commit comments

Comments
 (0)