-
-
Notifications
You must be signed in to change notification settings - Fork 475
ZA2-Shafiek Davids- JavaScript-Core-1-Coursework-Week1 #304
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| console.log("Hello world"); | ||
| console.log("Hello World!"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `greeting` | ||
|
|
||
| var greeting = "Hello World!"; | ||
| console.log(greeting); | ||
| console.log(greeting); | ||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `message` | ||
| var message = "This is a string"; | ||
| var messageType = typeof message; | ||
|
|
||
| console.log(message); | ||
| console.log(messageType); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| console.log(message); | ||
| var message = "Hello, my name is"; | ||
| var name = " Shafiek"; | ||
| var greeting = message + name; | ||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| // Start by creating a variable `message` | ||
| var message = "My name is "; | ||
| var name = "Shafiek"; | ||
| var sentence = " and my name is "; | ||
| var nameLength = name.length; | ||
| var sentenceEnd = " characters long"; | ||
| var fullSentence = message + name + sentence + nameLength + sentenceEnd; | ||
|
|
||
| console.log(message); | ||
| console.log(fullSentence); | ||
|
|
||
| // var name = "Daniel"; | ||
|
|
||
| // var nameLowerCase = name.toLowerCase(); | ||
|
|
||
| // console.log(nameLowerCase); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| const name = " Daniel "; | ||
|
|
||
| let message = name.trim(); | ||
| console.log(message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| let numberOfStudents = 15; | ||
| console.log("Number of students: ", numberOfStudents); | ||
| let numberOfMentors = 8; | ||
| console.log("Number of mentors: ", numberOfMentors); | ||
|
|
||
| let sum = numberOfStudents + numberOfMentors; | ||
| console.log("Total number of students and mentors: ", sum); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,10 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
|
|
||
| let preciseStudentPercentage = (numberOfStudents / 23) * 100; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works as expected , well done. |
||
| roundedStudentPercentage = Math.round(preciseStudentPercentage); | ||
| let preciseMentorPercentage = (numberOfMentors / 23) * 100; | ||
| roundedMentorPercentage = Math.round(preciseMentorPercentage); | ||
|
|
||
| console.log("Percentage students: ", roundedStudentPercentage, "%"); | ||
| console.log("Percentage mentors: ", roundedMentorPercentage, "%"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| function halve(number) { | ||
| // complete the function here | ||
| return number / 2; | ||
| } | ||
|
|
||
| var result = halve(12); | ||
| var result = halve(55); | ||
|
|
||
| console.log(result); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| function triple(number) { | ||
| // complete function here | ||
| return number * 3; | ||
| } | ||
|
|
||
| var result = triple(12); | ||
| var result = triple(9); | ||
|
|
||
| console.log(result); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Declare your function first | ||
|
|
||
| function divide(num1, num2) { | ||
| return num1 / num2; | ||
| } | ||
| var result = divide(3, 4); | ||
|
|
||
| console.log(result); |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,9 @@ | ||||||||
| // Write your function here | ||||||||
|
|
||||||||
| function createGreeting(name) { | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| return name; | ||||||||
| } | ||||||||
|
|
||||||||
| var greeting = createGreeting("Daniel"); | ||||||||
|
|
||||||||
| console.log(greeting); | ||||||||
| console.log("Hello, my name is", greeting); | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| // Declare your function first | ||
| let num1 = 13; | ||
| let num2 = 124; | ||
|
|
||
| function number(num1, num2) { | ||
| return num1 + num2; | ||
| } | ||
| let sum = number(num1, num2); | ||
| // Call the function and assign to a variable `sum` | ||
|
|
||
| console.log(sum); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| // Declare your function here | ||
|
|
||
| const greeting = createLongGreeting("Daniel", 30); | ||
|
|
||
| console.log(greeting); | ||
| function createLongGreeting(name, age) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modifying the code from exercise3.js in this folder, can you modify createLongGreeting to return a greeting with both the name ( a string ) and an age ( a number ) ? |
||
| if (name === "" && age === number); | ||
| return name, age; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| var mentor1 = "Daniel"; | ||
| let mentor1 = "Daniel"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right!! 👍 |
||
| var mentor2 = "Irina"; | ||
| var mentor3 = "Mimi"; | ||
| var mentor4 = "Rob"; | ||
| var mentor5 = "Yohannes"; | ||
|
|
||
| // function mentor(name) { | ||
| // return (name = mentor1.toUpperCase()); | ||
| // } | ||
| // console.log(mentor1); | ||
| let name = mentor1.toUpperCase(); | ||
|
|
||
| console.log(name); | ||
|
|
||
| // let name = mentor2.toUpperCase(); | ||
| // console.log(name); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| //Explanation of this function-- | ||
| //Math.random will generate a random number between 0 and 1, so the function will generate a random number between 0 and 1 and then multiply it by 10 | ||
| function getRandomNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
| console.log(getRandomNumber()); | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| //Explanation of this function-- | ||
| //This function will concatenate 2 strings | ||
| function combine2Words(word1, word2) { | ||
| return word1.concat(word2); | ||
| } | ||
|
|
||
| function concatenate(firstWord, secondWord, thirdWord) { | ||
| // Write the body of this function to concatenate three words together. | ||
| // Look at the test case below to understand what this function is expected to return. | ||
| return firstWord.concat(" ", secondWord).concat(" ", thirdWord); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of chaining functions, well done |
||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,10 @@ | |
| Sales tax is 20% of the price of the product. | ||
| */ | ||
|
|
||
| function calculateSalesTax() {} | ||
| function calculateSalesTax(price) { | ||
| let taxincl = price * 1.2; | ||
| return taxincl; | ||
| } | ||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
|
|
@@ -17,7 +20,10 @@ function calculateSalesTax() {} | |
| Remember that the prices must include the sales tax (hint: you already wrote a function for this!) | ||
| */ | ||
|
|
||
| function addTaxAndFormatCurrency() {} | ||
| function addTaxAndFormatCurrency(currency) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the hint in the comment (hint: you already wrote a function for this!), can you think of another way to calculate the tax in the addTaxAndFormatCurrency function? Remember, functions are written to be reusable and to prevent us from rewriting logic |
||
| total = currency * 1.2; | ||
| return "£" + total.toFixed(2); | ||
| } | ||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
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.
Nice work Shafiek 🔥
Works as expected, but maybe not all the parts of the string needs to be seperate variables.
Using template literals will make the code less cluttered and achieve the same result.