Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
c47bf5b
describe line 3 in Sprint-1/1-key-exercises/1-count.js
sul-devs Feb 16, 2025
acc10b1
get initials from names
sul-devs Feb 16, 2025
9352dfc
access directory, base, and extension of a file path.
sul-devs Feb 16, 2025
287b286
explain what 'num' represents.
sul-devs Feb 16, 2025
e7c5722
0 - insert comments
sul-devs Feb 16, 2025
66ae0d6
explain reassignment to const error.
sul-devs Feb 16, 2025
9723d03
const/let variables cannot be used before they're declared.
sul-devs Feb 16, 2025
4355afc
fix: slice is not a function error.
sul-devs Feb 16, 2025
cf304d7
variables names can't start with a number.
sul-devs Feb 16, 2025
465e60e
small changes.
sul-devs Feb 16, 2025
27ed8db
missing a comma
sul-devs Feb 16, 2025
6a8d65b
answer question A
sul-devs Feb 16, 2025
f733533
answer question B
sul-devs Feb 16, 2025
4a4c5ed
answer question C
sul-devs Feb 16, 2025
0c37ae7
answer question D
sul-devs Feb 16, 2025
6846ad4
answer question E
sul-devs Feb 16, 2025
8245022
2 - answer question A
sul-devs Feb 16, 2025
052a796
2 - answer question B
sul-devs Feb 16, 2025
24e08b9
2 - answer question C
sul-devs Feb 16, 2025
088fe11
2 - answer question D
sul-devs Feb 16, 2025
29eb448
2 - answer question E
sul-devs Feb 16, 2025
6cf79cd
2 - answer question F
sul-devs Feb 16, 2025
e4cee03
complete the 3rd exercise.
sul-devs Feb 16, 2025
8e53aa2
Merge branch 'exercise-3' into coursework/sprint-1
sul-devs Feb 19, 2025
de5c0e7
1-key-errors: write prediction of the program.
sul-devs Feb 19, 2025
1c84fed
1-key-errors: call function.
sul-devs Feb 19, 2025
e017cac
1-key-errors: explain why error occurs.
sul-devs Feb 19, 2025
9de938b
1-key-errors: move explanation to the right line.
sul-devs Feb 19, 2025
e4e77b0
1-key-errors: fix code.
sul-devs Feb 19, 2025
46f8ef1
1-key-errors: complete
sul-devs Feb 19, 2025
6e730ab
1-key-errors/1.js: write prediction.
sul-devs Feb 19, 2025
8e6571d
1-key-errors/1.js: write explanation for cause of the error.
sul-devs Feb 19, 2025
4f78e38
write my solution.
sul-devs Feb 19, 2025
f6e8c1b
1-key-errors/2.js: write prediction.
sul-devs Feb 19, 2025
e1f7d30
1-key-errors/2.js: move prediction to the right line.
sul-devs Feb 19, 2025
8ed5945
1-key-errors/2.js: write error message.
sul-devs Feb 19, 2025
8eaf787
1-key-errors/2.js: explain error message.
sul-devs Feb 20, 2025
99bd728
1-key-errors/2.js: fix code.
sul-devs Feb 20, 2025
430d7dc
1-key-errors/2.js: add answer comment to each answer.
sul-devs Feb 20, 2025
055257a
1-key-errors/2.js: complete exercise 2
sul-devs Feb 20, 2025
8da9cc9
2-mandatory-debug/0.js: write prediction.
sul-devs Feb 20, 2025
aa985d1
2-mandatory-debug/0.js: write explanation of program.
sul-devs Feb 20, 2025
bb702a8
2-mandatory-debug/0.js: adjust explanation of error program.
sul-devs Feb 20, 2025
d88c4c5
2-mandatory-debug/0.js: fix code - remove console.log() statement in …
sul-devs Feb 20, 2025
5f23e56
2-mandatory-debug/0.js: complete
sul-devs Feb 20, 2025
137bfd4
2-mandatory-debug/1.js: write prediction of program.
sul-devs Feb 20, 2025
5872998
2-mandatory-debug/1.js: write explanation of problem in the program.
sul-devs Feb 20, 2025
af48a45
2-mandatory-debug/1.js: fix code.
sul-devs Feb 20, 2025
56d9eed
2-mandatory-debug/1.js: complete
sul-devs Feb 20, 2025
8a86813
2-mandatory-debug/2.js: write prediction of the program
sul-devs Feb 20, 2025
9b4abe8
2-mandatory-debug/2.js: compare prediction to outcome.
sul-devs Feb 20, 2025
edba795
2-mandatory-debug/2.js: explain why the output is the way it is.
sul-devs Feb 20, 2025
6c47d6b
2-mandatory-debug/2.js: fix the program.
sul-devs Feb 20, 2025
d52bdaf
2-mandatory-debug/2.js: complete
sul-devs Feb 20, 2025
0e30202
3-mandatory-implement/1-bmi.js: write solution.
sul-devs Feb 20, 2025
970eb92
3-mandatory-implement/2-cases.js: implement solution.
sul-devs Feb 20, 2025
155fa75
3-mandatory-implement/3-to-pounds.js: write solution.
sul-devs Feb 20, 2025
c9fbb0b
4-mandatory-interpret/time-format.js: answer first question.
sul-devs Feb 20, 2025
3ee1bd5
4-mandatory-interpret/time-format.js: answer second question.
sul-devs Feb 20, 2025
08915fe
4-mandatory-interpret/time-format.js: answer third question.
sul-devs Feb 20, 2025
fbffd08
4-mandatory-interpret/time-format.js: answer fourth question.
sul-devs Feb 20, 2025
7994a7f
4-mandatory-interpret/time-format.js: answer last question.
sul-devs Feb 20, 2025
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
2 changes: 2 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,5 @@ 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

