diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..3e923cfb8 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -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); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..da6348264 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -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); +} \ No newline at end of file diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..aa5c65ca9 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -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); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..999734039 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -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); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..c26eca748 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -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); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..9f9afa155 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -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); \ No newline at end of file diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..c0ac27e99 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -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}`); \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..284da2c4e 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -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}%`); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..c8b0373db 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -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); \ No newline at end of file diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..6deefdc5d 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,7 @@ +//* Complete the function in exercise2.js so that it triples the input + function triple(number) { - // complete function here + return number * 3; } var result = triple(12); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..768d0b9c0 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,8 @@ +//* Write a function that multiplies two numbers together // Complete the function so that it takes input parameters -function multiply() { +function multiply(num1, num2) { // Calculate the result of the function and return it + return num1 * num2; } // Assign the result of calling the function the variable `result` diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..4eafe7a0a 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -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); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..dec5ecbf7 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -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); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..3b2a4473e 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -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); \ No newline at end of file diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..52046558c 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,8 @@ +//* Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) // Declare your function here +function createLongGreeting(name, age){ + return `Hello, my name is ${name} and I am ${age} years old`; +} const greeting = createLongGreeting("Daniel", 30); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..0693d91a7 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -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)); \ No newline at end of file diff --git a/exercises/L-functions-nested/exercise2.js b/exercises/L-functions-nested/exercise2.js new file mode 100644 index 000000000..029fd87ea --- /dev/null +++ b/exercises/L-functions-nested/exercise2.js @@ -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])); +} diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..d7a93dca3 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,8 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} - +function convertToUSD(priceInPounds) { + return priceInPounds * 1.4; +} +console.log(convertToUSD(32)); /* CURRENCY FORMATTING =================== @@ -15,7 +17,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(priceInPounds) { + return (priceInPounds * 0.99) * 5.7; +} +console.log(convertToBRL(30)); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/2-piping.js b/extra/2-piping.js index 067dd0f5b..d14121f44 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,31 @@ the final result to the variable goodCode */ -function add() { - +function add(num1, num2) { + return num1 + num2; } +console.log(add(1, 3)); -function multiply() { - +function multiply(num1, num2) { + return num1 * num2; } +console.log(multiply(2, 3)); -function format() { - +function format(number) { + return `£${number}`; } +console.log(format(16)); -const startingValue = 2 +const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = add(startingValue, 10); badCode = multiply(badCode, 2); console.log(badCode = format(badCode)); // code all on one line is difficult to read /* BETTER PRACTICE */ - -let goodCode = +// Comments can be added to explain each line and it is easier to read +let goodCode = add(startingValue, 10); // function call to add 10 to startingValue and store result in goodCode +goodCode = multiply(goodCode, 2); // function call to multiply value of goodCode by 2 +console.log(goodCode = format(goodCode)); // function call to format value of goodCode /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index e32182cfe..e046b7756 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -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])) { + 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 ===== diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..6f498d235 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -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; - return "The total is total" + return "The total is " + total; } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 3c578ad87..1f1245b39 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,15 @@ // 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(); } function getWordLength(word) { - return "word".length(); + return word.length; } function multiply(a, b, c) { - a * b * c; - return; + return a * b * c; } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index c9221a200..dca103231 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,18 +1,21 @@ // Add comments to explain what this function does. You're meant to use Google! function getNumber() { - return Math.random() * 10; + return Math.random() * 10; // returns the number entered multiplied by a random integer from 0 to 9 } // Add comments to explain what this function does. You're meant to use Google! function s(w1, w2) { - return w1.concat(w2); + return w1.concat(w2); // concatenates w1 and w2 without white space } function concatenate(firstWord, secondWord, thirdWord) { // Write the body of this function to concatenate three words together. + return `${firstWord} ${secondWord} ${thirdWord}`; // Look at the test case below to understand what this function is expected to return. } +console.log(concatenate("Code","Your","Future")); + /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..6186aaaf7 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,10 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(productPrice) { + return productPrice * 1.2; +} +console.log(calculateSalesTax(15)); /* CURRENCY FORMATTING @@ -17,8 +20,11 @@ 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 totalCost = productPrice * 1.2; + return `£${totalCost.toFixed(2)}`; // fixes to 2 decimal places +} +console.log(addTaxAndFormatCurrency(15)); /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE =====