-
-
Notifications
You must be signed in to change notification settings - Fork 475
Manchester NW5-LeilaFaez-Js-Core1-Week1 #371
base: master
Are you sure you want to change the base?
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. I just started learning JavaScript!" ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `greeting` | ||
|
|
||
| let greeting="Hi Leila" | ||
|
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. Good work not using There will be other areas in this PR where |
||
| 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` | ||
|
|
||
| let message="this is a string" | ||
| let messageType=typeof message; | ||
| console.log(message); | ||
| console.log(messageType); | ||
| // console.log(message + " "+ typeof message); | ||
|
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 works great. JavaScript does allow you to forget semicolons but we should always add them (line 2), no doubt this was an innocent mistake whilst learning :) I'd always suggest adding spaces between the variable and its assignment just for cleanliness / readability as well, like such (This feedback applies for the full PR): const message = "this is a string";
const messageType = typeof message;Finally one other suggestion would be to delete commented out code, it's a very easy thing to leave commented out code in your commits, I do it sometimes in my full time job as well. Here's a link to our style guide on commented out code to explain a little more: https://syllabus.codeyourfuture.io/guides/code-style-guide#dont-leave-lots-of-commented-out-code |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| console.log(message); | ||
| let greetingSentence ="Hi my name is"; | ||
| let name="Leila"; | ||
| let greeting =greetingSentence +" " + name; | ||
| console.log(greeting); |
| 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="Leila"; | ||
| let message=name.length; | ||
| console.log("My name is"+" "+name + " "+ "and my name is"+" " + message+" "+ "character 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. This passes and does exactly what we need it to do, great work Leila. We could tidy it up a little, if you notice you are using console.log("My name is " + name + " and my name is " + message + " characters long.");By adding the spaces in the original strings, we have less |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| const name = " Daniel "; | ||
|
|
||
| console.log(message); | ||
| const message=name.length; | ||
| console.log("My name is" + " " + name.trim() + " " + "and my name is" + " " + message + " " +"character 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. Great effort, I would apply my previous feedback here regarding the spaces, just so it's a little easier to read. I won't comment any other instances of this suggestion in the PR :) console.log("My name is " + name.trim() + " and my name is " + message + " character long."); |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| const numberOfStudents=10; | ||
| const numberOfMentors=7; | ||
| const sum=numberOfStudents+numberOfMentors; | ||
|
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. Perfect 🥇 As I've been advising other students, one thing which is completely optional but will greatly improve the readability of your code is to add parenthesis to comparisons and calculations. When it's just a simple sum like Your sum variable would become: const 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,8 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
| var total = numberOfMentors + numberOfStudents; | ||
| let studentPerc = Math.round((numberOfMentors / total) * 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. Well done and very good use of brackets / parenthesis to make the calculation more readable 💯 My only suggestion would be the variable names, whilst |
||
| let mentorPerc = Math.round((numberOfStudents / total) * 100); | ||
|
|
||
| console.log("Percent of student :" + " " + studentPerc + "%"); | ||
| console.log("Percent of mentor :" + " " + mentorPerc + "%"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function halve(number) { | ||
| // complete the function here | ||
| return number/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. Brilliant, this works great. Just another reminder that it's usually a good idea to use parenthesis when doing any sort of calculation (but it is optional). function halve(number) {
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,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` | ||
|
|
||
| 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 " + age + " years old"; | ||
| } | ||
| const greeting = createLongGreeting("Daniel", 30); | ||
|
|
||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,26 @@ | ||
|
|
||
| var greet="HELLO"; | ||
| var mentor1 = "Daniel"; | ||
| var mentor2 = "Irina"; | ||
| var mentor3 = "Mimi"; | ||
| var mentor4 = "Rob"; | ||
| var mentor5 = "Yohannes"; | ||
|
|
||
|
|
||
| function upperCase(name){ | ||
|
|
||
| return name.toUpperCase(); | ||
| } | ||
| function showName(greet,name){ | ||
| return greet +" "+ upperCase(name); | ||
| } | ||
| let result1=showName(greet,mentor1); | ||
| let result2 = showName(greet, mentor2); | ||
| let result3 = showName(greet, mentor3); | ||
| let result4 = showName(greet, mentor4); | ||
| let result5 = showName(greet, mentor5); | ||
| console.log(result1); | ||
| console.log(result2); | ||
| console.log(result3); | ||
| console.log(result4); | ||
| console.log(result5); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,10 @@ | |
| Write a function that converts a price to USD (exchange rate is 1.4 $ to £) | ||
| */ | ||
|
|
||
| function convertToUSD() {} | ||
| function convertToUSD(price) { | ||
| price *=1.4; | ||
|
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 good use of |
||
| return price; | ||
| } | ||
|
|
||
| /* | ||
| CURRENCY CONVERSION | ||
|
|
@@ -15,7 +18,11 @@ 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) { | ||
| price*=5.7; | ||
| total=(price-(price*1/100)).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. Again good use of multiplication assignment and good use of parenthesis on your total variable. We could probably use a |
||
| return +total; | ||
| } | ||
|
|
||
| /* ======= 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,29 @@ | |
| 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.toString()}` | ||
|
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. Great usage of backticks here Leila. This is the alternative to I would say backticks like you used here are much better 👍 |
||
| } | ||
|
|
||
| const startingValue = 2; | ||
|
|
||
| // Why can this code be seen as bad practice? Comment your answer. | ||
| let badCode = | ||
| let badCode = `£${(startingValue+10)*2}` | ||
|
|
||
|
|
||
| /* BETTER PRACTICE */ | ||
|
|
||
| let goodCode = | ||
| /* BETTER PRACTICE */ | ||
| const sum=startingValue+10; | ||
| const result=sum*2; | ||
| let goodCode =`£${result}`; | ||
|
|
||
| /* ======= 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 |
|---|---|---|
|
|
@@ -45,10 +45,40 @@ | |
|
|
||
| // This should log "The ball has shaken!" | ||
| // and return the answer. | ||
| let fixAnswer = [ | ||
| "It is certain.", //0 | ||
| "It is decidedly so.", //1 | ||
| "Without a doubt.", //2 | ||
| "Yes - definitely.", //3 | ||
| "You may rely on it.", //4-very positive | ||
| "As I see it, yes.", //5 | ||
| "Most likely.", //6 | ||
| "Outlook good.", //7 | ||
| "Yes.", //8 | ||
| "Signs point to yes.", //9-positive | ||
| "Reply hazy, try again.", //10 | ||
| "Ask again later.", //11 | ||
| "Better not tell you now.", //12 | ||
| "Cannot predict now.", //13 | ||
| "Concentrate and ask again.", //14-negetive | ||
| " Don't count on it.", //15 | ||
| "My reply is no.", //16 | ||
| "My sources say no.", //17 | ||
| "Outlook not so good.", //18 | ||
| " Very doubtful.", //19-very negetive | ||
| ]; | ||
|
|
||
|
|
||
| function shakeBall() { | ||
| //Write your code in here | ||
| console.log("The ball has shaken!"); | ||
| randomNumber = Math.floor(Math.random() * fixAnswer.length); | ||
| result =fixAnswer[randomNumber]; | ||
| return result; | ||
|
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 correct and you've done well Leila, good use of the math library as well 👍 I would just be careful of your indentation as well as use Another way you could return your value is just passing function shakeBall() {
console.log("The ball has shaken!");
const randomNumber = Math.floor(Math.random() * fixAnswer.length);
return fixAnswer[randomNumber];
} |
||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| This function should say whether the answer it is given is | ||
| - very positive | ||
|
|
@@ -58,10 +88,25 @@ 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 | ||
| } | ||
|
|
||
| function checkAnswer(answer) { | ||
| //Write your code in here | ||
| let ballPredict = fixAnswer.indexOf(answer); | ||
| if (ballPredict <= 4) { | ||
| answer = "very positive"; | ||
| } else if (ballPredict <= 9) { | ||
| answer = "positive"; | ||
| } else if (ballPredict <= 14) { | ||
| answer = "negative"; | ||
| } else if (ballPredict <= 19) { | ||
| answer = "very negative"; | ||
| } else answer = "Something broken"; | ||
|
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. Hi Leila, you've pretty much hit the nail on the head here 👍 I would just be careful at the end with your function checkAnswer(answer) {
const ballPredict = fixAnswer.indexOf(answer);
if (ballPredict <= 4) {
return "very positive";
} else if (ballPredict <= 9) {
return "positive";
} else if (ballPredict <= 14) {
return "negative";
} else if (ballPredict <= 19) {
return "very negative";
}
return "Something broken";
} |
||
|
|
||
| return answer; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| ================================== | ||
| ======= TESTS - DO NOT MODIFY ===== | ||
|
|
@@ -82,12 +127,16 @@ test("whole magic 8 ball sequence", () => { | |
| expect(consoleLogSpy).toHaveBeenCalledTimes(1); | ||
| expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); | ||
|
|
||
| expect(checkAnswer(answer)).toBeOneOf([ | ||
| "very positive", | ||
| // expect(checkAnswer(answer)).toBeOneOf([ | ||
| // "very positive", | ||
| // "positive", | ||
| // "negative", | ||
| // "very negative", | ||
| // ]); | ||
| expect(["very positive", | ||
| "positive", | ||
| "negative", | ||
| "very negative", | ||
| ]); | ||
| "very negative"]).toContain(checkAnswer(answer)) | ||
| }); | ||
|
|
||
| test("magic 8 ball returns different values each time", () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,18 @@ | ||
| // 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; | ||
|
|
||
| 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. You have solved the exercises and mandatory problems in a very neat, simple and understandable code and it's great. Well done.
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. Many thanks :) |
||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| // 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(); | ||
| } | ||
| word = " CodeYourFuture "; | ||
|
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 have successfully completed the exercise by fixing the errors 💯 We are however defining a |
||
| word = " CodeYourFuture teaches coding "; | ||
|
|
||
| function getStringLength(word) { | ||
| return "word".length(); | ||
|
|
||
| return word.length; | ||
| } | ||
| word = getStringLength("A wild sentence appeared!"); | ||
| word = getStringLength("Turtle"); ; | ||
|
|
||
| function multiply(a, b, c) { | ||
| a * b * c; | ||
| return; | ||
| const result= a * b * c; | ||
| return result; | ||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,24 @@ | ||
| // Add comments to explain what this function does. You're meant to use Google! | ||
|
|
||
| /* This function creat an integer random number(by multiplying decimal random number by ten)*/ | ||
| function getRandomNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
|
|
||
| /*This function concat two words which will be pass to the function as an argument | ||
| for example if word1="ali" and word="reza" the result of this function will be "alireza"*/ | ||
| 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. Hello Leila, you have done an amazing job and your codes are simple and easy to understand and review. However, I think for the return you could have used .concat to concatenate all the words together like on the above combine2Words function. If there is a reason for not using that do you mind explaining? Thank you
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. Thank you for your feedback. Yes you are completely right. I just wanted to try every piece of code that we have learnt during course :) |
||
| } | ||
| firstWord="code"; | ||
|
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've completed this mandatory exercise spot on Leila :) The only feedback would be that we don't need the |
||
| secondWord="your"; | ||
| thirdWord="future"; | ||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
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.
Hi Leila
You're very close with the answer, the last answer isn't quite correct but I definitely see your logic for this one, you can try to run
console.log(10);and see the result 👍