This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 475
Glasgow Class 5 - Andrew Robertson - JavaScript Core 1 - Week 1 #135
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e19980f
A-setup-ide complete
Andy-Robertson 3df4d7a
B-hello-world complete
Andy-Robertson 1283389
C-variables complete
Andy-Robertson 482aea5
D-strings Complete
Andy-Robertson 0d8b20d
E-strings-concatenation complete
Andy-Robertson 02b3ed6
F-strings-methods complete
Andy-Robertson dc27fb3
G-numbers complete
Andy-Robertson 9db6e96
I-floats complete
Andy-Robertson 5e0a6a3
J-functions complete
Andy-Robertson 599fc79
K-functions-parameters complete
Andy-Robertson 6099521
L-functions-nested complete
Andy-Robertson ac748fb
Final polish before submission
Andy-Robertson da7e5ac
Updated const naming convention & improved comments
Andy-Robertson 1c05f87
Code Tidy
Andy-Robertson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| console.log("Hello world"); | ||
|
|
||
| console.log("Hello World. I just started learning JavaScript!"); | ||
|
|
||
| console.log(12); | ||
|
|
||
| console.log("12"); | ||
|
|
||
| console.log(Hello World. No Quotes!); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| // Start by creating a variable `greeting` | ||
| // Program to print a `greeting` variable in the console 3 times | ||
| // Tried to reduce code repetition by using a for loop | ||
|
|
||
| console.log(greeting); | ||
| let greeting = ". Hello world"; | ||
|
|
||
| for (let i = 1; i < 4; i++) { | ||
| console.log(i + greeting); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,28 @@ | ||
| // Start by creating a variable `message` | ||
| // Program that selects one of the three basic types of variables then outputs it along with its type. | ||
| // Hopefully its ok that I added some randomness to this exercise by highlighting the three main data types, | ||
| // If not or it causes jest to fail I will re-write, it was fun though and has a 3:1 chance of printing the expected result :) | ||
|
|
||
| console.log(message); | ||
| // Declare and initialise variables. | ||
| let messageChoice = 0; | ||
| let messageType; | ||
| let message = ""; | ||
|
|
||
| // Set `messageChoice` variable to a random number between 0 and 2 rounded to the nearest integer. | ||
| messageChoice = Math.round(Math.random() * 2); | ||
|
|
||
| // Determine which message to display using the random `messageChoice` value. | ||
| if (messageChoice == 0) { | ||
| message = "This is a string"; | ||
| } else if (messageChoice == 1) { | ||
| message = 25.5; | ||
| } else { | ||
| message = true; | ||
| } | ||
|
|
||
| // Sets `messageType` variable equal to the variable `message` type (string, number or boolean). | ||
| messageType = typeof message; | ||
|
|
||
| // Outputs the selected `message` and `messageType` | ||
| console.log(` | ||
| ${message} | ||
| ${messageType}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| // Start by creating a variable `message` | ||
| // Program which states my first name to practice concatenation. | ||
|
|
||
| console.log(message); | ||
| // Set and initialise variables and constants | ||
| const MY_NAME = "Andrew"; | ||
| let greetingStart = "Hello, my name is "; | ||
| let greeting = ""; | ||
|
|
||
| // Update and output `greeting` | ||
| greeting = greetingStart + MY_NAME; | ||
| console.log(greeting); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,19 @@ | ||
| // Start by creating a variable `message` | ||
| // Program that outputs a concatenated string stored in variable `message` containing constant `myName` | ||
| // and constant `nameLength` after calculating `myNames` character length using string method .length. | ||
| // I used constants here as `myName` will never change and consequently its length wont either, however | ||
| // the variable `message` concatenation may be updated so I declared it using let | ||
|
|
||
| // Declare and initialise variables and constants | ||
| const MY_NAME = "Andrew"; | ||
| const NAME_LENGTH = MY_NAME.length; | ||
| let message = ""; | ||
|
|
||
| // Update Message & output message | ||
| message = | ||
| "My name is " + | ||
| MY_NAME + | ||
| " and my name is " + | ||
| NAME_LENGTH + | ||
| " characters long"; | ||
|
|
||
| console.log(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| const name = " Daniel "; | ||
| // This program performs the same basic function as exercise.js | ||
| // however using the string method .trim() to remove the white space | ||
| // at both sides of the string " Daniel ". | ||
|
|
||
| // Declare and initialise variables and constants | ||
| const MY_NAME = " Daniel ".trim(); | ||
| const NAME_LENGTH = MY_NAME.length; | ||
| let message = ""; | ||
|
|
||
| // Update Message & output message | ||
| message = | ||
| "My name is " + | ||
| MY_NAME + | ||
| " and my name is " + | ||
| NAME_LENGTH + | ||
| " characters long"; | ||
|
|
||
| console.log(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,19 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| // Program that outputs the value of `numberOfStudents`, `numberOfMentors` | ||
| // and `totalStudentsAndMentors` after using template string literal syntax to concatenate | ||
| // the string stored in `message`. | ||
|
|
||
| // Declare and initialise variables | ||
| let numberOfStudents = 15; | ||
| let numberOfMentors = 8; | ||
| let totalStudentsAndMentors = 0; | ||
| let message = ""; | ||
|
|
||
| // Calculate total number of students | ||
| totalStudentsAndMentors = numberOfStudents + numberOfMentors; | ||
|
|
||
| // Concatenate string | ||
| message = `Number of students: ${numberOfStudents} | ||
| Number of mentors: ${numberOfMentors} | ||
| Total number of students and mentors: ${totalStudentsAndMentors}`; | ||
|
|
||
| console.log(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,29 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
| // Program to calculate the percentage of students and mentors in a group. | ||
|
|
||
| // Declare and initialise variables | ||
| let numberOfStudents = 15; | ||
| let numberOfMentors = 8; | ||
| let percentValue = 0; | ||
| let percentageStudents = 0; | ||
| let PercentageMentors = 0; | ||
| let result = ""; | ||
|
|
||
| // Set `percentValue` value equal to the actual percentage value for the group size as | ||
| // this reduces the required nesting and hopefully makes the code easier read. | ||
| percentValue = 100 / (numberOfStudents + numberOfMentors); | ||
|
|
||
| // Performed main calculations using Math.round for both students and mentors, | ||
| // storing the results in their respective variables. Alternatively I could have calculated | ||
| // only one of the percentages then subtracted it from 100 (which I think would have been more efficient) | ||
| // but felt the spirit of the exercise was to use Math.floor so did it this way. | ||
| percentageStudents = Math.round(percentValue * numberOfStudents); | ||
| PercentageMentors = Math.round(percentValue * numberOfMentors); | ||
|
|
||
| // String literal syntax is used to concatenated a `result` string | ||
| result = ` | ||
| Percentage students: ${percentageStudents}% | ||
| Percentage mentors: ${PercentageMentors}% | ||
| `; | ||
|
|
||
| // Output the result to the console | ||
| console.log(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| function halve(number) { | ||
| // complete the function here | ||
| // Function to half the input parameter `number` | ||
| return result = number / 2; | ||
| } | ||
|
|
||
| var result = halve(12); | ||
| // Testing with specified input which also produces an integer value output | ||
| console.log(halve(12)); | ||
|
|
||
| console.log(result); | ||
| // Testing with an input that produces a float value output | ||
| console.log(halve(1)); | ||
|
|
||
| // Testing 0 just to see what happens :) | ||
| console.log(halve(0)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| function triple(number) { | ||
| // complete function here | ||
| // Function to triple the input parameter `number` | ||
| return (result = number * 3); | ||
| } | ||
|
|
||
| var result = triple(12); | ||
| // Testing with specified integer input that should produces an integer value output | ||
| console.log(triple(12)); | ||
|
|
||
| console.log(result); | ||
| // Testing with a float input that should produce a float output | ||
| console.log(triple(0.3)); | ||
|
|
||
| // Testing with 0 for fun again :) | ||
| console.log(triple(0)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| // Complete the function so that it takes input parameters | ||
| function multiply() { | ||
| // Calculate the result of the function and return it | ||
| function multiply(numOne, numTwo) { | ||
| // Function to multiply two parameters together | ||
| return (result = numOne * numTwo); | ||
| } | ||
|
|
||
| // Assign the result of calling the function the variable `result` | ||
| var result = multiply(3, 4); | ||
| // Testing by calling the function with suggested parameters | ||
| console.log(multiply(3, 4)); | ||
|
|
||
| console.log(result); | ||
| // Testing by calling the function with two float parameters | ||
| console.log(multiply(3.5, 4.7)); | ||
|
|
||
| // Testing with calling the function with 0 as parameters | ||
| console.log(multiply(0, 0)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| // Declare your function first | ||
| function divide(numOne, numTwo) { | ||
| // Function to divide two parameters together | ||
| return (result = numOne / numTwo); | ||
| } | ||
|
|
||
| var result = divide(3, 4); | ||
| // Testing by calling the function with suggested parameters | ||
| console.log(divide(3, 4)); | ||
|
|
||
| console.log(result); | ||
| // Testing by calling the function with two float parameters | ||
| console.log(divide(3.5, 4.7)); | ||
|
|
||
| // Testing with calling the function with 0 as parameters, interesting | ||
| // unlike the previous multiply function which produced a 0, this | ||
| // produces a NaN... | ||
| console.log(divide(0, 0)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| // Write your function here | ||
| function greeting(myName) { | ||
| // Function takes myName parameter (string) and adds it to a greeting | ||
| return `Hello, my name is ${myName}`; | ||
| } | ||
|
|
||
| var greeting = createGreeting("Daniel"); | ||
|
|
||
| console.log(greeting); | ||
| console.log(greeting("Daniel")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| // Declare your function first | ||
| function add(numOne, numTwo) { | ||
| // Function to add two parameters together | ||
| let sum = numOne + numTwo; | ||
| return sum; | ||
| } | ||
|
|
||
| // Call the function and assign to a variable `sum` | ||
| // Testing by calling the function with suggested parameters | ||
| console.log(add(13, 124)); | ||
|
|
||
| console.log(sum); | ||
| // Testing by calling the function with two float parameters | ||
| console.log(add(3.5, 4.7)); | ||
|
|
||
| // Testing with calling the function with 0 as parameters | ||
| console.log(add(0, 0)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Declare your function here | ||
| function createLongGreeting(myName, myAge) { | ||
| // Function takes myName parameter (string) and adds it to a greeting | ||
| return `Hello, my name is ${myName} and I'm ${myAge} years old`; | ||
| } | ||
|
|
||
| const greeting = createLongGreeting("Daniel", 30); | ||
|
|
||
| console.log(greeting); | ||
| // Testing by calling the function with suggested parameters. | ||
| console.log(createLongGreeting("Daniel", 30)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| var mentor1 = "Daniel"; | ||
| var mentor2 = "Irina"; | ||
| var mentor3 = "Mimi"; | ||
| var mentor4 = "Rob"; | ||
| var mentor5 = "Yohannes"; | ||
| // I hope the way I have solved this exercise is ok although the readme seemed to have | ||
| // some wiggle room (hope jest does!) but happy to redo if needed as I enjoyed thinking | ||
| // of an alternative solution. As its the last exercise I wanted to try and be a bit creative | ||
| // so to solve this I have put the pre-defined mentor names into an array then iterated | ||
| // through it using a for loop which contains two nested ES6 arrow functions with omitted | ||
| // return and curly brackets as they are on one line. Had great fun figuring out how to | ||
| // solve it like this:) | ||
|
|
||
| let mentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"]; | ||
|
|
||
| for (let i = 0; i < mentors.length; i++) { | ||
| let mentor = mentors[i]; | ||
| const getNameInUpperCase = (mentor) => mentor.toUpperCase(); | ||
| const shoutyGreeting = (mentor) => `HELLO ${getNameInUpperCase(mentor)}`; | ||
| console.log(shoutyGreeting(mentor)); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why there is dot before hello world text ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback Ibrahim,
By using a for loop to create the three console log messages I was able to then use its count variable "i" to add a preceding number to each message and display,
So, the second character in the above messages is the dot before the hello world text :)