// Explanation: In Line 3, the '=' is reassigning the 'count' variable to the current value of 'count' plus 1 i.e. incrementing 'count' by 1.
13 changes: 11 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,16 @@ 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

// MY SOLUTION START //

// Get first character of each name, store it in a string, and remove spaces with trim().
let initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(
0
)}`.trim();

// View output to check if 'initals' variable matches the expected output 'CKJ'.
console.log(initials);

// MY SOLUTION END //
15 changes: 8 additions & 7 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const indexOfDot = filePath.indexOf("."); // Get index of '.'
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
const dir = filePath.slice(0, lastSlashIndex); // 1. Create a variable to store the dir part of the filePath variable
const ext = filePath.slice(indexOfDot); // 2. Create a variable to store the ext part of the variable

// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable
// Output
console.log(`1. Dir - "${dir}"`);
console.log(`2. Ext - "${ext}"`);
console.log(`3. The base part of "${filePath}" is "${base}"`);

const dir = ;
const ext = ;

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

// DOCUMENTATION
// 'num' - generates a random number within range between 'minimum' and 'maximum'.
// Math.floor() - rounds any number down to the nearest whole number.
// Math.random() - generates a random number between 0 and 0.9 recurring (never 1).
// '* (maximum - minimum + 1)' - Calculates range. '+ 1' means generated number can actually be <= range instead of just < range.
// '+ minimum' - totals the minimum and maximum to get the total range.

// Output
console.log(num);
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?
2 changes: 2 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

const age = 33;
age = age + 1;

// You cannot reassign a constant variable. To create a variable that needs to be reassigned, the 'let' keyword should be used instead of 'const'.
2 changes: 2 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

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

// 'let' and 'const' variables can only be used after their declaration
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cardNumber = 4533787178994213;
const cardNumber = [4, 5, 3, 3, 7, 8, 7, 1, 7, 8, 9, 9, 4, 2, 1, 3];
const last4Digits = cardNumber.slice(-4);

// The last4Digits variable should store the last 4 digits of cardNumber
Expand All @@ -7,3 +7,10 @@ const last4Digits = cardNumber.slice(-4);
// Then run the code and see what error it gives.
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
// Then try updating the expression last4Digits is assigned to, in order to get the correct value

// Prediction: slice takes an index or index range, and I don't think you can have negative indexes, so that will be the error.
// Outcome: says '.slice' is not a function
// Fix: slice is an array method not a string method. convert card number into an array.

// Output
console.log(last4Digits);
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// Variable names can't start with a number.
const hour24ClockTime = "20:53";
const hour12ClockTime = "08:53";
12 changes: 11 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(",", "")); // missing a comma

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -13,10 +13,20 @@ console.log(`The percentage change is ${percentageChange}`);

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

// 4 function calls, in lines 4, 5, 8, and 10.

// 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?

// line 5 two values are passed into the replaceAll method, but aren't comma separated. Fixed error, by separating values with a comma.

// c) Identify all the lines that are variable reassignment statements

// In lines 4 and 5, 'priceDifference' and 'priceAfterOneYear' are reassigned.

// d) Identify all the lines that are variable declarations

// 1, 2, 7, 8 are all variable declarations, and 4 and 5 are variable redeclarations.

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

// Type conversion: converts values from string to number, in order to be used in calculations later on in the program.
12 changes: 12 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ console.log(result);

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

// Answer: 6

// b) How many function calls are there?

// Answer: 1 - console.log(result)

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// Answer: It sees how many whole minutes go into the movieLength, and tells you what was left over.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// Answer: It gets the total length of the movie in seconds, divides it by 60 to get the total length of the movie in minutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?

// Answer: It represents the current time in the movie, so maybe 'timestamp' could be a better name.

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

// Answer: Negative values don't make sense. Long decimals aren't rounded. But positive integers work.
7 changes: 6 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// 0399
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
Expand All @@ -25,3 +25,8 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 2. penceStringWithoutTrailingP: separates the number from the 'p', so it's now "399"
// 3. paddedPenceNumberString: adds a 0 to the start, so it's "0399"
// 4. pounds: extracts the first 2 characters to get the pounds, so it's "03"
// 5. pence: gets all the characters after 2nd character, so it's "99". The 'padEnd(2)' makes sure there is always two digits for the pence part
// 6. 'console.log(`£${pounds}.${pence}`): logs the amount in pounds and pence, with a dot separating the two.
14 changes: 8 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Predict and explain first...
// =============> write your prediction here
// Capitalise 1st letter of a string.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
capitalise("String input.");

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
// interpret the error message and figure out why an error is occurring

// =============> write your explanation here
// The error occurs because you cannot declare a variable with a name that exists in the same scope.

// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
15 changes: 8 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// Undefined, decimalNumber is not in the right scope for the console.log statement to access it.

// Try playing computer with the example to work out what is going on

// =============> write your explanation here
// Declaring variable with the same name in the same scope causes error.

// Finally, correct the code to fix the problem
// =============> write your new code here
const decimalNumber = 0.5;
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
console.log(convertToPercentage(decimalNumber));
14 changes: 8 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@

// 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;
}
// Answer: Cant set a value as a parameter (error)

// =============> write the error message here
// Answer: SyntaxError: Unexpected number. Identifier expected.

// =============> explain this error message here
// Answer: The error message means a number is being used in a place where it is syntactically invalid.

// Finally, correct the code to fix the problem

// =============> write your new code here
// Answer:
function square(num) {
return num * num;
}


console.log(square(3));
13 changes: 7 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// 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)}`);
// Answer: logs the product of 2 numbers to the console.

