-
-
Notifications
You must be signed in to change notification settings - Fork 475
Westmidlands-class 4-farzad-nazif- JavaScript-Core-1-Coursework-Week1 #332
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,3 @@ | ||
| console.log("Hello world"); | ||
| console.log("Hello World. I just started learning JavaScript!"); | ||
| console.log("I hope I will master JS someday"); | ||
| console.log(2); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `greeting` | ||
|
|
||
| let 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` | ||
|
|
||
| let message="This is a string" | ||
| let 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` | ||
|
|
||
| let name= "Daniel"; | ||
| let greetingMessage= "Hello, my name is " | ||
| let message= greetingMessage + name; | ||
| console.log(message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| console.log(message); | ||
| let name= "Daniel"; | ||
| let nameLength= name.length; | ||
| console.log("My name is Daniel and my name is " + nameLength +" characters long"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| const name = " Daniel "; | ||
|
|
||
| console.log(message); | ||
| let nameTrimmed = name.trim(); | ||
| let nameLength= name.length; | ||
| console.log("My name is "+nameTrimmed+" and my name is " + nameLength +" characters long"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| let numberOfStudents=15; | ||
| let numberOfMentors=8; | ||
| let total= numberOfMentors + numberOfStudents; | ||
| console.log("Total numnber of students and mentors: "+ total); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
| let total = numberOfMentors + numberOfStudents; | ||
| let poStudents = Math.round((15 / total) * 100); | ||
| let poMentors = Math.round((8 / total) * 100); | ||
| console.log(poStudents); | ||
| console.log(poMentors); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| function halve(number) { | ||
| // complete the function here | ||
| return number/2; | ||
| } | ||
|
|
||
| var result = halve(12); | ||
| var result2 = halve(24); | ||
| var result3 = halve(48); | ||
|
|
||
| console.log(result); | ||
| console.log(result2); | ||
| console.log(result3); |
| 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,5 +1,7 @@ | ||
| // Declare your function first | ||
|
|
||
| function divide (a,b){ | ||
| return a/b; | ||
| } | ||
| var result = divide(3, 4); | ||
|
|
||
| console.log(result); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Write your function here | ||
|
|
||
| function createGreeting (name){ | ||
| return "Hello, my name is " + name; | ||
| } | ||
| var greeting = createGreeting("Daniel"); | ||
|
|
||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Declare your function first | ||
|
|
||
| function add (a,b){ | ||
| return a+b; | ||
| } | ||
| // Call the function and assign to a variable `sum` | ||
|
|
||
| let sum = add(13,124); | ||
| console.log(sum); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Declare your function here | ||
|
|
||
| function createLongGreeting(name, age){ | ||
| return "Hello, my name is " + name + " and I'm 30 " + age + " years old"; | ||
| } | ||
| const greeting = createLongGreeting("Daniel", 30); | ||
|
|
||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,17 @@ | ||
| // There are syntax errors in this code - can you fix it to pass the tests? | ||
|
|
||
| function addNumbers(a b c) { | ||
| function addNumbers(a, b, c) { | ||
| return a + b + c; | ||
| } | ||
|
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. Correct, as you can see, while setting up parameters in a function, you need to separate them with commas. |
||
|
|
||
| function introduceMe(name, age) | ||
| return "Hello, my name is " + name "and I am " age + "years old"; | ||
| function introduceMe(name, age){ | ||
| return "Hello, my name is " + name +" and I am " + age + " years old"; | ||
| } | ||
|
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. Correct, when writing JavaScript functions, you need to use curly brackets to define the inside of the function, otherwise JavaScript won't understand that part and throw an error. In addition, as you did above, you need to put the |
||
|
|
||
| function getTotal(a, b) { | ||
| total = a ++ b; | ||
| total = a + b; | ||
|
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. Don't forget the let keyword while defining a variable! |
||
|
|
||
| return "The total is total"; | ||
| return "The total is " + total; | ||
| } | ||
|
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. Another nice way of concatenating a String with a variable. In addition, you can also use String interpolation to do this. |
||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| // The syntax for this function is valid but it has an error, find it and fix it. | ||
|
|
||
| function trimWord(word) { | ||
| return wordtrim(); | ||
| return word.trim(); | ||
| } | ||
|
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. There is no function called |
||
|
|
||
| function getStringLength(word) { | ||
| return "word".length(); | ||
| return word.length; | ||
| } | ||
|
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 is right, never put quotation marks around a variable, since JavaScript will identify it as a String instead of a variable. |
||
|
|
||
| function multiply(a, b, c) { | ||
| a * b * c; | ||
| return; | ||
| let total = a * b * c; | ||
| return total; | ||
| } | ||
|
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 way of using the let keyword to define a variable. |
||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,20 @@ | |
| function getRandomNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
| // Comment : math.random gives us a random number between 0 and 1. then we times it by 10, we will have a random number between 0 and 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. From my findings on Google, I could be wrong though, the getRandomNumber function will generate only floating numbers between 1 - 10
Author
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. math.random() gives you a number between 0 and 1 , if you times it by 10 , then gives you 0 to 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. "The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. " As you can see from the description taken from MDN Docs above, math.random() will randomly generate a number between 0 and 1. After that, it will multiply a number of your choice. |
||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| function combine2Words(word1, word2) { | ||
| return word1.concat(word2); | ||
| } | ||
| // Comment : This functions takes 2 parametrs and append word2 to word 1 (combine) and return a new string. | ||
|
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 correct, this function will concatenate 2 given Strings. |
||
|
|
||
| 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. | ||
| let wordCombination = firstWord.concat(secondWord); | ||
| let final = wordCombination.concat(thirdWord); | ||
| return final; | ||
| } | ||
|
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 correct. |
||
|
|
||
| /* | ||
|
|
@@ -25,13 +30,13 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 3-f | |
| */ | ||
|
|
||
| test("concatenate example #1", () => { | ||
| expect(concatenate("code", "your", "future")).toEqual("code your future"); | ||
| expect(concatenate("code", " your ", "future")).toEqual("code your future"); | ||
| }); | ||
|
|
||
| test("concatenate example #2", () => { | ||
| expect(concatenate("I", "like", "pizza")).toEqual("I like pizza"); | ||
| expect(concatenate("I", " like ", "pizza")).toEqual("I like pizza"); | ||
| }); | ||
|
|
||
| test("concatenate doesn't only accept strings", () => { | ||
| expect(concatenate("I", "am", 13)).toEqual("I am 13"); | ||
| expect(concatenate("I", " am ", 13)).toEqual("I am 13"); | ||
| }); | ||
| 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(productPrice) { | ||
| let total = productPrice + productPrice * 0.2; | ||
| return total; | ||
| } | ||
|
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 correct, I also like the way you calculated the total value in just 1 line, nice job. |
||
|
|
||
| /* | ||
| 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(productPrice) { | ||
| let total = calculateSalesTax(productPrice); | ||
| return "£" + total.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. This is also correct. In addition to this way, you can also use String interpolation to concatenate a String to a variable. |
||
|
|
||
| /* | ||
| =================================================== | ||
|
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. Had a great time going through your codes. it gave me alternate perspectives to solve a problem! Thumbs up Farzad!
Author
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. Thanks a lot Chioma |
||
|
|
||
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 job with the parseFloat(). I just got to know about it from reviewing your codes... Great job Farzad!