diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..91c5b702a 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1 @@ -console.log("Hello world"); +console.log('Hello World. I just started learning JavaScript!'); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..eccb4e25f 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `greeting` - + +let 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..3a5807781 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,4 @@ -// Start by creating a variable `message` - +let message = "This is a string"; console.log(message); +console.log(typeof message); // string + diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..219e0c574 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` +let name = 'Delroy'; +let greeting = 'Hello'; + +console.log(greeting + " " + name); -console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..5d78cb2bf 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,4 @@ -// Start by creating a variable `message` +let name = 'Delroy'; + +console.log("My name is " + name + " and my name is " + name.length + " characters long"); -console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..5ef7872dd 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,5 @@ -const name = " Daniel "; +const aname = " Daniel "; +let myname = aname.trim(); + +console.log("My name is " + myname + " and my name is " + myname.length + " characters long"); -console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..4904b21a0 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,8 @@ -// Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents = 78; +let numberOfMentors = 7; +let sum = numberOfStudents + numberOfMentors; + +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..3cadf32ed 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,7 @@ -var numberOfStudents = 15; -var numberOfMentors = 8; +let numberOfStudents = 15; +let numberOfMentors = 8; +let sum = numberOfStudents + numberOfMentors; + +console.log("Percentage students: "+Math.round(numberOfMentors / sum * 100) + '%'); +console.log("Percentage mentors: "+Math.round(numberOfStudents / sum * 100) + '%'); + diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..c028b6b67 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,13 @@ function halve(number) { - // complete the function here + return number / 2; } -var result = halve(12); +let result = halve(12); +console.log(result); + +result = halve(-77); +console.log(result); +result = halve(34); console.log(result); + diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..05b858b62 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,8 @@ function triple(number) { - // complete function here + return number*3; } 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..da2b4cc06 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,11 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(a,b) { // Calculate the result of the function and return it + return a * b; } // Assign the result of calling the function the variable `result` var result = multiply(3, 4); console.log(result); + diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..aaeb981a5 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,9 @@ // Declare your function first +function divide(m,n) { + return m / n; +} 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..3c8df3493 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(string) { + return "Hello, my name is " + string; +} -var greeting = createGreeting("Daniel"); +let greeting = createGreeting("Daniel"); console.log(greeting); + diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..3c817d10a 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,11 @@ // Declare your function first +function add(m,n) { + return m + n; +} + // 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..64e12e1a1 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,32 @@ // Declare your function here +function createLongGreeting(string,number) { + return "Hello, my name is " + string + " and I'm " + number + " years old"; +} + const greeting = createLongGreeting("Daniel", 30); console.log(greeting); + +https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1/tree/master/exercises/L-functions-nested + +function UpperCase(string) { + return string.toUpperCase(); +} + +function shoutyGreeting(name) { + let message = "HELLO " + UpperCase(name); + return message; +} + +let mentor1 = "Daniel"; +let mentor2 = "Irina"; +let mentor3 = "Mimi"; +let mentor4 = "Rob"; +let mentor5 = "Yohannes"; +console.log(shoutyGreeting(mentor1)); +console.log(shoutyGreeting(mentor2)); +console.log(shoutyGreeting(mentor3)); +console.log(shoutyGreeting(mentor4)); +console.log(shoutyGreeting(mentor5)); + diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..c1eaec4ca 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,27 +1,25 @@ // 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; } /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== - 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 1-syntax-errors` into your terminal (Reminder: You must have run `npm install` one time before this will work!) - =================================================== */ @@ -37,4 +35,4 @@ test("introduceMe function returns the correct string", () => { test("getTotal returns a string describing the total", () => { expect(getTotal(23, 5)).toEqual("The total is 28"); -}); +}); \ No newline at end of file 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..ba8418265 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,9 @@ -// Add comments to explain what this function does. You're meant to use Google! +// This generates a random number then multiplies it by 10; then returns the result function getRandomNumber() { return Math.random() * 10; } -// Add comments to explain what this function does. You're meant to use Google! +// This takes 2 strings and joins them together, returning the result function combine2Words(word1, word2) { return word1.concat(word2); } @@ -11,6 +11,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 + ' ' + secondWord + ' ' + thirdWord; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..df06f09f3 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+price*20/100 +} /* CURRENCY FORMATTING @@ -17,7 +19,14 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(number) { + + number = calculateSalesTax(number); + + +// The toFixed() method rounds a number to a given number of digits. + return "£" + number.toFixed(2); +} /* ===================================================