From 8f3b5296d6b5f5e7b58dbc6a11e22ede66c00b48 Mon Sep 17 00:00:00 2001 From: Krishan Date: Sat, 21 Aug 2021 12:56:30 +0100 Subject: [PATCH] Js core week 1 done finished --- exercises/B-hello-world/exercise.js | 4 ++ exercises/C-variables/exercise.js | 4 ++ exercises/D-strings/exercise.js | 4 ++ exercises/E-strings-concatenation/exercise.js | 5 +- exercises/F-strings-methods/exercise.js | 9 +++ exercises/F-strings-methods/exercise2.js | 5 ++ exercises/G-numbers/exercise.js | 4 ++ exercises/I-floats/exercise.js | 7 +++ exercises/J-functions/exercise.js | 6 ++ exercises/J-functions/exercise2.js | 2 +- exercises/K-functions-parameters/exercise.js | 5 +- exercises/K-functions-parameters/exercise2.js | 6 +- exercises/K-functions-parameters/exercise3.js | 4 ++ exercises/K-functions-parameters/exercise4.js | 6 +- exercises/K-functions-parameters/exercise5.js | 6 +- exercises/L-functions-nested/exercise.js | 22 +++++++ extra/1-currency-conversion.js | 11 +++- extra/2-piping.js | 24 +++++--- extra/3-magic-8-ball.js | 58 ++++++++++++++++++- mandatory/1-syntax-errors.js | 10 ++-- mandatory/2-logic-error.js | 7 +-- mandatory/3-function-output.js | 5 ++ mandatory/4-tax.js | 15 ++++- 23 files changed, 201 insertions(+), 28 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..a025ce3e9 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,5 @@ +console.log(`cd B-hello-world`) console.log("Hello world"); +console.log("I started to learn javascript!"); +console.log("Good Afternoon"); +console.log("I am thrilled!"); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..dcb5c9e01 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `greeting` console.log(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..f5c56c013 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` console.log(message); +var message = "Hello everyone, welcome to the show!"; +var typeOfMessage = typeof message; +console.log(message); +console.log(typeOfMessage); \ No newline at end of file diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..1e12f08f9 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` - +var greeting = "Good Morning ,"; +var yourName = "my name is "; +var name = "Krrish" +var message = greeting + yourName; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..d47d9e467 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,12 @@ // Start by creating a variable `message` console.log(message); +var message = "Hello my name is !!"; +var myName = "Krishan Kumar"; +console.log(message + myName.length); +//My name is Daniel and my name is 6 characters long + +console.log(message); +console.log( + "My name is " + myName + " and it's " + myName.length + " character long" +); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..7f10983ea 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,8 @@ const name = " Daniel "; +console.log(message); +const message = + "My name is " + name + " and my name is " + name.length + " characters long"; +console.log(message.trim()); + console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..603ac13f8 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,5 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +var numberOfMentors = 15; +var numberOfStudents = 35; +var totalNumber = numberOfMentors + numberOfStudents; +console.log("Total number of students and mentors is : " + totalNumber); \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..cea90b978 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,9 @@ var numberOfStudents = 15; var numberOfMentors = 8; +var NumOfTotalPeople = 23; + +var percentNum = numberOfStudents*100/NumOfTotalPeople; + +console.log("Percentage of mentors " + Math.round(percentNum) + "%"); +var percentNum = numberOfMentors*100/NumOfTotalPeople; +console.log("Percentage of students " + Math.round(percentNum) + "%"); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..67161ffbf 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); +console.log(result); + +var result = halve(5); +console.log(result); +var result = halve(19); console.log(result); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..03360480f 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,7 @@ 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..2f9faa0e3 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,10 @@ // 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..f641b00e0 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,9 @@ // Declare your function first -var result = divide(3, 4); +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..7705a6158 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..e359b702f 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,9 @@ // Declare your function first +function bigSum(num1, num2) { + return num1 + num2; + } -// Call the function and assign to a variable `sum` + // Call the function and assign to a variable `sum` +var sum = bigSum(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..5cf453b50 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,9 @@ // Declare your function here -const greeting = createLongGreeting("Daniel", 30); +function createLongGreeting(name, num1) { + return "Hello, my name is " + name + " and I'm " + num1 + " years old"; + } + + const greeting = createLongGreeting("Daniel", 30); console.log(greeting); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..8f39cfb86 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,25 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +//function to spelling name in uppercase + +function greeting(message) { + return message.toUpperCase(); +} +var greet_message = greeting("hello"); + +function getMentorName(name) { + return name.toUpperCase(); +} + +var mentor1 = getMentorName("Daniel"); +console.log(greet_message + " " + mentor1); +var mentor2 = getMentorName("Irina"); +console.log(greet_message + " " + mentor2); +var mentor3 = getMentorName("Mimi"); +console.log(greet_message + " " + mentor3); +var mentor4 = getMentorName("Rob"); +console.log(greet_message + " " + mentor4); +var mentor5 = getMentorName("Yohannes"); +console.log(greet_message + " " + mentor5); \ No newline at end of file diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index d82e59480..f4ca88e1f 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -6,6 +6,10 @@ */ function convertToUSD() {} +function convertToUSD(amount) { + let poundToDollar = 1.4 * amount; + return poundToDollar; +} /* CURRENCY CONVERSION @@ -16,11 +20,16 @@ function convertToUSD() {} */ function convertToBRL() {} +function convertToBRL(amount) { + let transferFee = amount * 0.99; + let poundToReal = (transferFee * 5.7).toFixed(2); + return parseFloat(poundToReal); +} /* ======= 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-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..67191da73 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,27 +16,33 @@ the final result to the variable goodCode */ -function add() { - +function add(num1, num2) { + let sumOfNum = num1 + num2; + return sumOfNum; } -function multiply() { - +function multiply(num1, num2) { + let multiplyNum = num1 * num2; + return multiplyNum; } -function format() { - +function format(num) { + let numToStr = "£" + num.toString(); + return numToStr; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = -/* BETTER PRACTICE */ +let badCode = `${format(multiply(add(startingValue, 10), 2))}`; -let goodCode = +/* BETTER PRACTICE */ +//let goodCode = +var addNum = add(startingValue, 10); //adding 10 to the starting value +let multiplyNum = multiply(addNum, 2); //multiplying the result of addNum by 2 +let goodCode = format(multiplyNum); //formatting the multiplied num into a string /* ======= 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..0d68a72ee 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -42,11 +42,44 @@ Outlook not so good. Very doubtful. */ +const ansArray = [ + //Very positive + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it.", + //Positive + "As I see it, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + "Signs point to yes.", + //Negative + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", + //Very negative + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", +]; +function generateRandom() { + return Math.floor(Math.random() * ansArray.length); +} // This should log "The ball has shaken!" // and return the answer. function shakeBall() { + const randomAnswer = ansArray[generateRandom()]; + //Write your code in here +console.log("The ball has shaken!"); +return randomAnswer; } /* @@ -58,10 +91,30 @@ 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 + let answerTurns; + for (let i = 0; i < ansArray.length; i++) { + if (answer === ansArray[i]) { + answerTurns = i; + } + } + if (answerTurns < 5) { + return "very positive"; + } else if (answerTurns >= 5 && answerTurns < 10) { + return "positive"; + } else if (answerTurns >= 10 && answerTurns < 15) { + return "negative"; + } else { + return "very negative"; + } } +let a = shakeBall(); +console.log(checkAnswer(a)); + + /* ================================== ======= TESTS - DO NOT MODIFY ===== @@ -101,7 +154,10 @@ 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..8221edb36 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,14 +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 " + (a + b); 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..f60b9db42 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -2,15 +2,20 @@ function getRandomNumber() { return Math.random() * 10; } +//generate a random number between 0 and 10 // Add comments to explain what this function does. You're meant to use Google! + function combine2Words(word1, word2) { return word1.concat(word2); } +//It merges two strings/arrays + 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..5d8c84a66 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() {} +function calculateSalesTax(price) { + return price + (20 * price) / 100; +} /* CURRENCY FORMATTING @@ -17,7 +20,15 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +//function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) +{ + price = price + (20 * price) / 100; + + return "£".concat(parseFloat(price). + toFixed(2)); + +} /* ===================================================