diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..6a1175bba 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +var greeting = "Hello world"; console.log(greeting); +console.log(greeting); +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..c659c6420 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +var message = "This is a string"; +var messageType = typeof message; console.log(message); +console.log(messageType); \ No newline at end of file diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..7294bbfbb 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - -console.log(message); +var greetingPart1 = "Hello"; +var greetingPart2 = "welcome to the world of programming!"; +var name = "Bob"; +console.log(greetingPart1 + " " + name + ", " + greetingPart2); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..57778f467 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` - -console.log(message); +var sentencePart1 = "My name is"; +var name = "Bob"; +var sentencePart2 = "and my name is"; +var nameLength = name.length; +var sentencePart3 = "characters long."; +console.log(sentencePart1 + " " + name + " " + sentencePart2 + " " + nameLength + " " + sentencePart3); \ No newline at end of file diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..62fdcdd23 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,14 @@ +// Code that existed already const name = " Daniel "; -console.log(message); +// Code written by me +// Variable initialization +var trimmedName = name.trim(); +var sentencePart1 = "My name is"; +var sentencePart2 = "and my name is"; +var nameLength = trimmedName.length; +var sentencePart3 = "characters long."; + +// Print the message on console +console.log(sentencePart1 + " " + trimmedName + " " + sentencePart2 + " " + nameLength + " " + sentencePart3); + diff --git a/exercises/G-numbers/README.md b/exercises/G-numbers/README.md index 4c6f45a41..75136adf1 100644 --- a/exercises/G-numbers/README.md +++ b/exercises/G-numbers/README.md @@ -6,7 +6,7 @@ Unlike strings, numbers do not need to be wrapped in quotes. var age = 30; ``` -You can use mathematical operators to caclulate numbers: +You can use mathematical operators to calculate numbers: ```js var sum = 10 + 2; // 12 @@ -17,13 +17,13 @@ var difference = 10 - 2; // 8 ## Exercise -* Create two variables `numberOfStudents` and `numberOfMentors` -* Log a message that displays the total number of students and mentors +- Create two variables `numberOfStudents` and `numberOfMentors` +- Log a message that displays the total number of students and mentors ## Expected result ``` Number of students: 15 Number of mentors: 8 -Total numnber of students and mentors: 23 +Total number of students and mentors: 23 ``` diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..69f752e10 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,7 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +var numberOfStudents = 15; +var numberOfMentors = 8; +var total = numberOfStudents + numberOfMentors; +console.log("Number of students: " + numberOfStudents); +console.log("Number of mentors: " + numberOfMentors); +console.log("Total number of students and mentors: " + total); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..192793260 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,12 @@ +// Variable initialization var numberOfStudents = 15; var numberOfMentors = 8; +var total = numberOfStudents + numberOfMentors; +var percentOfStudents = numberOfStudents / (total) * 100; +var percentOfMentors = numberOfMentors / (total) * 100; + +// Print the message +console.log(`Percentage students: ${Math.round(percentOfStudents)}%`); +console.log(`Percentage mentors: ${Math.round(percentOfMentors)}%`); + + diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..8a0522776 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,12 @@ function halve(number) { - // complete the function here + return number/2; } var result = halve(12); +console.log(result); + +result = halve(23); +console.log(result); +result = halve(12458798); console.log(result); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..c2ebe3eb0 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,7 @@ -function triple(number) { - // complete function here +function triple(number) +{ + return 3*number; } var result = triple(12); - console.log(result); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..4614501b1 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,6 @@ // Complete the function so that it takes input parameters -function multiply() { - // Calculate the result of the function and return it +function multiply(num1, num2) { + 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..5217381a8 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,11 @@ -// Declare your function first +// function declaration +function divide(num1, num2) +{ + return num1/num2; +} +// function call var result = divide(3, 4); +// print the result console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..d166613e8 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,4 +1,8 @@ -// Write your function here +// function declaration +function createGreeting(name) { + message = "Hello, my name is " + name; + return message; +} var greeting = createGreeting("Daniel"); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..cabd0398d 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,10 @@ -// Declare your function first +// Function declaration +function add(num1, num2) { + return num1 + num2; +} // Call the function and assign to a variable `sum` +sum = add(13, 124); +// Print the result console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..182e3d0d4 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,8 @@ -// Declare your function here +// Function declaration +function createLongGreeting(name, age) { + message = "Hello, my name is " + name + " and I'm " + age + " years old"; + return message; +} const greeting = createLongGreeting("Daniel", 30); diff --git a/exercises/L-functions-nested/README.md b/exercises/L-functions-nested/README.md index f5e571303..11597c986 100644 --- a/exercises/L-functions-nested/README.md +++ b/exercises/L-functions-nested/README.md @@ -9,7 +9,7 @@ function getAgeInDays(age) { return age * 365; } -function createCreeting(name, age) { +function createGreeting(name, age) { var ageInDays = getAgeInDays(age); var message = "My Name is " + name + " and I was born over " + ageInDays + " days ago!"; diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..fb80057c3 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,21 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; +// function to calculate the percentage +function percentage(num, total) { + return (num / total * 100); +} + +// function to create the message +function createMessage(memberType, percentage) { + message = `Percentage ${memberType}: ${Math.round(percentage)}%`; + return message; +} + +// variable initialization +var numberOfStudents = 15; +var numberOfMentors = 8; +var total = numberOfStudents + numberOfMentors; +var percentOfStudents = percentage(numberOfStudents, total); +var percentOfMentors = percentage(numberOfMentors, total); + +// print the results +console.log(createMessage("students", percentOfStudents)); +console.log(createMessage("mentors", percentOfMentors)); \ 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..ab8ff546b --- /dev/null +++ b/exercises/L-functions-nested/exercise2.js @@ -0,0 +1,24 @@ +// function to convert a word to uppercase +function toUpper(word) { + return word.toUpperCase(); +} + +// function to create the greeting +function createShoutyGreeting(name) { + var greeting = `HELLO ${toUpper(name)}`; + return greeting; +} + +// variable initialization +var mentor1 = "Daniel"; +var mentor2 = "Irina"; +var mentor3 = "Mimi"; +var mentor4 = "Rob"; +var mentor5 = "Yohannes"; + +// print the results +console.log(createShoutyGreeting(mentor1)); +console.log(createShoutyGreeting(mentor2)); +console.log(createShoutyGreeting(mentor3)); +console.log(createShoutyGreeting(mentor4)); +console.log(createShoutyGreeting(mentor5)); \ No newline at end of file diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..2152ef92f 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,17 +5,21 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(priceInPound) { + return priceInPound * 1.4; +} /* CURRENCY FORMATTING =================== The business is now breaking into the Brazilian market - Write a new function for converting to the Brazilian real (exchange rate is 5.7 BRL to £) + Write a new function for converting to the Brazilian real (exchange rate is 5.643 BRL to £) 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(priceInPound) { + return priceInPound * 5.643; +} /* ======= 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..55b4965c9 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,34 @@ 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(num) { + return "£" + num; } const startingValue = 2 // Why can this code be seen as bad practice? Comment your answer. -let badCode = - -/* BETTER PRACTICE */ +/* + This is a bad coding example because it is unreadable and confusing. Reading this bit of code is difficult. Calling a function, within a function call and so on can easily lead to errors. +*/ +let badCode = format(multiply(add(startingValue, 10), 2)); -let goodCode = +// BETTER PRACTICE +/* + This is considered a better practice because the complicated task has been broken down into small readable, easy to understand steps. +*/ +let step1 = add(startingValue, 10); +let step2 = multiply(step1, 2); +let finalStep = format(step2); +let goodCode = finalStep; /* ======= 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..4573f3004 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,7 +46,19 @@ // This should log "The ball has shaken!" // and return the answer. function shakeBall() { - //Write your code in here + + // Array containing all the possible answers + let possibleAnswers = ["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."]; + + // Generate a random number to chose an answer from array of answers + let tempIndex = Math.random()*(possibleAnswers.length - 1); + + // round the value of index to nearest whole number + let index = Math.round(tempIndex); + + // log the message and return the randomly selected answer + console.log("The ball has shaken!"); + return possibleAnswers[index]; } /* @@ -60,6 +72,32 @@ function shakeBall() { */ function checkAnswer(answer) { //Write your code in here + let result; + switch(answer) + { + case "It is certain." : + case "It is decidedly so." : + case "Without a doubt." : + case "Yes - definitely." : + case "You may rely on it." : result = "very positive"; break; + case "As I see it, yes." : + case "Most likely." : + case "Outlook good." : + case "Yes." : + case "Signs point to yes." : result = "positive"; break; + case "Reply hazy, try again." : + case "Ask again later." : + case "Better not tell you now." : + case "Cannot predict now." : + case "Concentrate and ask again." : result = "negative"; break; + case "Don't count on it." : + case "My reply is no." : + case "My sources say no." : + case "Outlook not so good." : + case "Very doubtful." : result = "very negative"; break; + default : break; + } + return result; } /* @@ -72,6 +110,7 @@ To run these tests type `node 3-magic-8-ball.js` into your terminal ================================== */ + const log = console.log; let logged; console.log = function () { diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..7c71d981e 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -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 " + 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; - - return "The total is total" + total = a + b; + 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..a33f66cd4 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,21 @@ // Add comments to explain what this function does. You're meant to use Google! +/* + * + * The Math.random() function returns a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive). The function getNumber() multiplies this pseudo random number by 10 and returns a number between 0 (inclusive) and 10 (exclusive). + * + * +*/ function getNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +/* + * + * This function concatenates string w2 at the end of string w1. And returns this new concatenated string. + * + * +*/ function s(w1, w2) { return w1.concat(w2); } @@ -11,6 +23,7 @@ function s(w1, w2) { 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; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..5a5c02d73 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,9 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(price) { + return price*1.2; +} /* CURRENCY FORMATTING @@ -17,7 +19,16 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) { + // Calculate the price including sales tax + taxedPrice = calculateSalesTax(price); + + /* + Return the correctly formatted final price. + The toFixed() method converts a number into a string, rounding to a specified number of decimals. + */ + return "£" + taxedPrice.toFixed(2); +} /* ===================================================