From 79deb3e304b91d365aebe032fc35b16bb6f6f7e3 Mon Sep 17 00:00:00 2001 From: Sam Pennington <56024559+sampennington@users.noreply.github.com> Date: Sat, 28 Sep 2024 22:56:37 +0100 Subject: [PATCH 1/2] Adds example soloutions --- Solouions/Sprint-1/count.js | 10 ++++++++++ Solouions/Sprint-1/decimal.js | 15 +++++++++++++++ Solouions/Sprint-1/initials.js | 8 ++++++++ Solouions/Sprint-1/paths.js | 24 ++++++++++++++++++++++++ Solouions/Sprint-1/random.js | 19 +++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 Solouions/Sprint-1/count.js create mode 100644 Solouions/Sprint-1/decimal.js create mode 100644 Solouions/Sprint-1/initials.js create mode 100644 Solouions/Sprint-1/paths.js create mode 100644 Solouions/Sprint-1/random.js diff --git a/Solouions/Sprint-1/count.js b/Solouions/Sprint-1/count.js new file mode 100644 index 000000000..c9253523d --- /dev/null +++ b/Solouions/Sprint-1/count.js @@ -0,0 +1,10 @@ +let count = 0; + +count = count + 1; + +// Line 1 is a variable declaration, creating the count variable with an initial value of 0 +// Line 3 is performing an assignment operation. The = operator is assigning a new value to the count variable. +// The new value is the result of the expression on the right side: count + 1. +// This operation is incrementing the count variable by 1, so after this line, count will be 1. + +console.log(`The value of count is now: ${count}`); diff --git a/Solouions/Sprint-1/decimal.js b/Solouions/Sprint-1/decimal.js new file mode 100644 index 000000000..84fd219a1 --- /dev/null +++ b/Solouions/Sprint-1/decimal.js @@ -0,0 +1,15 @@ +const num = 56.4567; + +// Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 +const wholeNumberPart = Math.floor(num); + +// Create a variable called decimalPart and assign to it an expression that evaluates to 0.4567 +const decimalPart = num - wholeNumberPart; + +// Create a variable called roundedNum and assign to it an expression that evaluates to 57 +const roundedNum = Math.round(num); + +// Log your variables to the console to check your answers +console.log(`Whole number part: ${wholeNumberPart}`); +console.log(`Decimal part: ${decimalPart}`); +console.log(`Rounded number: ${roundedNum}`); diff --git a/Solouions/Sprint-1/initials.js b/Solouions/Sprint-1/initials.js new file mode 100644 index 000000000..3f62a9bae --- /dev/null +++ b/Solouions/Sprint-1/initials.js @@ -0,0 +1,8 @@ +let firstName = "Creola"; +let middleName = "Katherine"; +let lastName = "Johnson"; + +// Declare a variable called initials that stores the first character of each string. +const initials = firstName[0] + middleName[0] + lastName[0]; + +console.log(`The initials are: ${initials}`); diff --git a/Solouions/Sprint-1/paths.js b/Solouions/Sprint-1/paths.js new file mode 100644 index 000000000..cf8ba529d --- /dev/null +++ b/Solouions/Sprint-1/paths.js @@ -0,0 +1,24 @@ +// 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 +const dir = filePath.slice(0, lastSlashIndex); + +// Create a variable to store the ext part of the variable +const ext = base.slice(base.lastIndexOf(".")); + +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); diff --git a/Solouions/Sprint-1/random.js b/Solouions/Sprint-1/random.js new file mode 100644 index 000000000..6913fc5fe --- /dev/null +++ b/Solouions/Sprint-1/random.js @@ -0,0 +1,19 @@ +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 + +// Breaking down the expression: +// 1. Math.random() generates a random number between 0 and 1 +// 2. We multiply it by (maximum - minimum + 1) to scale it to our range +// 3. Math.floor() rounds down to the nearest integer +// 4. We add minimum to shift the range to start from the minimum value + +// So num is a random integer between minimum and maximum values. + +console.log(`Num is a random number between ${minimum} and ${maximum}: ${num}`); From 30b201137c500dd7f58c52048bcd79020dd969d2 Mon Sep 17 00:00:00 2001 From: Sam Pennington <56024559+sampennington@users.noreply.github.com> Date: Sat, 28 Sep 2024 23:00:37 +0100 Subject: [PATCH 2/2] Add question back in --- Solouions/Sprint-1/count.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Solouions/Sprint-1/count.js b/Solouions/Sprint-1/count.js index c9253523d..78e908ff9 100644 --- a/Solouions/Sprint-1/count.js +++ b/Solouions/Sprint-1/count.js @@ -2,9 +2,10 @@ let count = 0; count = count + 1; -// Line 1 is a variable declaration, creating the count variable with an initial value of 0 -// Line 3 is performing an assignment operation. The = operator is assigning a new value to the count variable. +// Describe what line 3 is doing, in particular focus on what = is doing + +// Line 3 is performing an assignment, giving a new value to the count variable declared on line 1. // The new value is the result of the expression on the right side: count + 1. -// This operation is incrementing the count variable by 1, so after this line, count will be 1. +// So this operation is incrementing the count variable by 1, so after this line, count will now have the value 1. console.log(`The value of count is now: ${count}`);