From 6082322c47306472999ead74c3bcb468d3d5d91b Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 20:51:02 +0000 Subject: [PATCH 01/12] PR #845: only Sprint 3 files --- Sprint-1/1-key-exercises/1-count.js | 6 --- Sprint-1/1-key-exercises/2-initials.js | 11 ----- Sprint-1/1-key-exercises/3-paths.js | 23 ----------- Sprint-1/1-key-exercises/4-random.js | 9 ---- Sprint-1/2-mandatory-errors/0.js | 2 - Sprint-1/2-mandatory-errors/1.js | 4 -- Sprint-1/2-mandatory-errors/2.js | 5 --- Sprint-1/2-mandatory-errors/3.js | 9 ---- Sprint-1/2-mandatory-errors/4.js | 2 - .../1-percentage-change.js | 22 ---------- .../3-mandatory-interpret/2-time-format.js | 25 ----------- Sprint-1/3-mandatory-interpret/3-to-pounds.js | 27 ------------ Sprint-1/4-stretch-explore/chrome.md | 18 -------- Sprint-1/4-stretch-explore/objects.md | 16 -------- Sprint-1/readme.md | 35 ---------------- Sprint-2/1-key-errors/0.js | 13 ------ Sprint-2/1-key-errors/1.js | 20 --------- Sprint-2/1-key-errors/2.js | 20 --------- Sprint-2/2-mandatory-debug/0.js | 14 ------- Sprint-2/2-mandatory-debug/1.js | 13 ------ Sprint-2/2-mandatory-debug/2.js | 24 ----------- Sprint-2/3-mandatory-implement/1-bmi.js | 19 --------- Sprint-2/3-mandatory-implement/2-cases.js | 16 -------- Sprint-2/3-mandatory-implement/3-to-pounds.js | 6 --- Sprint-2/4-mandatory-interpret/time-format.js | 34 --------------- Sprint-2/5-stretch-extend/format-time.js | 25 ----------- Sprint-2/readme.md | 41 ------------------- 27 files changed, 459 deletions(-) delete mode 100644 Sprint-1/1-key-exercises/1-count.js delete mode 100644 Sprint-1/1-key-exercises/2-initials.js delete mode 100644 Sprint-1/1-key-exercises/3-paths.js delete mode 100644 Sprint-1/1-key-exercises/4-random.js delete mode 100644 Sprint-1/2-mandatory-errors/0.js delete mode 100644 Sprint-1/2-mandatory-errors/1.js delete mode 100644 Sprint-1/2-mandatory-errors/2.js delete mode 100644 Sprint-1/2-mandatory-errors/3.js delete mode 100644 Sprint-1/2-mandatory-errors/4.js delete mode 100644 Sprint-1/3-mandatory-interpret/1-percentage-change.js delete mode 100644 Sprint-1/3-mandatory-interpret/2-time-format.js delete mode 100644 Sprint-1/3-mandatory-interpret/3-to-pounds.js delete mode 100644 Sprint-1/4-stretch-explore/chrome.md delete mode 100644 Sprint-1/4-stretch-explore/objects.md delete mode 100644 Sprint-1/readme.md delete mode 100644 Sprint-2/1-key-errors/0.js delete mode 100644 Sprint-2/1-key-errors/1.js delete mode 100644 Sprint-2/1-key-errors/2.js delete mode 100644 Sprint-2/2-mandatory-debug/0.js delete mode 100644 Sprint-2/2-mandatory-debug/1.js delete mode 100644 Sprint-2/2-mandatory-debug/2.js delete mode 100644 Sprint-2/3-mandatory-implement/1-bmi.js delete mode 100644 Sprint-2/3-mandatory-implement/2-cases.js delete mode 100644 Sprint-2/3-mandatory-implement/3-to-pounds.js delete mode 100644 Sprint-2/4-mandatory-interpret/time-format.js delete mode 100644 Sprint-2/5-stretch-extend/format-time.js delete mode 100644 Sprint-2/readme.md diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js deleted file mode 100644 index 117bcb2b6..000000000 --- a/Sprint-1/1-key-exercises/1-count.js +++ /dev/null @@ -1,6 +0,0 @@ -let count = 0; - -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 diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js deleted file mode 100644 index 47561f617..000000000 --- a/Sprint-1/1-key-exercises/2-initials.js +++ /dev/null @@ -1,11 +0,0 @@ -let firstName = "Creola"; -let middleName = "Katherine"; -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 = ``; - -// https://www.google.com/search?q=get+first+character+of+string+mdn - diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js deleted file mode 100644 index ab90ebb28..000000000 --- a/Sprint-1/1-key-exercises/3-paths.js +++ /dev/null @@ -1,23 +0,0 @@ -// The diagram below shows the different names for parts of a file path on a Unix operating system - -// ┌─────────────────────┬────────────┐ -// │ dir │ base │ -// ├──────┬ ├──────┬─────┤ -// │ root │ │ name │ ext │ -// " / home/user/dir / file .txt " -// └──────┴──────────────┴──────┴─────┘ - -// (All spaces in the "" line should be ignored. They are purely for formatting.) - -const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; -const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(lastSlashIndex + 1); -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 = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js deleted file mode 100644 index 292f83aab..000000000 --- a/Sprint-1/1-key-exercises/4-random.js +++ /dev/null @@ -1,9 +0,0 @@ -const minimum = 1; -const maximum = 100; - -const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; - -// In this exercise, you will need to work out what num represents? -// 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 diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js deleted file mode 100644 index cf6c5039f..000000000 --- a/Sprint-1/2-mandatory-errors/0.js +++ /dev/null @@ -1,2 +0,0 @@ -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? \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js deleted file mode 100644 index 7a43cbea7..000000000 --- a/Sprint-1/2-mandatory-errors/1.js +++ /dev/null @@ -1,4 +0,0 @@ -// trying to create an age variable and then reassign the value by 1 - -const age = 33; -age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js deleted file mode 100644 index e09b89831..000000000 --- a/Sprint-1/2-mandatory-errors/2.js +++ /dev/null @@ -1,5 +0,0 @@ -// 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"; diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js deleted file mode 100644 index ec101884d..000000000 --- a/Sprint-1/2-mandatory-errors/3.js +++ /dev/null @@ -1,9 +0,0 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); - -// The last4Digits variable should store the last 4 digits of cardNumber -// However, the code isn't working -// Before running the code, make and explain a prediction about why the code won't work -// 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 diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js deleted file mode 100644 index 21dad8c5d..000000000 --- a/Sprint-1/2-mandatory-errors/4.js +++ /dev/null @@ -1,2 +0,0 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js deleted file mode 100644 index e24ecb8e1..000000000 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ /dev/null @@ -1,22 +0,0 @@ -let carPrice = "10,000"; -let priceAfterOneYear = "8,543"; - -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); - -const priceDifference = carPrice - priceAfterOneYear; -const percentageChange = (priceDifference / carPrice) * 100; - -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 - -// 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? - -// c) Identify all the lines that are variable reassignment statements - -// 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? diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js deleted file mode 100644 index 47d239558..000000000 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ /dev/null @@ -1,25 +0,0 @@ -const movieLength = 8784; // length of movie in seconds - -const remainingSeconds = movieLength % 60; -const totalMinutes = (movieLength - remainingSeconds) / 60; - -const remainingMinutes = totalMinutes % 60; -const totalHours = (totalMinutes - remainingMinutes) / 60; - -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; -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? - -// b) How many function calls are there? - -// c) Using documentation, explain what the expression movieLength % 60 represents -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators - -// d) Interpret line 4, what does the expression assigned to totalMinutes mean? - -// 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 diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js deleted file mode 100644 index 60c9ace69..000000000 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ /dev/null @@ -1,27 +0,0 @@ -const penceString = "399p"; - -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); - -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); - -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); - -console.log(`£${pounds}.${pence}`); - -// This program takes a string representing a price in pence -// The program then builds up a string representing the price in pounds - -// You need to do a step-by-step breakdown of each line in this program -// Try and describe the purpose / rationale behind each step - -// To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md deleted file mode 100644 index e7dd5feaf..000000000 --- a/Sprint-1/4-stretch-explore/chrome.md +++ /dev/null @@ -1,18 +0,0 @@ -Open a new window in Chrome, - -then locate the **Console** tab. - -Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). -Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. - -Let's try an example. - -In the Chrome console, -invoke the function `alert` with an input string of `"Hello world!"`; - -What effect does calling the `alert` function have? - -Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. - -What effect does calling the `prompt` function have? -What is the return value of `prompt`? diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md deleted file mode 100644 index 0216dee56..000000000 --- a/Sprint-1/4-stretch-explore/objects.md +++ /dev/null @@ -1,16 +0,0 @@ -## Objects - -In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. - -Open the Chrome devtools Console, type in `console.log` and then hit enter - -What output do you get? - -Now enter just `console` in the Console, what output do you get back? - -Try also entering `typeof console` - -Answer the following questions: - -What does `console` store? -What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? diff --git a/Sprint-1/readme.md b/Sprint-1/readme.md deleted file mode 100644 index 62d24c958..000000000 --- a/Sprint-1/readme.md +++ /dev/null @@ -1,35 +0,0 @@ -# 🧭 Guide to Week 1 exercises - -> https://programming.codeyourfuture.io/structuring-data/sprints/1/prep/ - -> [!TIP] -> You should always do the prep work _before_ attempting the coursework. -> The prep shows you _how_ to do the coursework. -> There is often a step by step video you can code along with too. -> Do the prep. - -This README will guide you through the different sections for this week. - -## 1 Exercises - -In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. - -https://developer.mozilla.org/en-US/docs/Web/JavaScript - -## 2 Errors - -In this section, you'll need to go to each file in `errors` directory and run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. - -## 3 Interpret - -In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. - -You must use documentation to make sense of anything unfamiliar - learning how to look things up this way is a fundamental part of being a developer! - -You can also use `console.log` to check the value of different variables in the code. - -https://developer.mozilla.org/en-US/docs/Web/JavaScript - -## 4 Explore - Stretch 💪 - -This stretch activity will get you to start exploring new concepts and environments by yourself. It will do so by prompting you to reflect on some questions. diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js deleted file mode 100644 index 653d6f5a0..000000000 --- a/Sprint-2/1-key-errors/0.js +++ /dev/null @@ -1,13 +0,0 @@ -// Predict and explain first... -// =============> write your prediction here - -// call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring - -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} - -// =============> write your explanation here -// =============> write your new code here diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js deleted file mode 100644 index f2d56151f..000000000 --- a/Sprint-2/1-key-errors/1.js +++ /dev/null @@ -1,20 +0,0 @@ -// Predict and explain first... - -// Why will an error occur when this program runs? -// =============> write your prediction here - -// Try playing computer with the example to work out what is going on - -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; - - return percentage; -} - -console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js deleted file mode 100644 index aad57f7cf..000000000 --- a/Sprint-2/1-key-errors/2.js +++ /dev/null @@ -1,20 +0,0 @@ - -// Predict and explain first BEFORE you run any code... - -// this function should square any number but instead we're going to get an error - -// =============> write your prediction of the error here - -function square(3) { - return num * num; -} - -// =============> write the error message here - -// =============> explain this error message here - -// Finally, correct the code to fix the problem - -// =============> write your new code here - - diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js deleted file mode 100644 index b27511b41..000000000 --- a/Sprint-2/2-mandatory-debug/0.js +++ /dev/null @@ -1,14 +0,0 @@ -// Predict and explain first... - -// =============> write your prediction here - -function multiply(a, b) { - console.log(a * b); -} - -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js deleted file mode 100644 index 37cedfbcf..000000000 --- a/Sprint-2/2-mandatory-debug/1.js +++ /dev/null @@ -1,13 +0,0 @@ -// Predict and explain first... -// =============> write your prediction here - -function sum(a, b) { - return; - a + b; -} - -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js deleted file mode 100644 index 57d3f5dc3..000000000 --- a/Sprint-2/2-mandatory-debug/2.js +++ /dev/null @@ -1,24 +0,0 @@ -// Predict and explain first... - -// Predict the output of the following code: -// =============> Write your prediction here - -const num = 103; - -function getLastDigit() { - return num.toString().slice(-1); -} - -console.log(`The last digit of 42 is ${getLastDigit(42)}`); -console.log(`The last digit of 105 is ${getLastDigit(105)}`); -console.log(`The last digit of 806 is ${getLastDigit(806)}`); - -// Now run the code and compare the output to your prediction -// =============> write the output here -// Explain why the output is the way it is -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here - -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js deleted file mode 100644 index 17b1cbde1..000000000 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ /dev/null @@ -1,19 +0,0 @@ -// Below are the steps for how BMI is calculated - -// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. - -// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: - -// squaring your height: 1.73 x 1.73 = 2.99 -// dividing 70 by 2.99 = 23.41 -// Your result will be displayed to 1 decimal place, for example 23.4. - -// You will need to implement a function that calculates the BMI of someone based off their weight and height - -// Given someone's weight in kg and height in metres -// Then when we call this function with the weight and height -// It should return their Body Mass Index to 1 decimal place - -function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js deleted file mode 100644 index 5b0ef77ad..000000000 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ /dev/null @@ -1,16 +0,0 @@ -// A set of words can be grouped together in different cases. - -// For example, "hello there" in snake case would be written "hello_there" -// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. - -// Implement a function that: - -// Given a string input like "hello there" -// When we call this function with the input string -// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" - -// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" - -// You will need to come up with an appropriate name for the function -// Use the MDN string documentation to help you find a solution -// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js deleted file mode 100644 index 6265a1a70..000000000 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ /dev/null @@ -1,6 +0,0 @@ -// In Sprint-1, there is a program written in interpret/to-pounds.js - -// You will need to take this code and turn it into a reusable block of code. -// You will need to declare a function called toPounds with an appropriately named parameter. - -// You should call this function a number of times to check it works for different inputs diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js deleted file mode 100644 index 7c98eb0e8..000000000 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ /dev/null @@ -1,34 +0,0 @@ -function pad(num) { - return num.toString().padStart(2, "0"); -} - -function formatTimeDisplay(seconds) { - const remainingSeconds = seconds % 60; - const totalMinutes = (seconds - remainingSeconds) / 60; - const remainingMinutes = totalMinutes % 60; - const totalHours = (totalMinutes - remainingMinutes) / 60; - - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; -} - -// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit -// to help you answer these questions - -// Questions - -// a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here - -// Call formatTimeDisplay with an input of 61, now answer the following: - -// b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here - -// c) What is the return value of pad is called for the first time? -// =============> write your answer here - -// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here - -// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js deleted file mode 100644 index 32a32e66b..000000000 --- a/Sprint-2/5-stretch-extend/format-time.js +++ /dev/null @@ -1,25 +0,0 @@ -// This is the latest solution to the problem from the prep. -// Make sure to do the prep before you do the coursework -// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. - -function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; -} - -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); diff --git a/Sprint-2/readme.md b/Sprint-2/readme.md deleted file mode 100644 index 44c118e33..000000000 --- a/Sprint-2/readme.md +++ /dev/null @@ -1,41 +0,0 @@ -# 🧭 Guide to week 2 exercises - -> https://programming.codeyourfuture.io/structuring-data/sprints/2/prep/ - -> [!TIP] -> You should always do the prep work _before_ attempting the coursework. -> The prep shows you how to do the coursework. -> There is often a step by step video you can code along with too. -> Do the prep. - -## 1 Errors - -In this section, you need to go to each file in `errors` directory. Read the file and predict what error will happen. Then run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. - -## 2 Debug - -In this section, you need to go to each file in `debug` to **explain and predict** why the program isn't behaving as intended. Then you'll need to run the program with node to check your prediction. You will also need to correct the code too. - -## 3 Implement - -In this section, you will have a short set of requirements about a function. You will need to implement a function based off this set of requirements. Make sure you check your function works for a number of different inputs. - -Here is a recommended order: - -1. `1-bmi.js` -1. `2-cases.js` -1. `3-to-pounds.js` - -## 4 Interpret - -In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. - -You must use documentation to make sense of anything unfamiliar. Learning how to look things up this way is a fundamental part of being a developer! - -You can also use `console.log` to check the value of different variables in the code. - -## 5 Extend - -In the prep for this sprint, we developed a function to convert 24 hour clock times to 12 hour clock times. - -Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. This section is not mandatory, but it will also help you solve some similar kata in Codewars. From 2e97a227644ef27d1809dcad9253730cf5a47c33 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 20:51:28 +0000 Subject: [PATCH 02/12] Add initial implementations and explanations for Sprint 1 and Sprint 2 exercises --- Sprint-1/1-key-exercises/1-count.js | 6 +++ Sprint-1/1-key-exercises/2-initials.js | 11 +++++ Sprint-1/1-key-exercises/3-paths.js | 23 +++++++++++ Sprint-1/1-key-exercises/4-random.js | 9 ++++ Sprint-1/2-mandatory-errors/0.js | 2 + Sprint-1/2-mandatory-errors/1.js | 4 ++ Sprint-1/2-mandatory-errors/2.js | 5 +++ Sprint-1/2-mandatory-errors/3.js | 9 ++++ Sprint-1/2-mandatory-errors/4.js | 2 + .../1-percentage-change.js | 22 ++++++++++ .../3-mandatory-interpret/2-time-format.js | 25 +++++++++++ Sprint-1/3-mandatory-interpret/3-to-pounds.js | 27 ++++++++++++ Sprint-1/4-stretch-explore/chrome.md | 18 ++++++++ Sprint-1/4-stretch-explore/objects.md | 16 ++++++++ Sprint-1/readme.md | 35 ++++++++++++++++ Sprint-2/1-key-errors/0.js | 13 ++++++ Sprint-2/1-key-errors/1.js | 20 +++++++++ Sprint-2/1-key-errors/2.js | 20 +++++++++ Sprint-2/2-mandatory-debug/0.js | 14 +++++++ Sprint-2/2-mandatory-debug/1.js | 13 ++++++ Sprint-2/2-mandatory-debug/2.js | 24 +++++++++++ Sprint-2/3-mandatory-implement/1-bmi.js | 19 +++++++++ Sprint-2/3-mandatory-implement/2-cases.js | 16 ++++++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 6 +++ Sprint-2/4-mandatory-interpret/time-format.js | 34 +++++++++++++++ Sprint-2/5-stretch-extend/format-time.js | 25 +++++++++++ Sprint-2/readme.md | 41 +++++++++++++++++++ 27 files changed, 459 insertions(+) create mode 100644 Sprint-1/1-key-exercises/1-count.js create mode 100644 Sprint-1/1-key-exercises/2-initials.js create mode 100644 Sprint-1/1-key-exercises/3-paths.js create mode 100644 Sprint-1/1-key-exercises/4-random.js create mode 100644 Sprint-1/2-mandatory-errors/0.js create mode 100644 Sprint-1/2-mandatory-errors/1.js create mode 100644 Sprint-1/2-mandatory-errors/2.js create mode 100644 Sprint-1/2-mandatory-errors/3.js create mode 100644 Sprint-1/2-mandatory-errors/4.js create mode 100644 Sprint-1/3-mandatory-interpret/1-percentage-change.js create mode 100644 Sprint-1/3-mandatory-interpret/2-time-format.js create mode 100644 Sprint-1/3-mandatory-interpret/3-to-pounds.js create mode 100644 Sprint-1/4-stretch-explore/chrome.md create mode 100644 Sprint-1/4-stretch-explore/objects.md create mode 100644 Sprint-1/readme.md create mode 100644 Sprint-2/1-key-errors/0.js create mode 100644 Sprint-2/1-key-errors/1.js create mode 100644 Sprint-2/1-key-errors/2.js create mode 100644 Sprint-2/2-mandatory-debug/0.js create mode 100644 Sprint-2/2-mandatory-debug/1.js create mode 100644 Sprint-2/2-mandatory-debug/2.js create mode 100644 Sprint-2/3-mandatory-implement/1-bmi.js create mode 100644 Sprint-2/3-mandatory-implement/2-cases.js create mode 100644 Sprint-2/3-mandatory-implement/3-to-pounds.js create mode 100644 Sprint-2/4-mandatory-interpret/time-format.js create mode 100644 Sprint-2/5-stretch-extend/format-time.js create mode 100644 Sprint-2/readme.md diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js new file mode 100644 index 000000000..117bcb2b6 --- /dev/null +++ b/Sprint-1/1-key-exercises/1-count.js @@ -0,0 +1,6 @@ +let count = 0; + +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 diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js new file mode 100644 index 000000000..47561f617 --- /dev/null +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -0,0 +1,11 @@ +let firstName = "Creola"; +let middleName = "Katherine"; +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 = ``; + +// https://www.google.com/search?q=get+first+character+of+string+mdn + diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js new file mode 100644 index 000000000..ab90ebb28 --- /dev/null +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -0,0 +1,23 @@ +// The diagram below shows the different names for parts of a file path on a Unix operating system + +// ┌─────────────────────┬────────────┐ +// │ dir │ base │ +// ├──────┬ ├──────┬─────┤ +// │ root │ │ name │ ext │ +// " / home/user/dir / file .txt " +// └──────┴──────────────┴──────┴─────┘ + +// (All spaces in the "" line should be ignored. They are purely for formatting.) + +const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; +const lastSlashIndex = filePath.lastIndexOf("/"); +const base = filePath.slice(lastSlashIndex + 1); +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 = ; + +// https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js new file mode 100644 index 000000000..292f83aab --- /dev/null +++ b/Sprint-1/1-key-exercises/4-random.js @@ -0,0 +1,9 @@ +const minimum = 1; +const maximum = 100; + +const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; + +// In this exercise, you will need to work out what num represents? +// 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 diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js new file mode 100644 index 000000000..cf6c5039f --- /dev/null +++ b/Sprint-1/2-mandatory-errors/0.js @@ -0,0 +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? \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js new file mode 100644 index 000000000..7a43cbea7 --- /dev/null +++ b/Sprint-1/2-mandatory-errors/1.js @@ -0,0 +1,4 @@ +// trying to create an age variable and then reassign the value by 1 + +const age = 33; +age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js new file mode 100644 index 000000000..e09b89831 --- /dev/null +++ b/Sprint-1/2-mandatory-errors/2.js @@ -0,0 +1,5 @@ +// 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"; diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js new file mode 100644 index 000000000..ec101884d --- /dev/null +++ b/Sprint-1/2-mandatory-errors/3.js @@ -0,0 +1,9 @@ +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.slice(-4); + +// The last4Digits variable should store the last 4 digits of cardNumber +// However, the code isn't working +// Before running the code, make and explain a prediction about why the code won't work +// 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 diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js new file mode 100644 index 000000000..21dad8c5d --- /dev/null +++ b/Sprint-1/2-mandatory-errors/4.js @@ -0,0 +1,2 @@ +const 12HourClockTime = "20:53"; +const 24hourClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js new file mode 100644 index 000000000..e24ecb8e1 --- /dev/null +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -0,0 +1,22 @@ +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; + +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 + +// 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? + +// c) Identify all the lines that are variable reassignment statements + +// 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? diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js new file mode 100644 index 000000000..47d239558 --- /dev/null +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -0,0 +1,25 @@ +const movieLength = 8784; // length of movie in seconds + +const remainingSeconds = movieLength % 60; +const totalMinutes = (movieLength - remainingSeconds) / 60; + +const remainingMinutes = totalMinutes % 60; +const totalHours = (totalMinutes - remainingMinutes) / 60; + +const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +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? + +// b) How many function calls are there? + +// c) Using documentation, explain what the expression movieLength % 60 represents +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators + +// d) Interpret line 4, what does the expression assigned to totalMinutes mean? + +// 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 diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js new file mode 100644 index 000000000..60c9ace69 --- /dev/null +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -0,0 +1,27 @@ +const penceString = "399p"; + +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 +); + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + +console.log(`£${pounds}.${pence}`); + +// This program takes a string representing a price in pence +// The program then builds up a string representing the price in pounds + +// You need to do a step-by-step breakdown of each line in this program +// Try and describe the purpose / rationale behind each step + +// To begin, we can start with +// 1. const penceString = "399p": initialises a string variable with the value "399p" diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md new file mode 100644 index 000000000..e7dd5feaf --- /dev/null +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -0,0 +1,18 @@ +Open a new window in Chrome, + +then locate the **Console** tab. + +Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). +Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. + +Let's try an example. + +In the Chrome console, +invoke the function `alert` with an input string of `"Hello world!"`; + +What effect does calling the `alert` function have? + +Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. + +What effect does calling the `prompt` function have? +What is the return value of `prompt`? diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md new file mode 100644 index 000000000..0216dee56 --- /dev/null +++ b/Sprint-1/4-stretch-explore/objects.md @@ -0,0 +1,16 @@ +## Objects + +In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. + +Open the Chrome devtools Console, type in `console.log` and then hit enter + +What output do you get? + +Now enter just `console` in the Console, what output do you get back? + +Try also entering `typeof console` + +Answer the following questions: + +What does `console` store? +What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? diff --git a/Sprint-1/readme.md b/Sprint-1/readme.md new file mode 100644 index 000000000..62d24c958 --- /dev/null +++ b/Sprint-1/readme.md @@ -0,0 +1,35 @@ +# 🧭 Guide to Week 1 exercises + +> https://programming.codeyourfuture.io/structuring-data/sprints/1/prep/ + +> [!TIP] +> You should always do the prep work _before_ attempting the coursework. +> The prep shows you _how_ to do the coursework. +> There is often a step by step video you can code along with too. +> Do the prep. + +This README will guide you through the different sections for this week. + +## 1 Exercises + +In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 2 Errors + +In this section, you'll need to go to each file in `errors` directory and run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. + +## 3 Interpret + +In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. + +You must use documentation to make sense of anything unfamiliar - learning how to look things up this way is a fundamental part of being a developer! + +You can also use `console.log` to check the value of different variables in the code. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 4 Explore - Stretch 💪 + +This stretch activity will get you to start exploring new concepts and environments by yourself. It will do so by prompting you to reflect on some questions. diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js new file mode 100644 index 000000000..653d6f5a0 --- /dev/null +++ b/Sprint-2/1-key-errors/0.js @@ -0,0 +1,13 @@ +// Predict and explain first... +// =============> write your prediction here + +// call the function capitalise with a string input +// interpret the error message and figure out why an error is occurring + +function capitalise(str) { + let str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +// =============> write your explanation here +// =============> write your new code here diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js new file mode 100644 index 000000000..f2d56151f --- /dev/null +++ b/Sprint-2/1-key-errors/1.js @@ -0,0 +1,20 @@ +// Predict and explain first... + +// Why will an error occur when this program runs? +// =============> write your prediction here + +// Try playing computer with the example to work out what is going on + +function convertToPercentage(decimalNumber) { + const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(decimalNumber); + +// =============> write your explanation here + +// Finally, correct the code to fix the problem +// =============> write your new code here diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js new file mode 100644 index 000000000..aad57f7cf --- /dev/null +++ b/Sprint-2/1-key-errors/2.js @@ -0,0 +1,20 @@ + +// Predict and explain first BEFORE you run any code... + +// this function should square any number but instead we're going to get an error + +// =============> write your prediction of the error here + +function square(3) { + return num * num; +} + +// =============> write the error message here + +// =============> explain this error message here + +// Finally, correct the code to fix the problem + +// =============> write your new code here + + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js new file mode 100644 index 000000000..b27511b41 --- /dev/null +++ b/Sprint-2/2-mandatory-debug/0.js @@ -0,0 +1,14 @@ +// Predict and explain first... + +// =============> write your prediction here + +function multiply(a, b) { + console.log(a * b); +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +// =============> write your explanation here + +// Finally, correct the code to fix the problem +// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js new file mode 100644 index 000000000..37cedfbcf --- /dev/null +++ b/Sprint-2/2-mandatory-debug/1.js @@ -0,0 +1,13 @@ +// Predict and explain first... +// =============> write your prediction here + +function sum(a, b) { + return; + a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +// =============> write your explanation here +// Finally, correct the code to fix the problem +// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js new file mode 100644 index 000000000..57d3f5dc3 --- /dev/null +++ b/Sprint-2/2-mandatory-debug/2.js @@ -0,0 +1,24 @@ +// Predict and explain first... + +// Predict the output of the following code: +// =============> Write your prediction here + +const num = 103; + +function getLastDigit() { + return num.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); +console.log(`The last digit of 105 is ${getLastDigit(105)}`); +console.log(`The last digit of 806 is ${getLastDigit(806)}`); + +// Now run the code and compare the output to your prediction +// =============> write the output here +// Explain why the output is the way it is +// =============> write your explanation here +// Finally, correct the code to fix the problem +// =============> write your new code here + +// This program should tell the user the last digit of each number. +// Explain why getLastDigit is not working properly - correct the problem diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js new file mode 100644 index 000000000..17b1cbde1 --- /dev/null +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -0,0 +1,19 @@ +// Below are the steps for how BMI is calculated + +// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. + +// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: + +// squaring your height: 1.73 x 1.73 = 2.99 +// dividing 70 by 2.99 = 23.41 +// Your result will be displayed to 1 decimal place, for example 23.4. + +// You will need to implement a function that calculates the BMI of someone based off their weight and height + +// Given someone's weight in kg and height in metres +// Then when we call this function with the weight and height +// It should return their Body Mass Index to 1 decimal place + +function calculateBMI(weight, height) { + // return the BMI of someone based off their weight and height +} \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js new file mode 100644 index 000000000..5b0ef77ad --- /dev/null +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -0,0 +1,16 @@ +// A set of words can be grouped together in different cases. + +// For example, "hello there" in snake case would be written "hello_there" +// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. + +// Implement a function that: + +// Given a string input like "hello there" +// When we call this function with the input string +// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" + +// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" + +// You will need to come up with an appropriate name for the function +// Use the MDN string documentation to help you find a solution +// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js new file mode 100644 index 000000000..6265a1a70 --- /dev/null +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -0,0 +1,6 @@ +// In Sprint-1, there is a program written in interpret/to-pounds.js + +// You will need to take this code and turn it into a reusable block of code. +// You will need to declare a function called toPounds with an appropriately named parameter. + +// You should call this function a number of times to check it works for different inputs diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js new file mode 100644 index 000000000..7c98eb0e8 --- /dev/null +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -0,0 +1,34 @@ +function pad(num) { + return num.toString().padStart(2, "0"); +} + +function formatTimeDisplay(seconds) { + const remainingSeconds = seconds % 60; + const totalMinutes = (seconds - remainingSeconds) / 60; + const remainingMinutes = totalMinutes % 60; + const totalHours = (totalMinutes - remainingMinutes) / 60; + + return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; +} + +// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit +// to help you answer these questions + +// Questions + +// a) When formatTimeDisplay is called how many times will pad be called? +// =============> write your answer here + +// Call formatTimeDisplay with an input of 61, now answer the following: + +// b) What is the value assigned to num when pad is called for the first time? +// =============> write your answer here + +// c) What is the return value of pad is called for the first time? +// =============> write your answer here + +// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer +// =============> write your answer here + +// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer +// =============> write your answer here diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js new file mode 100644 index 000000000..32a32e66b --- /dev/null +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -0,0 +1,25 @@ +// This is the latest solution to the problem from the prep. +// Make sure to do the prep before you do the coursework +// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. + +function formatAs12HourClock(time) { + const hours = Number(time.slice(0, 2)); + if (hours > 12) { + return `${hours - 12}:00 pm`; + } + return `${time} am`; +} + +const currentOutput = formatAs12HourClock("08:00"); +const targetOutput = "08:00 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +const currentOutput2 = formatAs12HourClock("23:00"); +const targetOutput2 = "11:00 pm"; +console.assert( + currentOutput2 === targetOutput2, + `current output: ${currentOutput2}, target output: ${targetOutput2}` +); diff --git a/Sprint-2/readme.md b/Sprint-2/readme.md new file mode 100644 index 000000000..44c118e33 --- /dev/null +++ b/Sprint-2/readme.md @@ -0,0 +1,41 @@ +# 🧭 Guide to week 2 exercises + +> https://programming.codeyourfuture.io/structuring-data/sprints/2/prep/ + +> [!TIP] +> You should always do the prep work _before_ attempting the coursework. +> The prep shows you how to do the coursework. +> There is often a step by step video you can code along with too. +> Do the prep. + +## 1 Errors + +In this section, you need to go to each file in `errors` directory. Read the file and predict what error will happen. Then run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. + +## 2 Debug + +In this section, you need to go to each file in `debug` to **explain and predict** why the program isn't behaving as intended. Then you'll need to run the program with node to check your prediction. You will also need to correct the code too. + +## 3 Implement + +In this section, you will have a short set of requirements about a function. You will need to implement a function based off this set of requirements. Make sure you check your function works for a number of different inputs. + +Here is a recommended order: + +1. `1-bmi.js` +1. `2-cases.js` +1. `3-to-pounds.js` + +## 4 Interpret + +In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. + +You must use documentation to make sense of anything unfamiliar. Learning how to look things up this way is a fundamental part of being a developer! + +You can also use `console.log` to check the value of different variables in the code. + +## 5 Extend + +In the prep for this sprint, we developed a function to convert 24 hour clock times to 12 hour clock times. + +Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. This section is not mandatory, but it will also help you solve some similar kata in Codewars. From d75ebfb9d246cc0e5b6836feee07e7714f4104f2 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 20:55:09 +0000 Subject: [PATCH 03/12] Implement angle type identification in getAngleType function and add corresponding tests --- .../implement/1-get-angle-type.js | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index ca1dfe7f2..fc73373de 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -8,11 +8,33 @@ // Then, write the next test! :) Go through this process until all the cases are implemented function getAngleType(angle) { + // Case 1: Identify Right Angles if (angle === 90) { return "Right angle"; } - // Run the tests, work out what Case 2 is testing, and implement the required code here. - // Then keep going for the other cases, one at a time. + + // Case 2: Identify Acute Angles + if (angle < 90) { + return "Acute angle"; + } + + // Case 3: Identify Obtuse Angles + if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } + + // Case 4: Identify Straight Angles + if (angle === 180) { + return "Straight angle"; + } + + // Case 5: Identify Reflex Angles + if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + + // Optional: handle invalid angles + return "Invalid angle"; } // The line below allows us to load the getAngleType function into tests in other files. @@ -50,14 +72,18 @@ 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 \ No newline at end of file +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); + +console.log("All tests ran! If no errors, all passed."); From 878d49e44cb8cda5a8596de6dc16f347901826b3 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 20:56:00 +0000 Subject: [PATCH 04/12] Refactor isProperFraction function to handle absolute values and complete assertions for all test cases --- .../implement/2-is-proper-fraction.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index a4739af77..092bfefe8 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -8,8 +8,11 @@ // write one test at a time, and make it pass, build your solution up methodically function isProperFraction(numerator, denominator) { - if (numerator < denominator) { + // Proper fraction if absolute numerator is less than denominator + if (Math.abs(numerator) < denominator) { return true; + } else { + return false; } } @@ -46,14 +49,19 @@ 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? +// Example: numerator = 0, denominator = 5 (0/5 is a proper fraction) +const zeroNumerator = isProperFraction(0, 5); +assertEquals(zeroNumerator, true); + +console.log("All tests ran! If no errors, all passed."); From 6032ba9c218fb71a02248ca2636110bd72eef473 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 20:58:03 +0000 Subject: [PATCH 05/12] Implement getCardValue function to handle card ranks, including Aces, face cards, and invalid inputs --- .../implement/3-get-card-value.js | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index 266525d1b..087e3b5e9 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -8,9 +8,26 @@ // 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) { + // Extract the rank (everything except the last character, which is the suit emoji) + const rank = card.slice(0, -1); + + // Handle Ace if (rank === "A") { return 11; } + + // Handle Face Cards (J, Q, K) + if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") { + return 10; + } + + // Handle Number Cards (2-9) + if (!isNaN(rank)) { + return Number(rank); + } + + // Handle Invalid Cards + throw new Error("Invalid card rank."); } // The line below allows us to load the getCardValue function into tests in other files. @@ -26,6 +43,7 @@ function assertEquals(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), @@ -39,19 +57,32 @@ assertEquals(aceofSpades, 11); // 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). const fiveofHearts = getCardValue("5♥"); -// ====> write your test here, and then add a line to pass the test in the function above +assertEquals(fiveofHearts, 5); + +const tenofClubs = getCardValue("10♣"); +assertEquals(tenofClubs, 10); // Handle Face Cards (J, Q, K): -// Given a card with a rank of "10," "J," "Q," or "K", +// Given a card with a rank of "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. +const jackOfDiamonds = getCardValue("J♦"); +assertEquals(jackOfDiamonds, 10); + +const queenOfSpades = getCardValue("Q♠"); +assertEquals(queenOfSpades, 10); -// 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. +const kingOfHearts = getCardValue("K♥"); +assertEquals(kingOfHearts, 10); // 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." +try { + getCardValue("Z♠"); +} catch (e) { + assertEquals(e.message, "Invalid card rank."); +} + +console.log("All tests ran! If no errors, all passed."); From cc9f8f2594411a2c2e9d02c62fa9405ccbabb89d Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 21:06:39 +0000 Subject: [PATCH 06/12] Refactor angle type tests for clarity and readability --- .../1-get-angle-type.test.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index 4a92a3e82..9673b50d6 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -6,21 +6,30 @@ test("should identify right angle (90°)", () => { expect(getAngleType(90)).toEqual("Right angle"); }); -// REPLACE the comments with the tests -// make your test descriptions as clear and readable as possible - // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" +test("should identify acute angle (<90°)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); +}); // Case 3: Identify Obtuse Angles: // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" +test("should identify obtuse angle (>90° and <180°)", () => { + expect(getAngleType(120)).toEqual("Obtuse angle"); +}); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" +test("should identify straight angle (180°)", () => { + expect(getAngleType(180)).toEqual("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" +test("should identify reflex angle (>180° and <360°)", () => { + expect(getAngleType(270)).toEqual("Reflex angle"); +}); From 6a9a7cd64fa179f0cd188217f545b462b1a37630 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 21:07:26 +0000 Subject: [PATCH 07/12] Enhance isProperFraction tests with detailed descriptions for clarity --- .../2-is-proper-fraction.test.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index caf08d15b..d194fbb78 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -2,12 +2,27 @@ // We will use the same function, but write tests for it using Jest in this file. const isProperFraction = require("../implement/2-is-proper-fraction"); -test("should return true for a proper fraction", () => { +test("should return true for a proper fraction (numerator < denominator)", () => { expect(isProperFraction(2, 3)).toEqual(true); }); // Case 2: Identify Improper Fractions: +// When the numerator is greater than or equal to the denominator, +// Then the function should return false +test("should return false for an improper fraction (numerator > denominator)", () => { + expect(isProperFraction(5, 2)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +// When the numerator is negative but absolute value < denominator, +// Then the function should return true +test("should return true for a negative proper fraction (|numerator| < denominator)", () => { + expect(isProperFraction(-4, 7)).toEqual(true); +}); // Case 4: Identify Equal Numerator and Denominator: +// When the numerator is equal to the denominator, +// Then the function should return false +test("should return false when numerator equals denominator", () => { + expect(isProperFraction(3, 3)).toEqual(false); +}); From ccd4e001a68db777524d627f1576a3bd11cd9920 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 21:08:14 +0000 Subject: [PATCH 08/12] Add comprehensive tests for getCardValue function to cover number cards, face cards, Aces, and invalid inputs --- .../3-get-card-value.test.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index 04418ff72..6b069ad35 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -8,6 +8,32 @@ test("should return 11 for Ace of Spades", () => { }); // Case 2: Handle Number Cards (2-10): +// When the card is a number between 2 and 10, +// Then the function should return the numeric value of the card +test("should return numeric value for number cards 2-10", () => { + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("10♦")).toEqual(10); +}); + // Case 3: Handle Face Cards (J, Q, K): +// When the card is J, Q, or K, +// Then the function should return 10 +test("should return 10 for face cards J, Q, K", () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("K♦")).toEqual(10); +}); + // Case 4: Handle Ace (A): +// When the card is an Ace, +// Then the function should return 11 +test("should return 11 for Ace cards", () => { + expect(getCardValue("A♥")).toEqual(11); +}); + // Case 5: Handle Invalid Cards: +// When the card has an invalid rank, +// Then the function should throw an error +test("should throw an error for invalid cards", () => { + expect(() => getCardValue("Z♠")).toThrow("Invalid card rank."); +}); From 9ddec99bfcf5944efa490192124ab3b680cf0e1d Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 15 Nov 2025 15:58:38 +0000 Subject: [PATCH 09/12] Refactor getCardValue function to streamline rank handling and improve test organization --- .../implement/3-get-card-value.js | 81 ++++++++----------- 1 file changed, 35 insertions(+), 46 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index 087e3b5e9..138071bb2 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -7,27 +7,33 @@ // 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) { - // Extract the rank (everything except the last character, which is the suit emoji) + // Extract rank (all characters except final suit emoji) const rank = card.slice(0, -1); - // Handle Ace - if (rank === "A") { - return 11; + // Mapping for face card values + const faceValues = { + J: 10, + Q: 10, + K: 10, + A: 11, + }; + + // If rank is a face card, return mapped value + if (faceValues[rank]) { + return faceValues[rank]; } - // Handle Face Cards (J, Q, K) - if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") { - return 10; - } + // Valid number ranks 2–10 only + const validNumberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; - // Handle Number Cards (2-9) - if (!isNaN(rank)) { + if (validNumberRanks.includes(rank)) { return Number(rank); } - // Handle Invalid Cards - throw new Error("Invalid card rank."); + // Anything else = invalid + throw new Error("Invalid card rank"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -44,45 +50,28 @@ function assertEquals(actualOutput, 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 -const aceofSpades = getCardValue("A♠"); -assertEquals(aceofSpades, 11); +// ---------------- TESTS ---------------- -// 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). -const fiveofHearts = getCardValue("5♥"); -assertEquals(fiveofHearts, 5); +// Handle Ace (A) +const aceOfSpades = getCardValue("A♠"); +assertEquals(aceOfSpades, 11); -const tenofClubs = getCardValue("10♣"); -assertEquals(tenofClubs, 10); +// Handle Number Cards (2-9) +const fiveOfHearts = getCardValue("5♥"); +assertEquals(fiveOfHearts, 5); -// Handle Face Cards (J, Q, K): -// Given a card with a rank of "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. -const jackOfDiamonds = getCardValue("J♦"); -assertEquals(jackOfDiamonds, 10); +// Handle 10 +const tenOfClubs = getCardValue("10♣"); +assertEquals(tenOfClubs, 10); -const queenOfSpades = getCardValue("Q♠"); -assertEquals(queenOfSpades, 10); +// Handle Face Cards (J, Q, K) +const queenOfDiamonds = getCardValue("Q♦"); +assertEquals(queenOfDiamonds, 10); -const kingOfHearts = getCardValue("K♥"); -assertEquals(kingOfHearts, 10); - -// 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." +// Handle invalid card try { - getCardValue("Z♠"); + getCardValue("15♣"); // invalid rank + console.assert(false, "Expected error for invalid rank"); } catch (e) { - assertEquals(e.message, "Invalid card rank."); + console.assert(true); } - -console.log("All tests ran! If no errors, all passed."); From 484ee5bccc8e4b54a21f9a1e13ae802bb084ffa1 Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 15 Nov 2025 16:05:46 +0000 Subject: [PATCH 10/12] Add tests for isProperFraction function and implement getCardValue function with rank handling --- .../2-is-proper-fraction.test.js | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index d194fbb78..a04ee45d9 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -1,3 +1,5 @@ +// Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js + // This statement loads the isProperFraction function you wrote in the implement directory. // We will use the same function, but write tests for it using Jest in this file. const isProperFraction = require("../implement/2-is-proper-fraction"); @@ -26,3 +28,67 @@ test("should return true for a negative proper fraction (|numerator| < denominat test("should return false when numerator equals denominator", () => { expect(isProperFraction(3, 3)).toEqual(false); }); + +// When the numerator is negative but |numerator| >= denominator, +// Then the function should return false +test("should return false for a negative improper fraction (|numerator| >= denominator)", () => { + expect(isProperFraction(-8, 7)).toEqual(false); +}); + +// When the numerator is zero, +// Then the function should return true +test("should return true when numerator is zero", () => { + expect(isProperFraction(0, 5)).toEqual(true); +}); + +// When the denominator is zero, +// Then the function should throw an error +test("should throw an error when denominator is zero", () => { + expect(() => { + isProperFraction(3, 0); + }).toThrow("Denominator cannot be zero"); +}); + +// When the numerator or denominator is not an integer, +// Then the function should throw an error +test("should throw an error when numerator or denominator is not an integer", () => { + expect(() => { + isProperFraction(2.5, 4); + }).toThrow("Numerator and denominator must be integers"); + + expect(() => { + isProperFraction(3, "5"); + }).toThrow("Numerator and denominator must be integers"); +}); + + +// Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +// This function takes a card string (e.g., "A♠", "10♥", "K♦") and returns its value in a game of cards. +function getCardValue(card) { + // Extract rank by removing suit symbol (last character) + const rank = card.slice(0, -1); + + // Map face cards to values + const faceValues = { + J: 10, + Q: 10, + K: 10, + A: 11, + }; + + // Return mapped value if rank is a face card + if (faceValues[rank]) { + return faceValues[rank]; + } + + // Convert to number and validate numeric cards (2–10) + const numericValue = Number(rank); + if (!isNaN(numericValue) && numericValue >= 2 && numericValue <= 10) { + return numericValue; + } + + // Invalid card + throw new Error("Invalid card rank"); +} + +module.exports = getCardValue; From b489194a864c5da94a0589c24bf4059b3ca1861e Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 15 Nov 2025 16:14:12 +0000 Subject: [PATCH 11/12] Refactor tests for getOrdinalNumber function to cover all ordinal cases and improve error handling --- .../2-practice-tdd/get-ordinal-number.test.js | 58 ++++++++++++++++--- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index dfe4b6091..ffc75860d 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,13 +1,55 @@ const getOrdinalNumber = require("./get-ordinal-number"); -// In this week's prep, we started implementing getOrdinalNumber -// continue testing and implementing getOrdinalNumber for additional cases -// Write your tests using Jest - remember to run your tests often for continual feedback +// Category 1: Numbers ending in 11, 12, 13 → always "th" +test("appends 'th' to numbers ending in 11, 12, or 13", () => { + const numbers = [11, 12, 13, 111, 1012, 1313]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); +}); + +// Category 2: Numbers ending in 1 → "st" (except those ending in 11) +test("appends 'st' to numbers ending in 1 except those ending in 11", () => { + const numbers = [1, 21, 101, 1001]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}st`); + }); +}); -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" +// Category 3: Numbers ending in 2 → "nd" (except those ending in 12) +test("appends 'nd' to numbers ending in 2 except those ending in 12", () => { + const numbers = [2, 22, 102, 202]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}nd`); + }); +}); + +// Category 4: Numbers ending in 3 → "rd" (except those ending in 13) +test("appends 'rd' to numbers ending in 3 except those ending in 13", () => { + const numbers = [3, 23, 103, 1003]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}rd`); + }); +}); + +// Category 5: All other numbers → "th" +test("appends 'th' to numbers that don't meet special suffix rules", () => { + const numbers = [4, 5, 6, 9, 20, 100, 1004, 1009]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); +}); + +// Error handling - Non-integers +test("throws an error when number is not an integer", () => { + const invalidInputs = [1.5, "3", null, undefined, {}, [], NaN]; + + invalidInputs.forEach(input => { + expect(() => getOrdinalNumber(input)).toThrow("Input must be an integer"); + }); +}); -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); +// Error handling - Negative values +test("throws an error when number is negative", () => { + expect(() => getOrdinalNumber(-1)).toThrow("Input must be a non-negative integer"); }); From 5c0c597961bb72d0f098ac6ae848a6320f0678db Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 15 Nov 2025 16:28:39 +0000 Subject: [PATCH 12/12] Revert "Refactor tests for getOrdinalNumber function to cover all ordinal cases and improve error handling" This reverts commit b489194a864c5da94a0589c24bf4059b3ca1861e. --- .../2-practice-tdd/get-ordinal-number.test.js | 58 +++---------------- 1 file changed, 8 insertions(+), 50 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index ffc75860d..dfe4b6091 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,55 +1,13 @@ const getOrdinalNumber = require("./get-ordinal-number"); +// In this week's prep, we started implementing getOrdinalNumber -// Category 1: Numbers ending in 11, 12, 13 → always "th" -test("appends 'th' to numbers ending in 11, 12, or 13", () => { - const numbers = [11, 12, 13, 111, 1012, 1313]; - numbers.forEach(num => { - expect(getOrdinalNumber(num)).toEqual(`${num}th`); - }); -}); - -// Category 2: Numbers ending in 1 → "st" (except those ending in 11) -test("appends 'st' to numbers ending in 1 except those ending in 11", () => { - const numbers = [1, 21, 101, 1001]; - numbers.forEach(num => { - expect(getOrdinalNumber(num)).toEqual(`${num}st`); - }); -}); +// continue testing and implementing getOrdinalNumber for additional cases +// Write your tests using Jest - remember to run your tests often for continual feedback -// Category 3: Numbers ending in 2 → "nd" (except those ending in 12) -test("appends 'nd' to numbers ending in 2 except those ending in 12", () => { - const numbers = [2, 22, 102, 202]; - numbers.forEach(num => { - expect(getOrdinalNumber(num)).toEqual(`${num}nd`); - }); -}); - -// Category 4: Numbers ending in 3 → "rd" (except those ending in 13) -test("appends 'rd' to numbers ending in 3 except those ending in 13", () => { - const numbers = [3, 23, 103, 1003]; - numbers.forEach(num => { - expect(getOrdinalNumber(num)).toEqual(`${num}rd`); - }); -}); - -// Category 5: All other numbers → "th" -test("appends 'th' to numbers that don't meet special suffix rules", () => { - const numbers = [4, 5, 6, 9, 20, 100, 1004, 1009]; - numbers.forEach(num => { - expect(getOrdinalNumber(num)).toEqual(`${num}th`); - }); -}); - -// Error handling - Non-integers -test("throws an error when number is not an integer", () => { - const invalidInputs = [1.5, "3", null, undefined, {}, [], NaN]; - - invalidInputs.forEach(input => { - expect(() => getOrdinalNumber(input)).toThrow("Input must be an integer"); - }); -}); +// Case 1: Identify the ordinal number for 1 +// When the number is 1, +// Then the function should return "1st" -// Error handling - Negative values -test("throws an error when number is negative", () => { - expect(() => getOrdinalNumber(-1)).toThrow("Input must be a non-negative integer"); +test("should return '1st' for 1", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); });