Skip to content
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: 1 addition & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
//Line 3 is adding 1 to the initial values of count which is 0 and reassing the new value (1+0 =1) to count
3 changes: 2 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let lastName = "Johnson";
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.

let initials = ``;

initials = firstName.slice(0, 1) + middleName.slice(0, 1) + lastName.slice(0, 1)
console.log(initials)
// https://www.google.com/search?q=get+first+character+of+string+mdn

9 changes: 6 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ console.log(`The base part of ${filePath} is ${base}`);
// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable

const dir = ;
const ext = ;
const dir = filePath.substring(0, lastSlashIndex);
console.log(`The dir part of the filePath variable is ${dir}`);
const lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex + 1);
console.log(`The ext of ${filePath} is ${ext}`);

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
10 changes: 10 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
// Try breaking down the expression and using documentation to explain what it means
// It will help to think about the order in which expressions are evaluated
// Try logging the value of num and running the program several times to build an idea of what the program is doing

//ANSWER
// const minimum = 1 sets the minimum value to 1
// const maximum = 100 sets the maximum value to 100
//Math.random() generates a random decimal number, zero inclusive but not 1.
// (maximum - minimum + 1) calculates how many whole numbers maximum and minimum
// Math.random() * (maximum - minimum + 1) ensures that the random decimal is within the desired range.
// Math.floor(...) rounds the number down to the nearest whole integer
// + minimum shifts the range upward so it starts at minimum instead of 0
// The program generated random numbers from 0 to 100 as whole integers.
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem?
We don't want the computer to run these 2 lines - how can we solve this problem?
//ANSWER:
//We change the two lines to comments by using double forward slash (//) at the beginning of the two lines.
6 changes: 6 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

const age = 33;
age = age + 1;
// ANSWER
// In the solution above, 'age' is a constant variable becuse the keyword 'const' was used with it hence the value cannot change.

// Solution:
let age = 33;
age += 1;
7 changes: 7 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
//ANSWER:
// The 'cityOfBirth' was not assigned before the console.log statement
// Solution will be to switch the lines as shown below:


const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
3 changes: 3 additions & 0 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ const last4Digits = cardNumber.slice(-4);
// Then run the code and see what error it gives.
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
// Then try updating the expression last4Digits is assigned to, in order to get the correct value

const cardNumber2 = String(cardNumber);
const last4Digits = cardNumber2.slice(-4);
8 changes: 6 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
//const 12HourClockTime = "20:53";
//const 24hourClockTime = "08:53";

// SUggested Solutions:
const ClockTime12Hour = "20:53";
const ClockTime24hour = "08:53";
12 changes: 7 additions & 5 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -12,11 +12,13 @@ console.log(`The percentage change is ${percentageChange}`);
// Read the code and then answer the questions below

// a) How many function calls are there in this file? Write down all the lines where a function call is made

//ANSWER: Four function calls asides console.log.
// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?

//ANSWER: The comma in the replaceAll() function in Line 5 is missing between the rguments. Put the comma back in to fix it.
// c) Identify all the lines that are variable reassignment statements

//ANSWER: Lines that are variable reassignment statements are Lines 4 & 5.
// d) Identify all the lines that are variable declarations

//ANSWER: Variable declarations line: 1,2,7,& 8
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
//ANSWER: The expression first removes the comma(,) in carPrice and then converts it to a number.

12 changes: 7 additions & 5 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ console.log(result);
// For the piece of code above, read the code and then answer the following questions

// a) How many variable declarations are there in this program?

//ANSWER: There were six variable declarations
// b) How many function calls are there?

//ANSWER: There were no function calls except for the log() function
// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

//ANSWER: The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

//ANSWER: Line 4 with find the remainder whenmovieLength is divided by 60 and assigns the remainder to remainingSeconds (24)
// e) What do you think the variable result represents? Can you think of a better name for this variable?

//ANSWER: The variable result represents the total lenth of the movie which shows the total hours, minutes and seconds remain in string format.
// A better name will be movieTotalTime.
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
//ANSWER: The code semed to work for all values of movieLength.
10 changes: 10 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 2. Lines 3 to 6: extracts "399" as a string and assigns the values to penceStringWithoutTrailingP.
// It uses the substring to extract from position 0 to 3.
// 3. Line 8: padStart is used to pad the values in penceStringWithoutTrailingP with "0" from the start of the value to make up a length of 3.
// Since penceStringWithoutTrailingP already has "399" with a length of 3, no padding is done.
// 4. Line 9 to 12: substring is used to extract from paddedPenceNumberString, starting from position 0 to 1 giving 3 which is stored in the variable pounds.
// 5. Line 14 to 16: substring is used to extract from paddedPenceNumberString, starting from position 1 to the end givin "99"
// "99" is padded with "0" from the end of the value to make a length of 2 but sincw "99" already has a length of 2, no padding is done.
// "99" is assigned to the variable pence.
// 6. Line 18: uses console.log() to print the values of pounds and pence preceeded by "£" and separated by "." to give "£3.99"

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@

function getAngleType(angle) {
// TODO: Implement this function
if (angle >= 0 && angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle > 180 && angle <= 360) {
return "Reflex angle";
} else {
return "Invalid angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -35,3 +48,18 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

const acute = getAngleType(70);
assertEquals(acute, "Acute angle");

const obtuse = getAngleType(140);
assertEquals(obtuse, "Obtuse angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const reflex = getAngleType(190);
assertEquals(reflex, "Reflex angle");

const invalid = getAngleType(370);
assertEquals(invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (numerator < denominator) {
return "true";
} else {
return "false";
}
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +36,7 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);

assertEquals(isProperFraction(1, 2), "true");
assertEquals(isProperFraction(2, 2), "false");
assertEquals(isProperFraction(7, 4), "false");
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@

function getCardValue(card) {
// TODO: Implement this function
rankCard = card.slice(0, 1);
if (rankCard === "A") {
return 11;
} else if (rankCard === "J" || rankCard === "Q" || rankCard === "K") {
return 10;
} else if (+rankCard >= 2 && +rankCard <= 10) {
return +rankCard;
} else {
return rankCard.toThrow();
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -46,9 +56,7 @@ try {
getCardValue("invalid");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
console.error("Error was not thrown for invalid card");
} catch (e) { }

// What other invalid card cases can you think of?
// What other invalid card cases can you think of? "13♠"
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle === 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(145)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when (angle === 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(245)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid angle" when (angle > 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(90)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});


// Special case: positives
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 7)).toEqual(false);
});

// Special case: negatives
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, -7)).toEqual(false);
});

// Special case: zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(10, 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,34 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)

test("Should return 2–10 for their respective card values", () => {
const cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
cards.forEach(card => {
expect(getCardValue(card)).toEqual(Number(card));
});
});

// Face Cards (J, Q, K)
test("Should return 2–10 for their respective card values", () => {
const cards = ["J", "Q", "K"];
cards.forEach(card => {
expect(getCardValue(card)).toEqual(10);
});
});

// Invalid Cards

test("Should return invalid for their respective card values", () => {
const cards = ["L", "M", "N"];
cards.forEach(card => {
expect(getCardValue(card)).toEqual(Number("Invalid"));

});
});



// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror
Expand Down
Loading