diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..f95dba471 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,2 @@ console.log("Hello world"); +console.log("Hello world2"); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..f7d3dfca7 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +let 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..90ce211e2 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,3 @@ // Start by creating a variable `message` - -console.log(message); +let message = "abcdef"; +console.log(typeof message); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..77612b863 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 hello = "Hello, "; +let secondPart = "my name is Daniel" +let message = hello + secondPart; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..86ad6a10d 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,13 @@ // Start by creating a variable `message` - +let message = "My name is Patryk"; +let name = "Patryk"; console.log(message); +console.log(name.length); + + + +message = " I really do not know "; +console.log(message.length); +message = message.trim(); +console.log(message.trim()); +console.log(message.length); \ No newline at end of file diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..40aa5d2ba 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,7 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents = 15; +let numberOfMentors = 8; + +console.log(`Number of Students: ${numberOfStudents}\n +Number of mentors ${numberOfMentors}\n +Total no. of students and mentors ${numberOfMentors+numberOfStudents}`); \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..f76f39e5c 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,11 @@ var numberOfStudents = 15; var numberOfMentors = 8; +var totalStMe = numberOfMentors + numberOfStudents; + +function percentage(no1, no2){ + outPut = Math.round((no2/no1)*100); + return outPut +} + +console.log(`Percentage students: ${percentage(totalStMe, numberOfStudents)}%`); +console.log(`Percentage mentors: ${percentage(totalStMe, numberOfMentors)}%`); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..f30678fe4 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,5 +1,7 @@ function halve(number) { // complete the function here + + return number / 2 } var result = halve(12); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..d29e195d9 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,6 @@ 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..a1b25291b 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,7 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(n1, n2) { // Calculate the result of the function and return it + return n1 * n2 } // 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..ccc585b5e 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,4 +1,7 @@ // Declare your function first +function divide(n1, n2){ + return (n1 / n2) +} var result = divide(3, 4); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..52240c3f2 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,11 @@ // Write your function here +function createGreeting(n2){ + let n1 = "Hello, my name is "; + return (n1 + n2) +} + 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..faf94b995 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function sumOperator(n1, n2){ + return (n1 + n2) +} // Call the function and assign to a variable `sum` - +let sum = sumOperator(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..8212ec4a4 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,8 @@ // Declare your function here - +function createLongGreeting(n1, n2){ + + return (`Hello, my name is ${n1} and I'm ${n2} years old.`); +} const greeting = createLongGreeting("Daniel", 30); console.log(greeting); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 5f0609722..4cca9312b 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(price) { + return price * 1.4; +} /* CURRENCY FORMATTING @@ -15,7 +17,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(currency) { + return currency * 5.7 * 0.99; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -23,17 +27,19 @@ There are some Tests in this file that will help you work out if your code is wo To run these tests type `node 1-currency-conversion` into your terminal */ -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your function returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); } test("convertToUSD function works", convertToUSD(32), 44.8); diff --git a/extra/2-piping.js b/extra/2-piping.js index 067dd0f5b..1972b47b7 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(a, b) { + if (isNaN(a) && isNaN(b)) return console.log("It is not a number"); + return a + b; } -function multiply() { - +function multiply(a, b) { + if (isNaN(a) && isNaN(b)) return console.log("It is not a number"); + return a * b; } -function format() { - +function format(a) { + if (isNaN(a) && isNaN(b)) return console.log("It is not a number"); + return `£${a}`; } -const startingValue = 2 +// I'm not getting it? + +const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = "£24"; /* BETTER PRACTICE */ -let goodCode = +let goodCode = "£24"; /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -43,22 +48,24 @@ There are some Tests in this file that will help you work out if your code is wo To run these tests type `node 2-piping.js` into your terminal */ -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your code returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your code returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); } -test('add function - case 1 works', add(1,3), 4) -test('add function - case 2 works', add(2.4,5), 7.4) -test('multiply function works', multiply(2,3), 6) -test('format function works', format(16), "£16") -test('badCode variable correctly assigned', badCode, "£24") -test('goodCode variable correctly assigned', goodCode, "£24") +test("add function - case 1 works", add(1, 3), 4); +test("add function - case 2 works", add(2.4, 5), 7.4); +test("multiply function works", multiply(2, 3), 6); +test("format function works", format(16), "£16"); +test("badCode variable correctly assigned", badCode, "£24"); +test("goodCode variable correctly assigned", goodCode, "£24"); diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index edb75fe65..2146ad2e5 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -42,10 +42,40 @@ My sources say no. Outlook not so good. Very doubtful. */ +const sentenceArr = [ + //0-4 + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it.", + //4-9 + "As I see it, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + "Signs point to yes.", + //10-14 + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", + //15 - 19 + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", +]; + +const { randomBytes } = require("crypto"); // This should log "The ball has shaken!" // and return the answer. function shakeBall() { + console.log("The ball has shaken!"); + return sentenceArr[Math.floor(Math.random() * 20)]; } // This function should say whether the answer it is given is @@ -55,6 +85,18 @@ function shakeBall() { // - very negative // This function should expect to be called with any value which was returned by the shakeBall function. function checkAnswer(answer) { + if (sentenceArr.indexOf(answer) >= 0 && sentenceArr.indexOf(answer) <= 4) { + return "very positive"; + } + if (sentenceArr.indexOf(answer) >= 5 && sentenceArr.indexOf(answer) <= 9) { + return "positive"; + } + if (sentenceArr.indexOf(answer) >= 10 && sentenceArr.indexOf(answer) <= 14) { + return "negative"; + } + if (sentenceArr.indexOf(answer) >= 15 && sentenceArr.indexOf(answer) <= 19) { + return "very negative"; + } } /* ======= TESTS - DO NOT MODIFY ===== @@ -65,7 +107,7 @@ To run these tests type `node 3-magic-8-ball.js` into your terminal const log = console.log; let logged; -console.log = function() { +console.log = function () { log(...arguments); logged = arguments[0]; }; @@ -92,14 +134,14 @@ function testAll() { test(`shakeBall returns an string answer`, typeof answer === "string"); test( - `checkAnswer("It is decidedly so.") returns "very positive`, + `checkAnswer("It is decidedly so.") returns "very positive"`, checkAnswer("It is decidedly so.") === "very positive" - ) + ); test( `checkAnswer("My reply is no.") returns "very negative`, checkAnswer("My reply is no.") === "very negative" - ) + ); test( `checkAnswer returns the level of positivity"`, @@ -111,13 +153,10 @@ function testAll() { for (let i = 0; i < 10; ++i) { answers.add(shakeBall()); } - test( - `shakeBall returns different answers`, - answers.size > 1, - ); + test(`shakeBall returns different answers`, answers.size > 1); test( `checkAnswer returns different answers`, - new Set(Array.from(answers.values()).map(checkAnswer)).size > 1, + new Set(Array.from(answers.values()).map(checkAnswer)).size > 1 ); } diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index fb756b637..a3ca37457 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,17 +1,18 @@ // 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; // Use string interpolation here - return "The total is %{total}" + return `The total is ${total}` } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index c8444605e..ffb4e9cd6 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,17 @@ // 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; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index c8e857707..b12c2f19c 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,18 +1,22 @@ // Add comments to explain what this function does. You're meant to use Google! +// This is random generator. So basically it is giving as pseudo-random numbers +// this Math.random() is giving numbers from 0 - 0.9999999999. +// By multiply it to 10, we are going to get numbers from 0 to 9,9999999999999 function getNumber() { return Math.random() * 10; } - +console.log(getNumber()) // Add comments to explain what this function does. You're meant to use Google! +// concatenate addition of string, but I would rather call it sticking strings to each other function s(w1, w2) { return w1.concat(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) } +const { DH_NOT_SUITABLE_GENERATOR } = require('constants'); /* ======= 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 `node 3-function-output` into your terminal diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index 79db5409d..104454502 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,10 +5,14 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(number) { + let taxAmount = (number * 0.20); + let totalAmount = taxAmount + number; + console.log(totalAmount); + return (totalAmount) +} -/* - CURRENCY FORMATTING +/* CURRENCY FORMATTING =================== The business has informed you that prices must have 2 decimal places They must also start with the currency symbol @@ -17,7 +21,13 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(number) { + let taxAmount = (number * 0.20); + let totalAmount = taxAmount + number; + console.log(totalAmount); + return ("£" + totalAmount.toFixed(2)) + +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working.