From 93cd59ca356c04603fcf99869cb89ea801c91c47 Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Fri, 26 Aug 2022 18:07:21 +0100 Subject: [PATCH 1/5] all task done --- exercises/B-hello-world/exercise.js | 2 +- exercises/C-variables/exercise.js | 5 +++++ exercises/D-strings/exercise.js | 2 ++ exercises/E-strings-concatenation/exercise.js | 4 +++- exercises/F-strings-methods/exercise.js | 6 +++++- exercises/F-strings-methods/exercise2.js | 7 +++++-- exercises/G-numbers/exercise.js | 10 ++++++++++ exercises/I-floats/exercise.js | 16 +++++++++++++-- exercises/J-functions/README.md | 2 +- exercises/J-functions/exercise.js | 2 +- exercises/J-functions/exercise2.js | 1 + exercises/K-functions-parameters/exercise.js | 3 ++- exercises/K-functions-parameters/exercise2.js | 4 +++- exercises/K-functions-parameters/exercise3.js | 3 +++ exercises/K-functions-parameters/exercise4.js | 5 ++++- exercises/K-functions-parameters/exercise5.js | 5 ++++- exercises/L-functions-nested/exercise.js | 20 +++++++++++++++++++ mandatory/1-syntax-errors.js | 12 +++++------ mandatory/2-logic-error.js | 11 +++++----- mandatory/3-function-output.js | 4 ++++ mandatory/4-tax.js | 11 ++++++++-- 21 files changed, 109 insertions(+), 26 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..cd4cc12ac 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1 @@ -console.log("Hello world"); +console.log( 1 ,"Hello azadeh", 12.4, 'bye'); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..82bbbb600 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,8 @@ // Start by creating a variable `greeting` +let greeting = "Hello Azadeh" + + +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..73c4745bf 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` +const 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..9e8e55914 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` - +const greetingStart = 'hello , I am '; +const myName = 'Azadeh' +const message= greetingStart + myName; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..abfc45277 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` - +const myName="Azadeh"; +const intro = "My name is"; +const message = intro + " " + 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..1fc4b2747 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,6 @@ -const name = " Daniel "; - +const name = " Daniel ".trim(); +const intro = "My name is"; +const message = intro + " " + name + " "+ "and my name is "+ name.length + " characters long "; console.log(message); + + diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..06711a5d5 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,11 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +const numberOfMentors = 5; +const numberOfStudents = 30; +const mentorsNum = "Number of mentors: "+ numberOfMentors; +const studentNum = "Number of students: " + numberOfStudents; +const totalNum = "Total number of students and mentors:" + (numberOfMentors + numberOfStudents); + + +console.log(mentorsNum); +console.log(studentNum); +console.log (totalNum); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..57faebe0e 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,14 @@ -var numberOfStudents = 15; -var numberOfMentors = 8; +const numberOfStudents = 15; +const numberOfMentors = 8; +const totalNumber = numberOfMentors+numberOfStudents; + + +const percentageOfMentors = (numberOfMentors/totalNumber)*100 ; +const percentageOfStudents = (numberOfStudents/totalNumber)*100 ; +const roundPercentageOfMentors = "Percentage Mentors:" + Math.round( percentageOfMentors) +"%"; +const roundPercentageOfStudents = "Percentage students:" + Math.round( percentageOfStudents)+"%"; + +console.log( roundPercentageOfMentors); +console.log(roundPercentageOfStudents); + + diff --git a/exercises/J-functions/README.md b/exercises/J-functions/README.md index 22105ca2a..142736657 100644 --- a/exercises/J-functions/README.md +++ b/exercises/J-functions/README.md @@ -13,7 +13,7 @@ a) _call_ it with an input and b) assign the returned value to a variable ```js -var result = double(2); +var result = double(); console.log(result); // 4 ``` diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..e7278f7bd 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,5 +1,5 @@ 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..468d45596 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..7b3b584bf 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,5 +1,6 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply( a , b ) { + return a * b; // Calculate the result of the function and return it } diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..e366146b2 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); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..fbfbf0c9b 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..7b3e0e59a 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,8 @@ // Declare your function first - +function adds(a , b){ + return a + b; +} +var sum = adds(13 , 124); // Call the function and assign to a variable `sum` console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..f9cc2ecae 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 (name,age){ + return "Hello, my name is " + name + " and I am " + age +" 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..06d00071f 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,23 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + function upperCaseName ( mentor ){ + return mentor.toUpperCase( ) + } + function greeting( hi,name){ + const mentor = upperCaseName(name) + return hi.concat( mentor) + } + const printName1= greeting("Hello ",mentor1); + const printName2= greeting("Hello " ,mentor2); + const printName3= greeting("Hello " ,mentor3); + const printName4= greeting("Hello " ,mentor4); + const printName5= greeting("Hello " ,mentor5); + console.log (printName1); + console.log (printName2); + console.log (printName3); + console.log (printName4); + console.log (printName5); + + + \ No newline at end of file diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..4957a5f12 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"; - +function introduceMe(name, age){ + return "Hello, my name is " + name + " and I am " + age + " years old"; +} function getTotal(a, b) { - total = a ++ b; + const total = a + b; - return "The total is total"; + return "The total is " + total; } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..a566ec60f 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 wordtrim(str) { + return str.trim(str) +} function trimWord(word) { - return wordtrim(); + return wordtrim(word); } 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..8be755201 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,11 @@ // 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 in the range 0 to less than 1 (inclusive of 0, but not 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. function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +// concatanetion two words to each other function combine2Words(word1, word2) { return word1.concat(word2); } @@ -11,6 +13,8 @@ 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 + return `${firstWord} ${secondWord} ${thirdWord}` } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..91b9ebfe9 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(price) { + const tax = price *20/100 + return tax+ price +} /* CURRENCY FORMATTING @@ -17,7 +20,11 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) { + const tax = price *20/100 + const total= tax+ price + return `£${total.toFixed(2)}` +} /* =================================================== From b4496e2f961ed03b73500369291e58408a65b73c Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Fri, 26 Aug 2022 21:59:07 +0100 Subject: [PATCH 2/5] extra- currencyconvert --- extra/1-currency-conversion.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..a33504be2 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( pound ) { + return pound * 1.4; +} /* CURRENCY CONVERSION @@ -15,7 +17,10 @@ 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(pound) { + let brl = pound * 5.7 ; + return Number( ( brl - (brl * (1 / 100)) ).toFixed(2) ); +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From e6985b1843564fc2851362cea882d1260f4ac83c Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Sun, 18 Sep 2022 17:40:56 +0100 Subject: [PATCH 3/5] .toBeOneOf is not a function / --- extra/3-magic-8-ball.js | 71 +++++++++++++++++++++++++++++++++++++++-- mandatory/4-tax.js | 2 +- package.json | 15 +++++++-- 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..dd0d998b9 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,6 +46,33 @@ // This should log "The ball has shaken!" // and return the answer. function shakeBall() { + let questions = [ + '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 questions[Math.floor(Math.random() * questions.length)]; //Write your code in here } @@ -56,9 +83,47 @@ function shakeBall() { - negative - very negative + This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(answer) { +function checkAnswer( answer ) { + let veryPositive = [ + 'It is certain.', + 'It is decidedly so.', + 'Without a doubt.', + 'Yes - definitely.', + 'You may rely on it.' + ]; + let positive =[ + 'As I see it, yes.', + 'Most likely.', + 'Outlook good.', + 'Yes.', + 'Signs point to yes.', + ]; + let negative = [ + 'Reply hazy, try again.', + 'Ask again later.', + 'Better not tell you now.', + 'Cannot predict now.', + 'Concentrate and ask again.', + ]; + let veryNegative = [ + "Don't count on it." , + 'My reply is no.', + 'My sources say no.', + 'Outlook not so good.', + 'Very doubtful.', + ]; + if( veryPositive.includes( answer )){ + return "very positive"; + }else if( positive.includes( answer )){ + return "positive"; + }else if ( negative.includes( answer )){ + return "negative"; + }else if( veryNegative.includes( answer )){ + return "very negative"; + } //Write your code in here } @@ -76,12 +141,12 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 3-m test("whole magic 8 ball sequence", () => { const consoleLogSpy = jest.spyOn(global.console, "log"); const answer = shakeBall(); - + expect(typeof answer).toEqual("string"); expect(consoleLogSpy).toHaveBeenCalledTimes(1); expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); - + expect(checkAnswer(answer)).toBeOneOf([ "very positive", "positive", diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index 91b9ebfe9..1e3d1308b 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -7,7 +7,7 @@ function calculateSalesTax(price) { const tax = price *20/100 - return tax+ price + return tax + price } /* diff --git a/package.json b/package.json index 93e0861e3..ec1719365 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" + ] } ] }, @@ -30,5 +36,8 @@ "devDependencies": { "jest": "^26.6.3", "jest-extended": "^0.11.5" + }, + "dependencies": { + "expect": "^29.0.3" } } From d971d4a91e23b8223a2e622ea5c6f10ae10fffca Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Sun, 18 Sep 2022 19:40:14 +0100 Subject: [PATCH 4/5] piping -complate --- extra/2-piping.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/extra/2-piping.js b/extra/2-piping.js index b4f8c4c1b..32ac774c9 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,28 @@ the final result to the variable goodCode */ -function add() { - +function add( a ,b ) { + return a + b; } -function multiply() { - +function multiply(a , b) { +return a * b; } -function format() { - -} +function format( number) { +return `£${number}`; +//"# "+number; +} const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = "£"+((startingValue+10 )*2); /* BETTER PRACTICE */ -let goodCode = +let goodCode = format( multiply( add( 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. From 0e873aec7457fd250abc9429b431999b793e351d Mon Sep 17 00:00:00 2001 From: azadehroshan <105980239+azadehroshan@users.noreply.github.com> Date: Sun, 18 Sep 2022 20:46:15 +0100 Subject: [PATCH 5/5] magic-8-balls-complate --- extra/3-magic-8-ball.js | 114 ++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 68 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index dd0d998b9..e86e50516 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -43,37 +43,50 @@ Very doubtful. */ +// This should log "The ball has shaken!" +// and return the answer. + +const veryNegativeAnswers = [ + "Don't count on it.", + 'My reply is no.', + 'My sources say no.', + 'Outlook not so good.', + 'Very doubtful.', +]; +const negativeAnswers = [ + 'Reply hazy, try again.', + 'Ask again later.', + 'Better not tell you now.', + 'Cannot predict now.', + 'Concentrate and ask again.', +]; +const positiveAnswers = [ + 'As I see it, yes.', + 'Most likely.', + 'Outlook good.', + 'Yes.', + 'Signs point to yes.', +]; +const veryPositiveAnswers = [ + 'You may rely on it', + 'You may rely on it', + 'It is decidedly so.', + 'It is certain.', +]; +const possibleAnswers = veryNegativeAnswers.concat( + negativeAnswers, + positiveAnswers, + veryPositiveAnswers +); // This should log "The ball has shaken!" // and return the answer. function shakeBall() { - let questions = [ - '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 questions[Math.floor(Math.random() * questions.length)]; //Write your code in here + const randomIndex = Math.floor(Math.random() * possibleAnswers.length); + const randomAnswer = possibleAnswers[randomIndex]; + const message = 'The ball has shaken!'; + console.log(message); + return randomAnswer; } /* @@ -82,49 +95,14 @@ function shakeBall() { - positive - negative - very negative - - This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer( answer ) { - let veryPositive = [ - 'It is certain.', - 'It is decidedly so.', - 'Without a doubt.', - 'Yes - definitely.', - 'You may rely on it.' - ]; - let positive =[ - 'As I see it, yes.', - 'Most likely.', - 'Outlook good.', - 'Yes.', - 'Signs point to yes.', - ]; - let negative = [ - 'Reply hazy, try again.', - 'Ask again later.', - 'Better not tell you now.', - 'Cannot predict now.', - 'Concentrate and ask again.', - ]; - let veryNegative = [ - "Don't count on it." , - 'My reply is no.', - 'My sources say no.', - 'Outlook not so good.', - 'Very doubtful.', - ]; - if( veryPositive.includes( answer )){ - return "very positive"; - }else if( positive.includes( answer )){ - return "positive"; - }else if ( negative.includes( answer )){ - return "negative"; - }else if( veryNegative.includes( answer )){ - return "very negative"; - } - //Write your code in here +function checkAnswer(answer) { + const answerIndex = possibleAnswers.indexOf(answer); + if (answerIndex >= 15) return 'very positive'; + if (answerIndex >= 10) return 'positive'; + if (answerIndex >= 5) return 'negative'; + return 'very negative'; } /*