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
3 changes: 3 additions & 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,6 @@ 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
//Answer:
// Line 3 takes the current value of `count`, adds 1, and assigns the result back to `count`.
// The = operator is the assignment operator.
6 changes: 4 additions & 2 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ let lastName = "Johnson";
// Declare a variable called initials that stores the first character of each string.
// 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 = ``;

let initials = firstName[0] + middleName[0] + lastName[0];
console.log(initials)
// https://www.google.com/search?q=get+first+character+of+string+mdn

// The variable 'initials' takes the first character of firstName, middleName, and lastName,
// and combines them to form a new string.
5 changes: 3 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ 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.slice(0, lastSlashIndex);
const lastDotIndex = base.lastIndexOf(".");
const ext = base.slice(lastDotIndex);

// https://www.google.com/search?q=slice+mdn
6 changes: 6 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,9 @@ 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:
// The variable 'num' stores a random integer between 'minimum' (1) and 'maximum' (100), inclusive.
// Math.random() generates a random number from 0 up to (but not including) 1.
// Multiplying by (maximum - minimum + 1) scales it to the desired range.
// Math.floor() rounds down to the nearest whole number.
// Adding 'minimum' shifts the range so it starts at 1 instead of 0.
4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
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?
//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?
7 changes: 6 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age = 33;
age = age + 1;
console.log(age);

// We use 'let' to create a variable that can be reassigned.
// 'age' starts at 33, then we add 1 to it and store the result back in 'age'.
// console.log(age) outputs 34.
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);

// The error was that the variable 'cityOfBirth' was used before it was declared.
// Using 'const', a variable must be declared before it can be accessed.
// Correct order: first declare the variable, then use it in console.log.
3 changes: 2 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().slice(-4);
console.log(last4Digits);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
Expand Down
10 changes: 8 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const hour12ClockTime = "20:53";
const hour24ClockTime = "08:53";

console.log(hour12ClockTime);
console.log(hour24ClockTime);

// Variable names cannot start with a number, so I use hour12ClockTime and hour24ClockTime.
// This way the code works and prints the correct times.
25 changes: 24 additions & 1 deletion 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 @@ -20,3 +20,26 @@ console.log(`The percentage change is ${percentageChange}`);
// d) Identify all the lines that are variable declarations

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?


//Answer:
// a) Function calls are on these lines:
// carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// console.log(`The percentage change is ${percentageChange}`);

// b) Error occurs on the line:
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
// Reason: missing comma between arguments of replaceAll. Fix: replaceAll(",", "")

// c) Variable reassignment lines:
// carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// d) Variable declaration lines:
// let carPrice = "10,000";
// let priceAfterOneYear = "8,543";
// const priceDifference = carPrice - priceAfterOneYear;
// const percentageChange = (priceDifference / carPrice) * 100;

// e) Number(carPrice.replaceAll(",", "")) removes all commas from the string and converts it to a number so calculations can be done.
15 changes: 15 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ console.log(result);
// e) What do you think the variable result represents? Can you think of a better name for this variable?

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

//Answer:
// a) Variable declarations:
// movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result (6 in total)

// b) Function calls:
// console.log(result) (1 function call)

// c) movieLength % 60 gives the remainder when movieLength is divided by 60, representing the remaining seconds after full minutes are counted

// d) totalMinutes = (movieLength - remainingSeconds) / 60 calculates the total number of full minutes by removing leftover seconds and dividing by 60

// e) result represents the movie duration in hours:minutes:seconds format. A better name could be formattedMovieLength or movieTimeString

// f) The code works for any positive number of seconds, but single-digit minutes or seconds will not have leading zeros. To fix this, use padStart to format as hh:mm:ss
8 changes: 7 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const penceString = "399p";
// Initialize a string variable with the value "399p"

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

//Remove the trailing "p" from the string to get just the number part, e.g. "399"
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// Ensure the string has at least 3 characters by adding leading zeros if needed, e.g. "399" stays "399", "5" becomes "005"

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
// Take all but the last two characters to get the pounds part, e.g. "3" from "399"

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
// Take the last two characters for the pence part. If less than 2 characters, add a zero at the end, e.g. "99" from "399"

console.log(`£${pounds}.${pence}`);
// Combine pounds and pence into a formatted string and print, e.g. "£3.99"

// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds
Expand Down
9 changes: 9 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ Now try invoking the function `prompt` with a string input of `"What is your nam

What effect does calling the `prompt` function have?
What is the return value of `prompt`?

Answer:
Calling alert("Hello world!") shows a popup with the message "Hello world!" to the user.
The alert function does not return any value (returns undefined).
Calling prompt("What is your name?") shows a popup with a text input field asking the user for their name.
The return value of prompt is the text entered by the user, or null if the user presses Cancel.
Example:
const myName = prompt("What is your name?");
console.log(myName); // will display the name entered or null
10 changes: 10 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?


Answer:
1. console stores an object that contains methods for logging and debugging, such as log, warn, error, assert, etc.
2. typeof console returns "object", confirming that console is an object.
3. console.log or console.assert uses the dot (.) syntax to access a method (function) of the console object.
The dot means "this method belongs to this object".
4. Example:
console.log("Hello"); // calls the log method of the console object
console.assert(1 === 2, "Not equal!"); // calls the assert method of the console object
15 changes: 10 additions & 5 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
// Then, write the next test! :) Go through this process until all the cases are implemented

function getAngleType(angle) {
if (angle === 90) return "Right angle";
// read to the end, complete line 36, then pass your test here
if (angle === 90) return "Right angle"; // Case 1
if (angle < 90) return "Acute angle"; // Case 2
if (angle > 90 && angle < 180) return "Obtuse angle"; // Case 3
if (angle === 180) return "Straight angle"; // Case 4
if (angle > 180 && angle < 360) return "Reflex angle"; // Case 5
}

// we're going to use this helper function to make our assertions easier to read
Expand Down Expand Up @@ -43,14 +46,16 @@ assertEquals(acute, "Acute angle");
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(obtuse, "Obtuse angle");

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
// ====> write your test here, and then add a line to pass the test in the function above
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
19 changes: 15 additions & 4 deletions Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// A proper fraction is when absolute value of numerator < denominator
return Math.abs(numerator) < denominator;
}

// here's our helper again
Expand Down Expand Up @@ -40,14 +41,24 @@ assertEquals(improperFraction, false);
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
// ====> complete with your assertion
assertEquals(negativeFraction, true);

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
// ====> complete with your assertion
assertEquals(equalFraction, false);

// Stretch:
// What other scenarios could you test for?
// Test zero numerator
const zeroNumerator = isProperFraction(0, 5); // 0/5 is proper
assertEquals(zeroNumerator, true);

// Test negative denominator
const negativeDenominator = isProperFraction(2, -5); // numerator 2, denominator -5 => proper
assertEquals(negativeDenominator, true);

// Test numerator larger negative than denominator
const largeNegativeNumerator = isProperFraction(-6, 5); // -6/5 is improper
assertEquals(largeNegativeNumerator, false);
82 changes: 55 additions & 27 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,73 @@
// complete the rest of the tests and cases
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers

function getCardValue(card) {
if (rank === "A") return 11;
// Extract rank by removing the last character (suit)
const rank = card.slice(0, -1);

// Ace
if (rank === "A") return 11;

// Face cards
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") return 10;

// Number cards
const numericRank = Number(rank);
if (numericRank >= 2 && numericRank <= 9) return numericRank;

// Invalid card
throw new Error("Invalid card rank.");
}

// You need to write assertions for your function to check it works in different cases
// we're going to use this helper function to make our assertions easier to read
// if the actual output matches the target output, the test will pass
// Helper function for assertions
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// Acceptance criteria:

// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value
// Ace of Spades
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);

// Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
// Number Card: 5 of Hearts
const fiveofHearts = getCardValue("5♥");
// ====> write your test here, and then add a line to pass the test in the function above

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.

// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.

// Handle Invalid Cards:
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
assertEquals(fiveofHearts, 5);

// Face Card: King of Diamonds
const kingOfDiamonds = getCardValue("K♦");
assertEquals(kingOfDiamonds, 10);

// Face Card: Queen of Clubs
const queenOfClubs = getCardValue("Q♣");
assertEquals(queenOfClubs, 10);

// Number Card: 10 of Hearts
const tenOfHearts = getCardValue("10♥");
assertEquals(tenOfHearts, 10);

// Invalid Card: "1♠"
try {
getCardValue("1♠");
console.assert(false, "Expected an error for invalid card");
} catch (e) {
console.assert(
e.message === "Invalid card rank.",
`Unexpected error: ${e.message}`
);
}

// Invalid Card: "Z♣"
try {
getCardValue("Z♣");
console.assert(false, "Expected an error for invalid card");
} catch (e) {
console.assert(
e.message === "Invalid card rank.",
`Unexpected error: ${e.message}`
);
}
Loading