This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 475
london-10-shadi-fakhri-javascript-core-1-coursework-week1 #500
Open
Shadi38
wants to merge
7
commits into
CodeYourFuture:master
Choose a base branch
from
Shadi38:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
de80144
i solved mandatory practises
Shadi38 e35beb6
doing Extra practice
Shadi38 64fa1e3
working on practices
Shadi38 796a872
change some code in extra
Shadi38 c09afeb
i completed extra
Shadi38 c7fce4d
some changes
Shadi38 07b95ba
final extra
Shadi38 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| let total=price * 1.4; | ||
| return total; | ||
| } | ||
|
|
||
| /* | ||
| CURRENCY CONVERSION | ||
|
|
@@ -15,7 +18,10 @@ 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 total=parseFloat((((price/100)*99)*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. i think this is doing quite a lot on one line. Adding an extra line with a variable could make this more clear. |
||
| return total; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,32 @@ | ||
| /** | ||
|
|
||
| Let's peer into the future using a Magic 8 Ball! | ||
| https://en.wikipedia.org/wiki/Magic_8-Ball | ||
|
|
||
| There are a few steps to being able view the future though: | ||
| * Ask a question | ||
| * Shake the ball | ||
| * Get an answer | ||
| * Decide if it's positive or negative | ||
|
|
||
| The question can be anything, but the answers are fixed, | ||
| and have different levels of positivity or negativity. | ||
|
|
||
| Below are the possible answers: | ||
|
|
||
| ## Very positive | ||
| ## 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. | ||
|
|
@@ -43,53 +35,117 @@ | |
| Very doubtful. | ||
| */ | ||
|
|
||
|
|
||
| const veryPositive = [ | ||
| "It is certain.", | ||
| "It is decidedly so.", | ||
| "Without a doubt.", | ||
| "Yes - definitely.", | ||
| "You may rely on it.", | ||
| ]; | ||
|
|
||
| const positive = [ | ||
| "As I see it, yes.", | ||
| "Most likely.", | ||
| "Outlook good.", | ||
| "Yes.", | ||
| "Signs point to yes.", | ||
| ]; | ||
|
|
||
| const negative = [ | ||
| "Reply hazy, try again.", | ||
| "Ask again later.", | ||
| "Better not tell you now.", | ||
| "Cannot predict now.", | ||
| "Concentrate and ask again.", | ||
| ]; | ||
|
|
||
| const veryNegative = [ | ||
| "Don't count on it.", | ||
| "My reply is no.", | ||
| "My sources say no.", | ||
| "Outlook not so good.", | ||
| "Very doubtful.", | ||
| ]; | ||
|
|
||
| const answers = [ | ||
| "It is certain.", | ||
| "It is decidedly so.", | ||
| "Without a doubt.", | ||
| "Yes - definitely.", | ||
| "You may rely on it.", | ||
| "As I see it, yes.", | ||
| "Most likely.", | ||
| "Outlook good.", | ||
| "Yes.", | ||
| "Signs point to yes.", | ||
| "Reply hazy, try again.", | ||
| "Ask again later.", | ||
| "Better not tell you now.", | ||
| "Cannot predict now.", | ||
| "Concentrate and ask again.", | ||
| "Don't count on it.", | ||
| "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!"); | ||
| let finalAnswer = Math.floor(Math.random() * answers.length); | ||
| return answers[finalAnswer]; | ||
| }; | ||
|
|
||
| /* | ||
| This function should say whether the answer it is given is | ||
| - very positive | ||
| - positive | ||
| - negative | ||
| - very negative | ||
|
|
||
| This function should expect to be called with any value which was returned by the shakeBall function. | ||
| */ | ||
| function checkAnswer(answer) { | ||
|
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 job! Nice and clean:) |
||
| //Write your code in here | ||
| } | ||
| if (veryPositive.includes(answer)) { | ||
| return "very positive"; | ||
| } | ||
| else if(positive.includes(answer)) { | ||
| return "positive"; | ||
| } | ||
| else if (negative.includes(answer)) { | ||
| return "negative"; | ||
| } | ||
| else { | ||
| return "very negative"; | ||
| } | ||
| }; | ||
|
|
||
| /* | ||
| ================================== | ||
| ======= TESTS - DO NOT MODIFY ===== | ||
|
|
||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
| To run the tests for just this one file, type `npm test -- --testPathPattern 3-magic-8-ball` into your terminal | ||
| (Reminder: You must have run `npm install` one time before this will work!) | ||
| ================================== | ||
| */ | ||
| const { toBeOneOf } = require("jest-extended"); | ||
|
|
||
| test("whole magic 8 ball sequence", () => { | ||
| const consoleLogSpy = jest.spyOn(global.console, "log"); | ||
| const answer = shakeBall(); | ||
|
|
||
| expect(typeof answer).toEqual("string"); | ||
|
|
||
| expect(consoleLogSpy).toHaveBeenCalledTimes(1); | ||
| expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); | ||
|
|
||
| expect(checkAnswer(answer)).toBeOneOf([ | ||
| "very positive", | ||
| "positive", | ||
| "negative", | ||
| "very negative", | ||
| ]); | ||
| }); | ||
|
|
||
| test("magic 8 ball returns different values each time", () => { | ||
| const seenAnswers = new Set(); | ||
| for (let i = 0; i < 10; ++i) { | ||
|
|
@@ -100,19 +156,16 @@ test("magic 8 ball returns different values each time", () => { | |
| "Expected to get different random answers each time shakeBall was called, but always got the same one" | ||
| ); | ||
| } | ||
|
|
||
| let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer)); | ||
| if (seenPositivities.size < 2) { | ||
| throw Error( | ||
| "Expected to random answers with different positivities each time shakeBall was called, but always got the same one" | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test("checkAnswer works for `It is decidedly so.`", () => { | ||
| expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); | ||
| }); | ||
|
|
||
| test("checkAnswer works for `My reply is no.`", () => { | ||
| expect(checkAnswer("My reply is no.")).toEqual("very negative"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| // 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 "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. I like that you have used string interpolation here, nice! |
||
| } | ||
| function getTotal(a, b) { | ||
| total = a ++ b; | ||
| total = a + b; | ||
|
|
||
| return "The total is total"; | ||
| return `The total is ${total}`; | ||
| } | ||
|
|
||
| /* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,11 @@ | |
| Sales tax is 20% of the price of the product. | ||
| */ | ||
|
|
||
| function calculateSalesTax() {} | ||
| function calculateSalesTax(price) { | ||
| return price + (price/100)*20 ; | ||
|
|
||
|
|
||
| } | ||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
|
|
@@ -17,7 +21,10 @@ function calculateSalesTax() {} | |
| Remember that the prices must include the sales tax (hint: you already wrote a function for this!) | ||
| */ | ||
|
|
||
| function addTaxAndFormatCurrency() {} | ||
| function addTaxAndFormatCurrency(price) { | ||
| total=calculateSalesTax(price).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. Good use of .toFixed() |
||
| return "£"+total; | ||
| } | ||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I really like how clear you've made this .