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 476
London Class 7 - Kara Howard - JavaScript Core 1 - Week 1 #28
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9aac196
Completed Ex b hello world
kmh256 d524e42
Completed Ex c Variables
kmh256 21bb8c8
Updated Ex c
kmh256 70903a5
Completed Ex d Strings
kmh256 3567985
Completed Ex e Concatenation
kmh256 a07a5f4
Completed Ex f String Methods
kmh256 8ed0f2f
Completed Ex g Numbers
kmh256 0bb67e1
Completed Ex i Floats
kmh256 f17533b
Completed Ex j Functions
kmh256 661c538
Completed Ex k Functions-parameters
kmh256 9c974d7
Completed Ex l Functions-nested
kmh256 fe69129
Completed Mandatory Ex 1 Syntax
kmh256 96de503
Completed Mandatory Ex 2 Logic
kmh256 8c4d5c3
Completed Mandatory Ex 3 Function
kmh256 31f3c9a
Completed Mandatory Ex 4 Tax
kmh256 a9156ee
Completed Extra 1 Currency
kmh256 64583d6
Completed Extra 2 piping
kmh256 58ef563
Completed Extra Magic 8 Ball
kmh256 4a1825a
Updated Magic 8 Ball
kmh256 63646c5
Improved Magic 8 Ball Solution
kmh256 dc6d622
Added comments to Magic 8 Ball
kmh256 545d6be
Corrected an error in answerSelection
kmh256 094e4ae
Updated comments for Magic 8 Ball
kmh256 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
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 +1,15 @@ | ||
| console.log("Hello world"); | ||
|
|
||
| //* Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'. | ||
| console.log('Hello World. I just started learning JavaScript!'); | ||
|
|
||
| // * Try to console.log() several things at once. | ||
| let greeting = "Hello"; | ||
| let firstName = "Kara"; | ||
| console.log(`${greeting} ${firstName}`); | ||
|
|
||
| //* What happens when you get rid of the quote marks? | ||
| //console.log(Hello, World!); SyntaxError: missing ) after argument list | ||
|
|
||
| //* What happens when you console.log() just a number without quotes? | ||
| console.log(12345); |
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,3 +1,6 @@ | ||
| // Start by creating a variable `greeting` | ||
|
|
||
| console.log(greeting); | ||
| var greeting = "Hello world"; | ||
| //* Print your `greeting` to the console 3 times | ||
| for (i = 0; i < 3; i++){ | ||
| console.log(greeting); | ||
| } |
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,3 +1,5 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| //* Write a program that logs a message and its type | ||
| let message = "This is a string"; | ||
| console.log(message); | ||
| console.log(typeof message); |
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,3 +1,6 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| //* Write a program that logs a message with a greeting and your name | ||
| let greetingStart = "Hello, my name is "; | ||
| let myName = "Kara"; | ||
| let message = greetingStart + myName; | ||
| console.log(message); |
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,3 +1,5 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| //* Log a message that includes the length of your name | ||
| let myName = "Kara"; | ||
| let message = `My name is ${myName} and my name is ${myName.length} characters long`; | ||
| console.log(message); |
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,3 +1,6 @@ | ||
| const name = " Daniel "; | ||
| //* Log the same message using the variable, `name` provided | ||
| //* Use the `.trim` method to remove the extra whitespace | ||
|
|
||
| console.log(message); | ||
| const name = " Daniel "; | ||
| let message = `My name is ${name.trim()} and my name is ${name.length} characters long`; | ||
| console.log(message); |
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 +1,7 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| //* Create two variables `numberOfStudents` and `numberOfMentors` | ||
| //* Log a message that displays the total number of students and mentors | ||
| let numberOfStudents = 15; | ||
| let numberOfMentors = 8; | ||
| let total = numberOfStudents + numberOfMentors | ||
| console.log(`Number of Students: ${numberOfStudents}\nNumber of Mentors: ${numberOfMentors}\nTotal number of students and mentors: ${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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,9 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
|
|
||
| //* Using the variables provided in the exercise calculate the percentage of mentors and students in the group | ||
| let total = numberOfStudents + numberOfMentors; | ||
| let percentageStudents = Math.round((numberOfStudents / total) * 100); | ||
| let percentageMentors = Math.round((numberOfMentors / total) * 100); | ||
| console.log(`Percentage students: ${percentageStudents}%`); | ||
| console.log(`Percentage mentors: ${percentageMentors}%`); |
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,7 +1,13 @@ | ||
| //* Complete the function in exercise.js so that it halves the input | ||
| //* Try calling the function more than once with some different numbers | ||
|
|
||
| function halve(number) { | ||
| // complete the function here | ||
| return number / 2; | ||
| } | ||
|
|
||
| var result = halve(12); | ||
|
|
||
| console.log(result); | ||
| result = halve(14); | ||
| console.log(result); | ||
| result = halve(16); | ||
| console.log(result); |
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 |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| //* From scratch, write a function that divides two numbers | ||
| // Declare your function first | ||
|
|
||
| function divide(num1, num2){ | ||
| return num1 / num2; | ||
| } | ||
| var result = divide(3, 4); | ||
|
|
||
| console.log(result); |
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,5 +1,8 @@ | ||
| //* Write a function that takes a name (a string) and returns a greeting | ||
| // Write your function here | ||
|
|
||
| function createGreeting(name){ | ||
| return `Hello, my name is ${name}`; | ||
| } | ||
| var greeting = createGreeting("Daniel"); | ||
|
|
||
| console.log(greeting); |
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,5 +1,9 @@ | ||
| //* Write a function that adds two numbers together | ||
| //* Call the function, passing `13` and `124` as parameters, and assigning the returned value to a variable `sum` | ||
| // Declare your function first | ||
|
|
||
| function addNumbers(num1, num2){ | ||
| return num1 + num2; | ||
| } | ||
| // Call the function and assign to a variable `sum` | ||
|
|
||
| console.log(sum); | ||
| let sum = addNumbers(13, 124); | ||
| console.log(sum); |
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,5 +1,12 @@ | ||
| var mentor1 = "Daniel"; | ||
| var mentor2 = "Irina"; | ||
| var mentor3 = "Mimi"; | ||
| var mentor4 = "Rob"; | ||
| var mentor5 = "Yohannes"; | ||
| //- In `exercise.js` write a program that displays the percentage of students and mentors in the group | ||
| //- The percentage should be rounded to the nearest whole number (use a search engine to find out how to this with JavaScript) | ||
| //- You should have one function that calculates the percentage, and one function that creates a message | ||
|
|
||
| function percentageOfStudentsAndMentors(numberOfStudents, numberOfMentors){ | ||
| let total = numberOfStudents + numberOfMentors; | ||
| let percentageStudents = Math.round((numberOfStudents / total) * 100); | ||
| let percentageMentors = Math.round((numberOfMentors / total) * 100); | ||
| return `Percentage mentors: ${percentageStudents}%\nPercentage students: ${percentageMentors}%`; | ||
| } | ||
|
|
||
| console.log(percentageOfStudentsAndMentors(15, 8)); |
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //- In `exercise2.js` you have been provided with the names of some mentors. Write a program that logs a shouty greeting to each one. | ||
| //- Your program should include a function that spells their name in uppercase, and a function that creates a shouty greeting. | ||
| //- Log each greeting to the console. | ||
|
|
||
| function shoutyGreeting(mentorName){ | ||
| return `hello ${mentorName}`.toUpperCase(); | ||
| } | ||
|
|
||
| let mentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"]; | ||
|
|
||
| for (i = 0; i < 5; i++){ | ||
| console.log(shoutyGreeting(mentors[i])); | ||
| } |
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 |
|---|---|---|
|
|
@@ -45,10 +45,53 @@ | |
|
|
||
| // This should log "The ball has shaken!" | ||
| // and return the answer. | ||
|
|
||
| // Object "possibleAnswers" declared for the 4 categories to set up key:value pairs | ||
| let possibleAnswers = { | ||
| veryPositive: ["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."], | ||
| veryNegative: ["Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."] | ||
| } | ||
|
|
||
| // Random number function | ||
| function getRandomNumber(min, max) { | ||
| // Produces a random decimal number between 0-1 then multiplies it by the range then adds the min value. Rounds to nearest number. | ||
| return Math.round(Math.random() * (max - min) + min); | ||
| } | ||
|
|
||
| // Variable used in shakeBall() function and checkAnswer function to select an answer from the array of the chosen category (passes in a random number between 0 and 4) | ||
| let answerSelection = getRandomNumber(0 , 4); | ||
|
|
||
| function shakeBall() { | ||
| //Write your code in here | ||
| console.log("The ball has shaken!"); | ||
| let randomNumber = getRandomNumber(1, 4); | ||
| let answer = ""; | ||
|
|
||
| if (randomNumber == 1) { | ||
| // if the random number selected is 1, then variable "answer" is assigned the value of a random selection (0-4) from the "veryPositive" values array in the possibleAnswers object | ||
| answer = possibleAnswers.veryPositive[answerSelection]; | ||
| return(answer); | ||
| } | ||
| else if (randomNumber == 2) { | ||
| // if the random number selected is 2, then variable "answer" is assigned the value of a random selection (0-4) from the "positive" values array in the possibleAnswers object | ||
| answer = possibleAnswers.positive[answerSelection]; | ||
| return(answer); | ||
| } | ||
| else if (randomNumber == 3) { | ||
| // if the random number selected is 3, then variable "answer" is assigned the value of a random selection (0-4) from the "negative" values array in the possibleAnswers object | ||
| answer = possibleAnswers.negative[answerSelection]; | ||
| return(answer); | ||
| } | ||
| else if (randomNumber == 4) { | ||
| // if the random number selected is 4, then variable "answer" is assigned the value of a random selection (0-4) from the "veryNegative" values array in the possibleAnswers object | ||
| answer = possibleAnswers.veryNegative[answerSelection]; | ||
| return(answer); | ||
| } | ||
| } | ||
|
|
||
| console.log(shakeBall()); | ||
|
|
||
| /* | ||
| This function should say whether the answer it is given is | ||
| - very positive | ||
|
|
@@ -58,10 +101,29 @@ function shakeBall() { | |
|
|
||
| This function should expect to be called with any value which was returned by the shakeBall function. | ||
| */ | ||
| function checkAnswer(answer) { | ||
|
|
||
| function checkAnswer(answer, answerSelection) { | ||
| //Write your code in here | ||
| // if the "veryPositive" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "very positive" | ||
| if (possibleAnswers.veryPositive.includes(answer, [answerSelection])) { | ||
|
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 Kara. you used includes method as exactly as described in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes. Your codes are brilliant!
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 all your helpful suggestions :) |
||
| return "very positive"; | ||
| } | ||
| // if the "positive" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "positive" | ||
| else if (possibleAnswers.positive.includes(answer, [answerSelection])) { | ||
| return "positive"; | ||
| } | ||
| // if the "negative" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "negative" | ||
| else if (possibleAnswers.negative.includes(answer, [answerSelection])) { | ||
| return "negative"; | ||
| } | ||
| // if the "veryNegative" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "very negative" | ||
| else if (possibleAnswers.veryNegative.includes(answer, [answerSelection])) { | ||
| return "very negative"; | ||
| } | ||
| } | ||
|
|
||
| checkAnswer(); | ||
| console.log(); | ||
| /* | ||
| ================================== | ||
| ======= TESTS - DO NOT MODIFY ===== | ||
|
|
||
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
Oops, something went wrong.
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.
Great work Kara ✨ One suggestion rather than reassigning
goodCodewhen you want to call another function, you can nest function calls for example:let goodCode = format(multiply(add(startingValue, 10), 2));This still gives the same result but this is an alternative solution and is all done to preference.
Keep up the good work