diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..017a3123e 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,4 @@ console.log("Hello world"); +console.log("Are you learning as well?"); +console.log("I " + "hope " + "that you know " + "what you are doing."); +console.log(3); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..3244c32f2 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); diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..11b1e4b1f 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"; console.log(message); +var messageType = typeof message; +console.log(messageType); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..e15251fc8 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` - +var firstPart = "Hello, my name is "; +var secondPart = "Ro."; +var message = firstPart + secondPart; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..6609cb1fe 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,18 @@ // Start by creating a variable `message` - +var name = "Daniel"; +var nameLength = name.length; +var messagePart; +if (name.length === 1) { + var messagePart = "character"; +} else { + var messagePart = "characters"; +} +var message = + "My name is " + + name + + " and my name is " + + nameLength + + " " + + messagePart + + " long."; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..0b0b66a5f 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,18 @@ const name = " Daniel "; - +var noSpaceName = name.trim(); +var nameLength = noSpaceName.length; +var messagePart; +if (noSpaceName.length === 1) { + var messagePart = "character"; +} else { + var messagePart = "characters"; +} +var message = + "My name is " + + noSpaceName + + " and my name is " + + nameLength + + " " + + messagePart + + " long."; console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..b787d49bb 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,9 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents = 15; +let numberOfMentors = 8; +console.log("Number of students: " + numberOfStudents); +console.log("Number of mentors: " + numberOfMentors); +console.log( + "Total number of students and mentors: " + + (numberOfStudents + numberOfMentors) +); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..f88fe01b8 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,11 @@ var numberOfStudents = 15; var numberOfMentors = 8; +let total = numberOfStudents + numberOfMentors; +function percentageOf(num) { + var precisePercentage = (num / total) * 100; + let roughPercentage = Math.round(precisePercentage); + return roughPercentage; +} + +console.log("Percentage students: " + percentageOf(numberOfStudents) + "%"); +console.log("Percentage mentors: " + percentageOf(numberOfMentors) + "%"); diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..99898ae3c 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,4 +1,5 @@ function halve(number) { + return number / 2; // complete the function here } diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..75d1c2f90 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,4 +1,5 @@ function triple(number) { + return number * 3; // complete function here } diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..cb0b0d927 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,5 +1,6 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(num1, num2) { + return num1 * num2; // Calculate the result of the function and return it } diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..8f0f0680b 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,7 @@ // 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..8aaea4624 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,9 @@ // 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..0e5a78614 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,9 @@ // Declare your function first +function add(num1, num2) { + return num1 + num2; +} // Call the function and assign to a variable `sum` +let sum = add(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..1b8bd1499 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,17 @@ // Declare your function here -const greeting = createLongGreeting("Daniel", 30); +function createLongGreeting(name, age) { + let ageGreet; + if (age === 1) { + ageGreet = "year"; + } else { + ageGreet = "years"; + } + return ( + "Hello, my name is " + name + " and I'm " + age + " " + ageGreet + " old" + ); +} + +const greeting = createLongGreeting("Daniel", 20); console.log(greeting); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..b8449b254 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,13 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +function shoutGreeting(name) { + return "HELLO " + name.toUpperCase(); +} + +console.log(shoutGreeting(mentor1)); +console.log(shoutGreeting(mentor2)); +console.log(shoutGreeting(mentor3)); +console.log(shoutGreeting(mentor4)); +console.log(shoutGreeting(mentor5)); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index d82e59480..9a94f4b1a 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,9 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(num) { + return num * 1.4; +} /* CURRENCY CONVERSION @@ -15,12 +17,15 @@ 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(num) { + let result = num * 0.99 * 5.7; + return Number(result.toFixed(2)); +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. -To run these tests type `npm run extraTo run the tests for just this one file, type `npm run extra-tests -- --testPathPattern 1-syntax-errors` into your terminal +To run these tests type `npm run extraTo run the tests for just this one file, type `npm run extra-tests -- --testPathPattern 1-syntax-errors` correct path: `npm run extra-tests -- --testPathPattern 1-currency-conversion` into your terminal (Reminder: You must have run `npm install` one time before this will work!) */ diff --git a/extra/2-piping.js b/extra/2-piping.js index 9c8ebc76a..a673525ad 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,26 @@ 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 = +let badCode = format(24); /* BETTER PRACTICE */ -let goodCode = +let goodCode = format(24); /* ======= 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 f3adbefa5..02a03b72c 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -43,9 +43,41 @@ Very doubtful. */ + + // This should log "The ball has shaken!" // and return the answer. function shakeBall() { + + listOfAnswers = [ + "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.", +]; + +var randomNumber = Math.floor(Math.random() * 20); + + console.log("The ball has shaken!"); + + var answer = listOfAnswers[randomNumber]; + return answer; //Write your code in here } @@ -59,6 +91,17 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ function checkAnswer(answer) { + var shortMessage; + if (listOfAnswers.indexOf(answer) >= 0 && listOfAnswers.indexOf(answer) <= 4 ) { + shortMessage = "very positive"; + } else if (listOfAnswers.indexOf(answer) >= 5 && listOfAnswers.indexOf(answer) <= 9) { + shortMessage = "positive"; + } else if (listOfAnswers.indexOf(answer) >= 10 && listOfAnswers.indexOf(answer) <= 14) { + shortMessage = "negative"; + } else if (listOfAnswers.indexOf(answer) >= 15 && listOfAnswers.indexOf(answer) <= 19) { + shortMessage = "very negative"; + } + return shortMessage; //Write your code in here } @@ -101,7 +144,9 @@ test("magic 8 ball returns different values each time", () => { ); } - let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer)); + 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" diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..98360ac6b 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 9cca7603b..37c4ebc15 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 getStringLength(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 5a953ba60..d5ab20241 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,13 @@ // 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 in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.*/ +/*this function create random number, the result is random number time by 10*/ function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +/*The concat() method concatenates the string arguments to the calling string and returns a new string.*/ +/*this function is going to create connected word from word1 and word2 input*/ function combine2Words(word1, word2) { return word1.concat(word2); } @@ -11,6 +15,7 @@ function combine2Words(word1, 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.concat(" " + secondWord + " " + thirdWord); } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..f7aeb85f2 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(num) { + let result = num + num * 0.2; + return result; +} /* CURRENCY FORMATTING @@ -17,7 +20,10 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(num) { + let result = num + num * 0.2; + return "£" + result.toFixed(2); +} /* ===================================================