diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..5a5e1a685 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,3 @@ -console.log("Hello world"); +console.log(78967868678); + +console.log("Hello World.I just started learning JavaScript!"); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..68c55c70d 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `greeting` - +var greeting = "Hello world"; +console.log(greeting); +console.log(greeting); +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..c950961db 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); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..98c461e0d 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` +var greetingStart = "Hello, my name is "; +var name1 = "Oleks. "; +var you = "And you ? " +var greeting = greetingStart + name1 + you; -console.log(message); +console.log(greeting); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..1c016a4f2 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,25 @@ // Start by creating a variable `message` - +var name1 = "Oleks"; +var nameLength = name1.length; +var introduce1 = " My name is Oleks and my name is "; +var introduce2 = "characters long."; +var message = introduce1 + nameLength + " " + introduce2; console.log(message); +// second way +var name1 = "Oleks"; +var nameLength = name1.length; +var introduce1 = " My name is "; +var introduce3 = "and my name is "; +var introduce2 = "characters long."; +var message = introduce1 + name1 + " "+ introduce3 + +nameLength + " " + introduce2; +console.log(message); +// trim method +var name1 = "Oleks"; +var nameLength = name1.length; +var introduce1 = " My name is "; +var introduce3 = "and my name is "; +var introduce2 = "characters long."; +var message = + introduce1 + name1 + " " + introduce3 + nameLength + " " + introduce2; +console.log(message.trim()); \ No newline at end of file diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..d1edb205f 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,20 @@ const name = " Daniel "; +var name1 = "Oleks"; +var nameLength = name1.length; +var introduce1 = " My name is "; +var introduce3 = "and my name is "; +var introduce2 = "characters long."; +var message = + introduce1 + name1 + " " + introduce3 + nameLength + " " + introduce2; console.log(message); +// trim method +var name1 = "Oleks"; +var nameLength = name1.length; +var introduce1 = " My name is "; +var introduce3 = "and my name is "; +var introduce2 = "characters long."; +var message = + introduce1 + name1 + " " + introduce3 + nameLength + " " + introduce2; +console.log(message.trim()); + diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..64f40203b 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,12 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +var numberOfStudents = 50; +var numberOfMentors = 10; +var stud = "Number of students: "; +console.log(stud + numberOfStudents); +var ment = "Number of mentors: "; +console.log(ment + numberOfMentors); +var total = "Total number of students and mentors: "; +var sum = numberOfMentors + numberOfStudents; +console.log(total + sum); + + diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..0e2580730 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,29 @@ var numberOfStudents = 15; var numberOfMentors = 8; +var preciseAge = 30.612437; +varroughAge = Math.round(preciseAge); // 30 +console.log(varroughAge); + +//from G ex + new solution + +var stud = "Number of students: "; +console.log(stud + numberOfStudents); +var ment = "Number of mentors: "; +console.log(ment + numberOfMentors); +var total = "Total number of students and mentors: "; +var sum = numberOfMentors + numberOfStudents; +console.log(total + sum); +var percentageStudents = 100*numberOfStudents/sum +var percentageMentors = 100*numberOfMentors/sum +var roundStudents = Math.round(percentageStudents); +var roundMentors = Math.round(percentageMentors); + +console.log("Percentage mentors: " + roundMentors + " %."); +console.log("Percentage students: " + roundStudents + " %."); +// Result +// 31 +// Number of students: 15 +// Number of mentors: 8 +// Total number of students and mentors: 23 +// Percentage mentors: 35 %. +// Percentage students: 65 %. \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..4aa15efc3 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,21 @@ function halve(number) { - // complete the function here + return number/2; } var result = halve(12); +console.log("result 12/2=", result); -console.log(result); +var result = halve(24); +console.log("result 24/2=", result); + +var result = halve(100); +console.log("result 100/2=", result); + +var result = halve(365); + +console.log("result 365/2=", result); + +// result 12/2= 6 +// result 24/2= 12 +// result 100/2= 50 +// result 365/2= 182.5 \ No newline at end of file diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..453519c6f 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,9 @@ function triple(number) { - // complete function here + return number*3; } var result = triple(12); -console.log(result); +console.log("result 12 x 3 = ", result); + +// result 12 x 3 = 36 diff --git a/exercises/K-functions-parameters/README.md b/exercises/K-functions-parameters/README.md index 1614e221b..c850a7526 100644 --- a/exercises/K-functions-parameters/README.md +++ b/exercises/K-functions-parameters/README.md @@ -1,4 +1,4 @@ -The input given to a function is called a **parameter**. +"The input given to a function is called a **parameter**. A function can take more than one parameter: diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..9fa5abf89 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,9 @@ // Complete the function so that it takes input parameters -function multiply() { - // Calculate the result of the function and return it +function multiply(a, b) { + return a * b; } // Assign the result of calling the function the variable `result` var result = multiply(3, 4); -console.log(result); +console.log("result 3*4 = ", result); diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..d2bb1d2cd 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 ( a, b) { + return a / b; +} var result = divide(3, 4); -console.log(result); +console.log("a / b = ", result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..81f47c3b0 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,8 @@ -// Write your function here - +function createGreeting(name) { + return ("Hello, my name is " + name ); +} var greeting = createGreeting("Daniel"); console.log(greeting); + +// Hello, my name is Daniel \ No newline at end of file diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..b4ee9c0cc 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function summ(a, b) { + return a + b +} // Call the function and assign to a variable `sum` - -console.log(sum); +var sum = summ(13, 124); +console.log("13 + 124 = ", sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..938319e2d 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,11 @@ // 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); console.log(greeting); + + +// Hello, my name is Daniel and I am 30 years old \ No newline at end of file diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..f78ed6f18 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,19 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +function shoutyGreeting(mentor) { + return ("HELLO," + " " + mentor.toUpperCase() + " !") +} +console.log(shoutyGreeting(mentor1)); +console.log(shoutyGreeting(mentor2)); +console.log(shoutyGreeting(mentor3)); +console.log(shoutyGreeting(mentor4)); +console.log(shoutyGreeting(mentor5)); + +// Result a shouty greeting to each one mentors: +// HELLO, DANIEL ! +// HELLO, IRINA ! +// HELLO, MIMI ! +// HELLO, ROB ! +// HELLO, YOHANNES ! \ No newline at end of file diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..f58d96a6c 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,11 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(price) { + let dol = (price*1.4).toFixed(2); + return Number(dol); +} + /* CURRENCY CONVERSION @@ -15,7 +19,9 @@ 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(price) { + return Number((price*0.99*5.7).toFixed(2)); +} /* ======= 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 b4f8c4c1b..1bbe727e4 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,29 @@ the final result to the variable goodCode */ -function add() { - +function add(number1, number2) { +return number1 + number2; } -function multiply() { - +function multiply(number1, number2) { +return number1 * number2; } -function format() { - +function format(n) { +return "£" + n; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = format(multiply(add(startingValue, 10), startingValue)); // Because vjhjjk /* BETTER PRACTICE */ -let goodCode = +let sum = add(startingValue, 10); +let multiplies = multiply(sum, startingValue); +let formNumber = format(multiplies); +let goodCode = formNumber; /* ======= 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 46f65f928..f93875960 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -43,10 +43,59 @@ Very doubtful. */ +// This should log "The ball has shaken!" +// and return the answer.------------------------------------------------------------------HERE +//function shakeBall() { + //Write your code in here +//} + + + +let veryPositiveAnswers = [ + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it.", +]; + +let positiveAnswers = [ + "As I see it, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + "Signs point to yes.", +]; + +let negativeAnswers = [ + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", +]; + +let veryNegativeAnswers = [ + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", +]; + +let allAnswers = [ + ...veryPositiveAnswers, + ...positiveAnswers, + ...negativeAnswers, + ...veryNegativeAnswers, +]; + // This should log "The ball has shaken!" // and return the answer. function shakeBall() { - //Write your code in here + console.log("The ball has shaken!"); + let randomIndex = Math.floor(Math.random() * allAnswers.length); + return allAnswers[randomIndex]; } /* @@ -59,9 +108,39 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ function checkAnswer(answer) { - //Write your code in here + if (veryPositiveAnswers.includes(answer)) { + return "very positive"; + } else if (positiveAnswers.includes(answer)) { + return "positive"; + } else if (negativeAnswers.includes(answer)) { + return "negative"; + } else if (veryNegativeAnswers.includes(answer)) { + return "very negative"; + } } +const foo = shakeBall(); +const bar = checkAnswer(foo); +console.log(bar); + + + + + + +/* + This function should say whether the answer it is given is + - very positive + - positive + - negative + - very negative + + This function should expect to be called with any value which was returned by the shakeBall function.----------------------------HERE +*/ + //function checkAnswer(answer) { + //Write your code in here + //} + /* ================================== ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..89ceb13f0 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,18 +1,15 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { - return 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"; + const total = a + b; + return 'The total is ' + total; } - /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..995910d8d 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..e2425c6f4 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,18 +1,30 @@ // 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 +//that's greater than or equal to 0 and less than 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. +//Return value A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive). +// 0 < Number < 10 ; + function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +// The concat function concatenates two or more strings and returns the resulting string. +//Syntax : concat( string1, string2 [,string]* ) +// function combine2Words(word1, word2) { return word1.concat(word2); } function concatenate(firstWord, secondWord, thirdWord) { + return firstWord.concat(" ", 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. } - +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 ba77c7ae2..491edbe72 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,8 +5,10 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} - +function calculateSalesTax(price) { +let tax = price*0.2; +return price + tax; +} /* CURRENCY FORMATTING =================== @@ -17,7 +19,10 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) { + let newPrice = calculateSalesTax(price); + return "£" + newPrice.toFixed(2); +} /* =================================================== diff --git a/package.json b/package.json index 93e0861e3..9e7f2ffeb 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,21 @@ "url": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1/issues" }, "jest": { - "setupFilesAfterEnv": ["jest-extended"], + "setupFilesAfterEnv": [ + "jest-extended" + ], "projects": [ { "displayName": "mandatory", - "testMatch": ["/mandatory/*.js"] + "testMatch": [ + "/mandatory/*.js" + ] }, { "displayName": "extra", - "testMatch": ["/extra/*.js"] + "testMatch": [ + "/extra/*.js" + ] } ] },