Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
let count = 0;
//let count = 0;

count = count + 1;
//count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// Line 3 returns the incremented value of count. The operator = assigns count +1 to the varibale count in the
// left.

// Described what line three is doing.

// Example from codewar (from programming workshop)

function drawStairs(n) {
let result = "";
if (n < 1) return result; // nothing to do

let iCount = 0;
while (iCount < n) {
// add spaces before the I (for all lines after the first)
let spaceCount = 0;
while (spaceCount < iCount) {
result = result.concat(" ");
spaceCount = spaceCount + 1;
}

// add the "I"
result = result.concat("I");

// add a newline after each line except the last one
if (iCount < n - 1) {
result = result.concat("\n");
}

// update the count
iCount = iCount + 1;
}

return result;
}

console.log(drawStairs(10));

/// Another example

function drawStairs(n) {
let result = [];

for (let i = 0; i < n; i++) {
result[i] = `${' '.repeat(i)}I`;
}

return result.join('\n');
}
console.log(drawStairs(5));
12 changes: 11 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ let lastName = "Johnson";
// Declare a variable called initials that stores the first character of each string.
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.

let initials = ``;
//let initials = firstName[0] + middleName[0] + lastName[0];

let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;

console.log(initials)

// https://www.google.com/search?q=get+first+character+of+string+mdn

// Declared a variable named initials

// Used template string to display initials Line 10



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

const dir = ;
const ext = ;
const dir = filePath.slice(lastSlashIndex+1);
const ext = base.slice(base.lastIndexOf(".") + 1);

console.log(dir)
// https://www.google.com/search?q=slice+mdn
15 changes: 15 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,18 @@ 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


// num represents the result of equation at the right hand side of the operator =

// Step-1 ---> Math.random() generates a random decimal,
// Step-2 ---> (maximum-minumum +1) is evaluated
// Step-3 ---> Math.random() is multiplied by (maximum-minumum +1)
// Step-4 ---> the resukt of Step-3 is round by the method Math.floor
// Step-5 ---> minimum is added to the result of Step-4

// Thus, num represents random numbers generated between minimum 1 and maximum 100. As we run the code several times it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thus, suggests to me this might've been generated by AI

Remember, the aim of this is to ensure you're understanding the exercises. It's hard to do this if you don't write the answers in your own words

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've decided Thus is not a good indicator of AI 👀

// generates new numbers.


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

// We have solved it by adding two forward slashes (//) at the beginning of each line.
// The computer does not run this line too.
12 changes: 11 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
//const age = 33;

// we need to replace "const" with "let", in javascript "const" is usd for variable
// that will nor change / will be kept constant troughout the code, where is "let"
// lets us to reassign variable later as we have done in the lines below the variabel age
// gets updated by one...

let age = 33;
age = age + 1;


console.log(age);
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

console.log(`I was born in ${cityOfBirth}`);
// The variable cityOfBirth was called before it was defined, so we need to bring that variable first
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);

// Problem has been fixed

13 changes: 12 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
//const last4Digits = cardNumber.slice(-4);

const last4Digits = cardNumber.toString().slice(-4);


console.log(last4Digits);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
// 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

// I think the code does not work because cardNumber variable is not stored as string.

// My guess was right.

// Therefore first we need to change CardNumber variable into string before we use the slice method.
14 changes: 13 additions & 1 deletion Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const 24hourClockTime = "08:53";

// Alternatively the above variable names can be corrected as follows:

// Option-1

const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";

// Option -2

const hour12ClockTime = "20:53";
const hour24ClockTime = "08:53";
14 changes: 13 additions & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

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

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -12,11 +12,23 @@ 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

// Ans: There are five (5) function calls, Line 4 Number() and replaceAll(), Line 5, Number() and replaceAll()
// Line 10 log().

// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?

// Ans: The error is coming from line 5 "("," ""));"" a comma was missing. Putting a comma solved the problem.

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

// Ans: Line 4 carPrice and Line 5 priceAfterOneyear are variable reassignments.

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

// Ans: 4 variable declaration Line 1, Line 2, Line 7 and Line 8

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

// Ans: The purpose of the expression Number(carPrice.replaceAll(",","")) is to change a string argument the contains commas passed to replaceAll()
// function into number, first the function replaceAll() removes the commas and then the function Number() changes it into number.
26 changes: 25 additions & 1 deletion Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = -3500; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -13,13 +13,37 @@ console.log(result);

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

// Ans: There are 6 variable declaration: Line 1, Line 3, Line 4, Line 6, Line 7, Line 9,

// b) How many function calls are there?

// Ans: There is one function call ... log()

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

// Ans: The expression movieLength % 60 returns the remainder after totalMinutes is divided by 60.

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

// The expression is calculating how many full minutes are in the movie’s total length by removing leftover seconds(less than 60)

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

// Ans: the variable result represent total movie lenght, Movie duration_hr_min_sec would be better name.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Javascript, the convention is to use "camel case". This would be perfect for a language like Python 🙂

Could you suggest one in camel case?

See this article: link


// In a "camel case" the following options would be better names
// 1. movieDurationHrMinSec
// 2. totalMovieSpan
// 3. totalMoviePeriod

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

// Ans: I have tried with different values and it seems to work for all values, but it needs to validate to avoid entering negative values
// I inserted --3500 and returned 0: -58: -20 which does not mean real time representation.


// Corrected typo reminder -----> remainder
// Suggested better names in "camel case" a convention in Javascript

// corrected name suggestion according to camelCase
11 changes: 11 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 2. const penceStringWithoutTrailingP = a string value "399" is reassigned to penceStringWithoutTrailingP after
// the the letter p is removed from "399p"
// 3. const paddedPenceNumberString = declared new variable, padStart ensures any argument passed is at least 3 digits long.
// 4. Line 9 a new variable pence is declared and its value comes from the padded string by removing the last two strings.
// eg. 399 removing 99 get 3 if the intial amount was 55 after padding it would be 055 and the pound result would be 0

// 5. Line 14 const pence = declared a variable and the variable is the result after extracts the last two digits
// from the pence string and pads it to 2 characters to have two digit pence string.

// 6. Line 18- console.log(`£${pounds}.${pence}`); returns money amount combining pound and pence alog with the symbol £.
// eg. £3.99