-
-
Notifications
You must be signed in to change notification settings - Fork 475
NW4 -Gulnihal Naldoken - JS -Week1 #125
Changes from all commits
52f4879
175a03d
5f77525
8d1ea85
f1895bb
8673b79
3f900f5
c80ac4e
007d3ad
a0acf7d
5836ca5
7d26fc7
a5c71b0
34a27b7
a47e595
f837225
d3ed366
10eea64
e6b82d1
9be7ce5
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(555); |
| 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,8 @@ | ||
| // Start by creating a variable `message` | ||
| // var message = "This is a string"; | ||
| // var messageType = typeof message; | ||
|
|
||
| console.log(message); | ||
| // console.log(messageType); | ||
|
|
||
| var message = "Terminal still scares me"; | ||
| console.log(typeof message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| // Start by creating a variable `message` | ||
| var message = "Hello, my name is "; | ||
| var firstName = "Gulnihal"; | ||
|
|
||
| console.log(message); | ||
| var greeting = message + firstName; | ||
|
|
||
|
|
||
| console.log(greeting); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| var firstName = "Gulnihal"; | ||
|
|
||
| var firstNameLength = firstName.length; | ||
|
|
||
|
|
||
| console.log(firstNameLength); | ||
|
|
||
| var message = "My name is Gulnihal and my name is 8 characters long" | ||
|
|
||
|
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. Here you could concatenate the |
||
| console.log(message); | ||
|
|
||
| var noSpaceMessage = message.trim; | ||
| console.log(noSpaceMessage); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| const name = " Daniel "; | ||
|
|
||
| console.log(message); | ||
| const name = " Gulnihal "; | ||
| var message = "My name is Gulnihal and my name is 8 characters long" | ||
| var noSpace = message.trim; | ||
| console.log(noSpace); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| var numberOfStudents =30; | ||
| var numberOfMentors = 16; | ||
| var totalNumberOfStudentsAndMentors = numberOfStudents + numberOfMentors; | ||
| console.log(totalNumberOfStudentsAndMentors); | ||
|
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 use string concatenation here to log it in this format |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,17 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
|
|
||
|
|
||
| // var preciseAge = 30.612437; | ||
| // var roughAge = Math.round(preciseAge); | ||
| // console.log(roughAge); | ||
|
|
||
| var proportionStudents = (numberOfStudents / (numberOfStudents + numberOfMentors)) *100; | ||
| console.log(proportionStudents); | ||
| var roughProportionStudents = Math.round(proportionStudents); | ||
| console.log(roughProportionStudents); | ||
|
|
||
| var proportionMentors = (numberOfMentors / (numberOfStudents + numberOfMentors)) *100; | ||
| console.log(proportionMentors); | ||
|
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. Instead of using |
||
| var roughProportionMentors = Math.round(proportionMentors); | ||
| console.log(roughProportionMentors); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| function add(num1, num2){ | ||
| return num1 + num2; | ||
| } | ||
| // Declare your function first | ||
|
|
||
| // Call the function and assign to a variable `sum` | ||
|
|
||
| var sum = add(13, 124); | ||
| console.log(sum); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| Write a function that converts a price to USD (exchange rate is 1.4 $ to £) | ||
| */ | ||
|
|
||
| function convertToUSD() {} | ||
| function convertToUSD(price) { | ||
| return price * 1.4; | ||
| } | ||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
|
|
@@ -15,7 +17,12 @@ function convertToUSD() {} | |
| They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL. | ||
| */ | ||
|
|
||
| function convertToBRL() {} | ||
| function convertToBRL(price) { | ||
| let BrlToP = parseFloat((price *(99/100) * 5.7).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. Because this variable won't change after being assigned it's better to use 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. Also to make it easier to know what the numbers are for, it is better to assign them to variables. So we could have This is just to help avoid 'magic numbers' https://levelup.gitconnected.com/magic-numbers-820d2d570cc5 |
||
|
|
||
| return BrlToP; | ||
|
|
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,26 +16,27 @@ | |
| the final result to the variable goodCode | ||
| */ | ||
|
|
||
| function add() { | ||
|
|
||
| function add(num1, num2) { | ||
| return num1 + num2; | ||
| } | ||
|
|
||
| function multiply() { | ||
|
|
||
| function multiply(num1, num2) { | ||
| return num1 * num2; | ||
| } | ||
|
|
||
| function format() { | ||
|
|
||
| function format(number) { | ||
| return "£" + number; | ||
| } | ||
|
|
||
| const startingValue = 2; | ||
|
|
||
| // Why can this code be seen as bad practice? Comment your answer. | ||
| let badCode = | ||
| // Why can this code be seen as bad practice? Comment your answer. | ||
| // bacause: lots of parantheses, and there are already mathematical symbols to do these operations. | ||
| let badCode = format(multiply((add(startingValue, 10)), 2)); | ||
|
|
||
| /* BETTER PRACTICE */ | ||
|
|
||
| let goodCode = | ||
| let goodCode = "£" + ((startingValue+10)*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. It's actually the other way round here, it's better practice to use the functions, as it gives less room for error. However, you could make it more readable, because as you said it looks quite crowded. You could assign the output of each function to a variable and then reference that, it'd make it easier to read. For example: |
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,40 +13,45 @@ | |
| and have different levels of positivity or negativity. | ||
|
|
||
| Below are the possible answers: | ||
|
|
||
| ## Very positive | ||
| It is certain. | ||
| It is decidedly so. | ||
| Without a doubt. | ||
| Yes - definitely. | ||
| You may rely on it. | ||
|
|
||
| ## Positive | ||
| As I see it, yes. | ||
| Most likely. | ||
| Outlook good. | ||
| Yes. | ||
| Signs point to yes. | ||
|
|
||
| ## Negative | ||
| Reply hazy, try again. | ||
| Ask again later. | ||
| Better not tell you now. | ||
| Cannot predict now. | ||
| Concentrate and ask again. | ||
|
|
||
| ## Very negative | ||
| Don't count on it. | ||
| My reply is no. | ||
| My sources say no. | ||
| Outlook not so good. | ||
| Very doubtful. | ||
| */ | ||
|
|
||
| // ## Very positive | ||
| let answers = [ | ||
| "It is certain.", | ||
| "It is decidedly so.", | ||
| "Without a doubt.", | ||
| "Yes - definitely.", | ||
| "You may rely on it.", | ||
|
|
||
| // ## Positive | ||
| "As I see it, yes.", | ||
| "Most likely.", | ||
| "Outlook good.", | ||
| "Yes.", | ||
| "Signs point to yes.", | ||
|
|
||
| // ## Negative | ||
| "Reply hazy, try again.", | ||
| "Ask again later.", | ||
| "Better not tell you now.", | ||
| "Cannot predict now.", | ||
| "Concentrate and ask again.", | ||
|
|
||
| // ## Very negative | ||
| "Don't count on it.", | ||
|
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 have 4 different array variables here, |
||
| "My reply is no.", | ||
| "My sources say no.", | ||
| "Outlook not so good.", | ||
| "Very doubtful." | ||
| ]; | ||
|
|
||
|
|
||
| // This should log "The ball has shaken!" | ||
| // and return the answer. | ||
| function shakeBall() { | ||
| //Write your code in here | ||
| console.log("The ball has shaken!") | ||
| var index = Math.floor(Math.random()*answers.length); | ||
| return answers[index]; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -59,7 +64,23 @@ function shakeBall() { | |
| This function should expect to be called with any value which was returned by the shakeBall function. | ||
| */ | ||
| function checkAnswer(answer) { | ||
| //Write your code in here | ||
| let index = answers.indexOf(answer); | ||
|
|
||
| return index <5 ? "very positive" | ||
| :index <10 ? "positive" | ||
| :index <15 ? "negative" | ||
| : "very negative"; | ||
|
|
||
|
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. Very nice use of the ternary operator here 🙂 |
||
| // if (index <5){ | ||
| // return "very positive"; | ||
| // } else if (index >= 5 && index <10) { | ||
| // return "positive"; | ||
| // } else if (index >= 10 && index <15) { | ||
| // return "negative"; | ||
| // } else { | ||
| // return "very negative"; | ||
| // } | ||
| //Write your code in here | ||
| } | ||
|
|
||
| /* | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| 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"; | ||
| } | ||
|
|
||
| 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. This also needs a |
||
| return "The total is total"; | ||
| return "The total is " + total; | ||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| function getRandomNumber() { | ||
| function getRandomNumber() { //gets random numbers | ||
| return Math.random() * 10; | ||
| } | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| function combine2Words(word1, word2) { | ||
| function combine2Words(word1, word2) { //combine two words without space between them | ||
| return word1.concat(word2); | ||
| } | ||
|
|
||
| function concatenate(firstWord, secondWord, thirdWord) { | ||
| return firstWord.concat(secondWord, thirdWord); | ||
| // Write the body of this function to concatenate three words together. | ||
|
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 can add spaces as separate strings here, for example |
||
| // Look at the test case below to understand what this function is expected to return. | ||
| } | ||
|
|
@@ -25,13 +26,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"); | ||
| }); | ||
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.
When leaving gaps between lines of code, it's best to go for no more than one line at a time to make things easier to read 🙂