// =============> write your explanation here
// Answer: multiples 2 numbers, and logs them to the console. However the function doesn't include a return statement, so the output of the function will default to undefined.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
14 changes: 8 additions & 6 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Predict and explain first...
// =============> write your prediction here
// Answer: function returns nothing (undefined), because it is on a different line from the return statement and isn't wrapped in parentheses ().

// =============> write your explanation here
// Answer: function calls of sum() return undefined as the statement is on a separate line from the return statement without braces ().

// Finally, correct the code to fix the problem
// =============> write your new code here
// Answer:
function sum(a, b) {
return;
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
26 changes: 15 additions & 11 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

// Predict the output of the following code:
// =============> Write your prediction here
// Answer: Error because parameters aren't defined, but arguments are passed in.

// Now run the code and compare the output to your prediction
// =============> write the output here
// Answer: It just gets the last digit of the variable 'num'.

// Explain why the output is the way it is
// =============> write your explanation here
// Answer: Parameters aren't defined, so arguments being passed in arent used, so it just uses the 'num' variable referenced within the function as opposed to the arguments passed in.

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// Finally, correct the code to fix the problem
// =============> write your new code here
// Answer:
const num = 103;

function getLastDigit() {
function getLastDigit(num) {
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
7 changes: 5 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// 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
}
// return the BMI of someone based off their weight and height
return Number((weight / (height * 2)).toFixed(1));
}

console.log(calculateBMI(85, 1.81));
6 changes: 6 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
// 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
// Answer:
function upperSnakeCase(string) {
return string.toUpperCase().split(" ").join("_");
}

console.log(upperSnakeCase("I am a string"));
19 changes: 19 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@
// 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
function toPounds(penceString) {
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");

return `£${pounds}.${pence}`;
}
console.log(toPounds("98765532626p"));
Loading