From c8a704cbd551ca6bf92abb8e56c19c736ac5d752 Mon Sep 17 00:00:00 2001 From: Bahare Date: Mon, 21 Nov 2022 13:31:34 +0000 Subject: [PATCH 1/4] exercises done --- exercises/B-hello-world/exercise.js | 4 ++++ exercises/C-variables/exercise.js | 5 ++++- exercises/D-strings/exercise.js | 3 ++- exercises/E-strings-concatenation/exercise.js | 4 +++- exercises/F-strings-methods/exercise.js | 3 ++- exercises/F-strings-methods/exercise2.js | 2 +- exercises/G-numbers/exercise.js | 5 +++++ exercises/I-floats/exercise.js | 2 ++ exercises/J-functions/exercise.js | 1 + exercises/J-functions/exercise2.js | 1 + exercises/K-functions-parameters/exercise.js | 3 ++- exercises/K-functions-parameters/exercise2.js | 3 +++ exercises/K-functions-parameters/exercise3.js | 3 +++ exercises/K-functions-parameters/exercise4.js | 5 ++++- exercises/K-functions-parameters/exercise5.js | 3 +++ exercises/L-functions-nested/exercise.js | 22 +++++++++++++++++++ mandatory/1-syntax-errors.js | 8 +++---- 17 files changed, 66 insertions(+), 11 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..e461b6d0e 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,5 @@ console.log("Hello world"); +console.log("i study in code your future"); +console.log("hello" ,4 ," hello"); +console.log('bbc'); +console.log(544); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..d1a403ce6 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,6 @@ // 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..12030495c 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); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..ef062f703 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 greeting= "Hello ,my name is "; +let myName ="Bahare"; +let message =greeting+myName; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..9e73d01da 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 myName="Bahare" +let message="My name is "+myName+" and my name is "+myName.length +" characters long" console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..66acb5840 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,3 @@ const name = " Daniel "; - +let message="My name is "+name+" and my name is "+(name.trim()).length +" characters long" console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..448e6111c 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,6 @@ // 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)) \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..9c851aad8 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,4 @@ var numberOfStudents = 15; var numberOfMentors = 8; +console.log("Percentage students: "+ Math.round((numberOfStudents*100)/(numberOfMentors+numberOfStudents))+"%"); +console.log("Percentage mentors: "+ Math.round((numberOfMentors*100)/(numberOfMentors+numberOfStudents))+"%"); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..e9ba2f0ef 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,5 +1,6 @@ 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..6d216ffc6 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..302221e5f 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(a ,b) { // Calculate the result of the function and return it + return a*b; } // 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..6949b15a5 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 (a,b){ + return a/b; +} var result = divide(3, 4); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..d0821e697 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,4 +1,7 @@ // Write your function here +function createGreeting (name){ + return "Hello, my name is "+name ; +} var greeting = createGreeting("Daniel"); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..97b5c3a08 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,8 @@ // Declare your function first +function add (a,b){ + return a+b; +} // Call the function and assign to a variable `sum` -console.log(sum); +console.log(sum=add(13,124)); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..02350d6b4 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 (a,b){ + return "Hello, my name is "+a+" and I'm "+b+" 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..8a5d47274 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 convertToUpperCase (name){ + return name.toUpperCase() +} +function shoutyGreeting (mentor){ + let upperCase=convertToUpperCase(mentor); + let greeting="Hello "+upperCase + return greeting; + } + + +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..086a91911 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +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"; + 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; } /* From 7c33084662d18b6a368416f7a288ab5fe15649e7 Mon Sep 17 00:00:00 2001 From: Bahare Date: Mon, 21 Nov 2022 19:11:10 +0000 Subject: [PATCH 2/4] Mandatory Done --- mandatory/1-syntax-errors.js | 14 ++++++++------ mandatory/2-logic-error.js | 8 ++++---- mandatory/3-function-output.js | 5 ++++- mandatory/4-tax.js | 9 +++++++-- package.json | 12 +++++++++--- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 086a91911..0c2dca332 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; + let total = a + b; - return "The total is"+total; + return "The total is " +total; } /* @@ -19,7 +20,8 @@ function getTotal(a, b) { 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 +To run the tests for just this one file, type ` +` into your terminal (Reminder: You must have run `npm install` one time before this will work!) =================================================== diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..e94a32c4a 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,16 @@ // 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..15faab0f8 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,16 +1,19 @@ // Add comments to explain what this function does. You're meant to use Google! +//Math.random() returns a random number between 0 (inclusive), and 1 (exclusive),so this function return a (number between 0 and 1 )*10 function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +//The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array..so this function get 2 string and return a new string from combining them function combine2Words(word1, word2) { - return word1.concat(word2); + return word1.concat(" ",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..1ad98efaf 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(productPrice) { +return productPrice + (0.2 * productPrice) +} /* 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(productPrice) { + + return "£"+(calculateSalesTax(productPrice)).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" + ] } ] }, From 6e3e8ec220b413d38e339213ab431efe38cfa700 Mon Sep 17 00:00:00 2001 From: Bahare Date: Wed, 23 Nov 2022 18:16:17 +0000 Subject: [PATCH 3/4] Extra Done --- extra/1-currency-conversion.js | 8 ++++-- extra/2-piping.js | 17 ++++++----- extra/3-magic-8-ball.js | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..77ab08db9 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 CONVERSION @@ -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(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..17e305f1c 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(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(multiply(add(startingValue, 10), 2)); + /* BETTER PRACTICE */ +//Just for fun,i dont know the better way to write that code:) -let goodCode = +let goodCode =format((startingValue+10)*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/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..c20eae16f 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,8 +45,16 @@ // This should log "The ball has shaken!" // and return the answer. +let answer=""; function shakeBall() { //Write your code in here + + const answers= [["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."]]; + console.log("The ball has shaken!") ; + return answer = answers[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 5)]; } /* @@ -60,7 +68,51 @@ function shakeBall() { */ function checkAnswer(answer) { //Write your code in here + const answers= [["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."]]; + + for (let i=0;i<4;i++){ + for (let j=0;j<5;j++){ + if (answers[i][j]=== answer){ + if (i === 0) { + return "very positive"; + } + else if(i===1){ + return "positive"; + } + else if (i === 2) { + return "negative"; + } + else { + return "very negative"; + } + + } + } + } } + + + + + + + + + + + + + + + + + + + + /* ================================== From 4292feeb9ce9c60fb21c0460fbb43398945967e0 Mon Sep 17 00:00:00 2001 From: Bahare Date: Wed, 23 Nov 2022 19:04:29 +0000 Subject: [PATCH 4/4] update magic 8 ball --- extra/3-magic-8-ball.js | 148 ++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 59 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index c20eae16f..ab314abb8 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,7 +1,7 @@ /** Let's peer into the future using a Magic 8 Ball! - https://en.wikipedia.org/wiki/Magic_8-Ball + https://en.wikipedia.org/wiki/Magic_8-Ball There are a few steps to being able view the future though: * Ask a question @@ -14,7 +14,7 @@ Below are the possible answers: - ## Very positive + ## Very positive It is certain. It is decidedly so. Without a doubt. @@ -45,19 +45,46 @@ // This should log "The ball has shaken!" // and return the answer. -let answer=""; +let answer = ""; function shakeBall() { //Write your code in here - - const answers= [["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."]]; - console.log("The ball has shaken!") ; - return answer = answers[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 5)]; + + const answers = [ + [ + "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.", + ], + ]; + console.log("The ball has shaken!"); + return (answer = + answers[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 5)]); } -/* +/* This function should say whether the answer it is given is - very positive - positive @@ -68,53 +95,55 @@ function shakeBall() { */ function checkAnswer(answer) { //Write your code in here - const answers= [["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."]]; - - for (let i=0;i<4;i++){ - for (let j=0;j<5;j++){ - if (answers[i][j]=== answer){ - if (i === 0) { - return "very positive"; - } - else if(i===1){ + const answers = [ + [ + "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.", + ], + ]; + + for (let i = 0; i < 4; i++) { + for (let j = 0; j < 5; j++) { + if (answers[i][j] === answer) { + if (i === 0) { + return "very positive"; + } else if (i === 1) { return "positive"; + } else if (i === 2) { + return "negative"; + } else { + return "very negative"; } - else if (i === 2) { - return "negative"; - } - else { - return "very negative"; - } - } } } } - - - - - - - - - - - - - - - - - - - - -/* +/* ================================== ======= TESTS - DO NOT MODIFY ===== @@ -134,12 +163,11 @@ test("whole magic 8 ball sequence", () => { expect(consoleLogSpy).toHaveBeenCalledTimes(1); expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); - expect(checkAnswer(answer)).toBeOneOf([ - "very positive", - "positive", - "negative", - "very negative", - ]); + expect( + ["very positive", "positive", "negative", "very negative"].includes( + checkAnswer(answer) + ) + ).toEqual(true); }); test("magic 8 ball returns different values each time", () => { @@ -153,7 +181,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" @@ -167,4 +197,4 @@ test("checkAnswer works for `It is decidedly so.`", () => { test("checkAnswer works for `My reply is no.`", () => { expect(checkAnswer("My reply is no.")).toEqual("very negative"); -}); +}); \ No newline at end of file