-
-
Notifications
You must be signed in to change notification settings - Fork 475
NW Class 4 - Maha Malik - JS Core 1 - Week 1 #110
Changes from all commits
64c365e
ff2b393
5a8f803
58cb84a
594881b
0e328a4
10ad651
711193b
c87a9b1
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "cSpell.words": [ | ||
| "Sonjide", | ||
| "wordtrim" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| // Start by creating a variable `greeting` | ||
| var greeting = "Hello World!! :D" | ||
|
|
||
| console.log(greeting); | ||
| console.log(greeting); | ||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| // Start by creating a variable `message` | ||
| var message = "Message in a bottle"; | ||
| var messageType = typeof message; | ||
|
|
||
| console.log(message); | ||
| console.log(messageType); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| console.log(message); | ||
| var greetingStart = "Bonjour, je m'appelle "; | ||
| var myName = "Maha"; | ||
|
|
||
| var greeting = greetingStart + myName; | ||
|
|
||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| var message; | ||
| var myName = "Maha"; | ||
| var nameLength = myName.length; | ||
| message = | ||
| "My name is " + | ||
| myName + | ||
| " and my name is only " + | ||
| nameLength + | ||
| " characters long"; | ||
|
|
||
| console.log(message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| const name = " Daniel "; | ||
| const nameTrim = name.trim(); | ||
| const nameLength = nameTrim.length; | ||
|
|
||
| message = | ||
| "My name is " + nameTrim + " and my name is " + nameLength + " characters long"; | ||
|
|
||
| console.log(message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
|
|
||
| const numberOfStudents = 12; | ||
| const numberOfMentors = 9; | ||
|
|
||
| const total = numberOfStudents + numberOfMentors; | ||
|
|
||
| console.log(total); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,13 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
|
|
||
| var totalNumber = numberOfMentors + numberOfStudents; | ||
|
|
||
| var studentPercentage = numberOfStudents / totalNumber * 100; | ||
| var mentorsPercentage = numberOfMentors / totalNumber * 100; | ||
|
|
||
| var roundedStudentPercentage = Math.round(studentPercentage); | ||
| var roundedMentorsPercentage = Math.round(mentorsPercentage); | ||
|
|
||
| console.log("Percentage student: " + roundedStudentPercentage + "%"); | ||
| console.log("Percentage mentors: " + roundedMentorsPercentage + "%"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function halve(number) { | ||
| // complete the function here | ||
| return number/2; | ||
| } | ||
|
|
||
| var result = halve(12); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function triple(number) { | ||
| // complete function here | ||
| return number*3; | ||
| } | ||
|
|
||
| var result = triple(12); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| // Declare your function first | ||
| function divide(a, b) { | ||
| return a / b; | ||
| } | ||
|
|
||
| var result = divide(3, 4); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| // Declare your function first | ||
| function addNum(a, b) { | ||
| return a + b; | ||
| } | ||
|
|
||
| // Call the function and assign to a variable `sum` | ||
| var sum = addNum(13, 124); | ||
|
|
||
| console.log(sum); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,19 @@ | ||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| //My Comment - This function gives a random number by using math.random() and multiplying the value by 10 | ||
|
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. Can you explain the range of the random numbers that will be returned? What range does Math.random() return, and so what will your method return? |
||
| function getRandomNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
|
|
||
| //My comment - The function combines two words using the concat method. | ||
| 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} ${secondWord} ${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. can you do this using the |
||
| } | ||
| /*Can also be written this way: | ||
| return firstWord + " " + secondWord + " " + thirdWord; */ | ||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| Sales tax is 20% of the price of the product. | ||
| */ | ||
|
|
||
| function calculateSalesTax() {} | ||
| function calculateSalesTax(num) { | ||
| return num *1.2; //Adds 20% to number and gives total price including tax | ||
|
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. You could also declare 1.2 as a variable called priceWithTax or something similar, or even declare 0.2 as a variable call tax, and then multiply by the price and then add to the price. This would would make it easier to understand what your code is doing. However, your solution has the advantage of being concise. Both approaches work, so it's a matter of taste. |
||
| } | ||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
|
|
@@ -15,9 +17,18 @@ function calculateSalesTax() {} | |
| Write a function that adds tax to a number, and then transforms the total into the format £0.00 | ||
|
|
||
| Remember that the prices must include the sales tax (hint: you already wrote a function for this!) | ||
|
|
||
| */ | ||
|
|
||
| function addTaxAndFormatCurrency() {} | ||
| function addTaxAndFormatCurrency(num) { | ||
| return `£${calculateSalesTax(num).toFixed(2)}`; | ||
| } | ||
|
|
||
| //CAN ALSO WRITE THIS WAY: | ||
| // function addTaxAndFormatCurrency(num) { | ||
| // var totalPrice= `£${calculateSalesTax(num).toFixed(2)}`; | ||
|
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. Yes, as discussed in the previous comment, the first is more concise, but sometimes breaking your code down into stages and declaring variables with good names helps to make your code more understandable. It's up to you which you prefer. |
||
| // return totalPrice; | ||
| // } | ||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
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.
the idea of this exercise was to have 2 separate functions, one that returns the name in uppercase and another that returns the greeting by calling the first function to get the name