From d0a69e1d0100e583e3e91cd90fdfb63a466b29a1 Mon Sep 17 00:00:00 2001 From: Mehreenaziz <113247845+Mehreenaziz@users.noreply.github.com> Date: Thu, 24 Nov 2022 20:34:16 +0000 Subject: [PATCH 1/4] completed B-L exercises --- .DS_Store | Bin 0 -> 6148 bytes exercises/.DS_Store | Bin 0 -> 6148 bytes exercises/B-hello-world/exercise.js | 6 ++++++ exercises/C-variables/exercise.js | 6 ++++++ exercises/D-strings/exercise.js | 8 ++++++++ exercises/E-strings-concatenation/exercise.js | 5 +++++ exercises/F-strings-methods/exercise.js | 14 ++++++++++++-- exercises/F-strings-methods/exercise2.js | 8 ++++++-- exercises/G-numbers/exercise.js | 8 +++++++- exercises/I-floats/exercise.js | 13 +++++++++++++ exercises/J-functions/exercise.js | 11 ++++++++++- exercises/J-functions/exercise2.js | 11 ++++++++++- exercises/K-functions-parameters/exercise.js | 4 ++-- exercises/K-functions-parameters/exercise2.js | 7 ++++++- 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 | 11 +++++++++++ 18 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 .DS_Store create mode 100644 exercises/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1c1af204234451abb98bf8d98acef0a0f397e9c1 GIT binary patch literal 6148 zcmeHK-AcnS6i&A3T87XIg|g#ErZNQYTlknJ`MBMF=ap)0W3z76t%h~w-^s!s2jfZZ z1)~cbol2R8^L`MXC2=uywvS{s4w5V$se~koA>{Hr$s$>Ja*{>4%JsFwvMqb)G^f)} zyWbL>UU$|K(|)Jj620!hY-U?KyL*Qx!~66hlP{W14u77KErWA-h0cnVJ^7<7mDw$L z=b7^iLSldzAO_Zj0dxLYjdfWtt&P(KT3VR7VFiczr~F4G{(U_?AEv z23>=tM(}`eoeHQ^xp`u6oep+k;#`BJMxD;Mni;-hX6ELF!qx0x7b=`_S0lB=05P!0 zKvfSNtpCT~zyBALXhaMU1OJKvUK{uW50+$V>(b(|)(X&TP!xj!>!_*}RYmG?C9XlFX@ykvVwrO52_rZF3Nbd+!t7SDQy-#8KEn_3 zCH)@W>~26%%CS<=j5PBmvopK)TWdE%L@HV2M?^y+ilB_W0nA^7$60SlFGRM0%FR(x zMV1~@3oEh-4DjCVQGz|YqyoG5Yyal(^Q^6}vdU)2`kq}?W!;R&H>$VQf3Uq1?L@oL zyY$kwX)|jUbuyd3;a@MUt+KU0%U+d5I~^W8v!axtxqoM|yH{yc+4{>0~_8C&y1$t3mYe(UY?m)6dlxW50Tu6}~_xdjZ$* z4bDH_@H(BF%9>Be;u-eo0$hBg1sVE44VCzk4b_Ml@KYmFQ2s~FdL=<&Ko}4PHidz3 zVn(~0^1$+(!hkSvZw&DHprMR$z|NyPI$&H20PG{|1U~l?T%!ZV0XvTvfhb=J^rb4a z7|NF;u0viNu=D84NrmP^g_Tw4P?TF8^J|w*D)uN=7!U@2XMlS@$dK><=Xdx2E=i0q zAPoGU45EP>;$V&w8wd Date: Thu, 24 Nov 2022 22:52:50 +0000 Subject: [PATCH 2/4] mandatory exercises complete --- .DS_Store | Bin 6148 -> 6148 bytes exercises/.DS_Store | Bin 6148 -> 6148 bytes mandatory/1-syntax-errors.js | 16 ++++++++-------- mandatory/2-logic-error.js | 8 ++++---- mandatory/3-function-output.js | 14 +++++++++++--- mandatory/4-tax.js | 12 ++++++++++-- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/.DS_Store b/.DS_Store index 1c1af204234451abb98bf8d98acef0a0f397e9c1..163f99b51aeb61d02e7fa9abfe8996313d447491 100644 GIT binary patch delta 56 zcmZoMXfc@J&&a$nU^gQp^JE^T|I(ZcxeSR6c?>B)vVB!ku~2NHo+2a5#(>?7ixZfc7+EI2W0Kpf#U#c!S&OMr*w{ix!O+62 zR!5=Q(#SwZ!NkmD@;)Zx#nQ}7o7p+|IeB)qu~2NHo+2aj#(>?7jLehYvB+)KVi9B9tiyJjabv?9rp@de{2V}a an*}+(Gf(ChapYhC0!9V~mdz0&YnTDZei70D diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..4fdb3c67e 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +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; + let total = a + b; - return "The total is total"; + return `The total is` + total; } /* @@ -28,13 +30,11 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 1-s test("addNumbers adds numbers correctly", () => { expect(addNumbers(3, 4, 6)).toEqual(13); }); - test("introduceMe function returns the correct string", () => { expect(introduceMe("Sonjide", 27)).toEqual( "Hello, my name is Sonjide and I am 27 years old" ); }); - test("getTotal returns a string describing the total", () => { expect(getTotal(23, 5)).toEqual("The total is 28"); -}); +}); \ No newline at end of file diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..5bfbf3666 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..5063a1e23 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,16 +1,24 @@ // Add comments to explain what this function does. You're meant to use Google! function getRandomNumber() { - return Math.random() * 10; + return Math.random() * 10; } +/* ANSWER: This function gives a random number between 0-10. This is because Math.random() will return a floating point number +between 0-1 which will then be multiplied by 10, which in the end results in a random number between 0-10*/ + + // Add comments to explain what this function does. You're meant to use Google! function combine2Words(word1, word2) { return word1.concat(word2); } +/* ANSWER: this function returns a single word which is the concatenation of word1 and word2. Word2 will be added +to the end of word1*/ + 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. + + const concatWords = firstWord.concat(" ", secondWord, " ", thirdWord); + return concatWords; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..d9706629f 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(pricePreTax) { + let salesTaxPrice = (pricePreTax * 0.2) + pricePreTax; + return salesTaxPrice +} /* CURRENCY FORMATTING @@ -17,7 +20,12 @@ 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 priceIncTax = calculateSalesTax(price); + let priceIncTaxTwoDP = priceIncTax.toFixed(2); + return `£${priceIncTaxTwoDP}`; + +} /* =================================================== From c380418070b5c9478beac2bf765156683ff3bd59 Mon Sep 17 00:00:00 2001 From: Mehreenaziz <113247845+Mehreenaziz@users.noreply.github.com> Date: Fri, 25 Nov 2022 15:53:38 +0000 Subject: [PATCH 3/4] tests passing --- mandatory/1-syntax-errors.js | 2 +- mandatory/2-logic-error.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 4fdb3c67e..7195dbaa2 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -12,7 +12,7 @@ function introduceMe(name, age) { function getTotal(a, b) { let 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 5bfbf3666..ef14abdf9 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -5,7 +5,7 @@ function trimWord(word) { } function getStringLength(word) { - return word.length(); + return word.length; } function multiply(a, b, c) { From 4d12af77f5bba344ce1628c092eb2c1e0c1596cb Mon Sep 17 00:00:00 2001 From: Mehreenaziz <113247845+Mehreenaziz@users.noreply.github.com> Date: Thu, 1 Dec 2022 15:05:53 +0000 Subject: [PATCH 4/4] updates to exercises --- extra/5 | 0 extra/lesson2.js | 167 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 extra/5 create mode 100644 extra/lesson2.js diff --git a/extra/5 b/extra/5 new file mode 100644 index 000000000..e69de29bb diff --git a/extra/lesson2.js b/extra/lesson2.js new file mode 100644 index 000000000..574b74d90 --- /dev/null +++ b/extra/lesson2.js @@ -0,0 +1,167 @@ +// //Create a function called isEven that should +// //take a number as an input +// // return a boolean indicating if the number is even or odd +// // Don’t use console.log inside the function itself +// // Example: isEven(100) should return true and isEven(53) should return false +// // Call your function with different arguments. +// // You don’t need to use an if statement for this exercise (we’ll get to if statements next…) + +// function isEven (x) { +// return x % 2 === 0 +// } + +// console.log(isEven(5)) +// console.log(isEven(8)) + +// // Create a function called moreThanSixGoals that should +// // take 2 numbers as input (the number of goals for each football team) +// // return a boolean value indicating if the total goals is more than 6 +// // Don’t use console.log inside the function. +// // Example: moreThanSixGoals(2,3) should return false and moreThanSixGoals(5,2) should return true +// // Try calling your function with different arguments. +// // Ensure your function doesn't use an if statement for this exercise (we’ll get to if statements next…) + +// function moreThanSixGoals(x, y) { +// return x+y > 6 +// } + +// console.log(moreThanSixGoals(0, 5)) + +// console.log(moreThanSixGoals(10, 1)) + +// console.log(moreThanSixGoals(6, 0)) + +// console.log(moreThanSixGoals(7, 2)) +// - + + + +// Create a function that gives you a message depending on your mood! It should: +// take one input +// return "Good job, you're doing great!" if you pass in "happy" +// return "Every cloud has a silver lining" if you pass in "sad" +// return "Beep beep boop" if you pass in a number +// return "I'm sorry, I'm still learning about feelings!" if you pass in anything else + +// function mood(x) { +// if (x === "happy") { +// return "Good job, you're doing great!" +// } +// else if (x==="sad"){ +// return "Every cloud has a silver lining" +// } +// else if (typeof x==="number"){ +// return "Beep beep boop" +// } +// else { +// return "I'm sorry, I'm still learning about feelings!" +// } +// } + +// console.log(mood("happy")) +// console.log(mood(5)) + + +// If you type the following into Node Console (in order), what would you expect to see? Guess before trying it out. +// let num = 10 +// num > 5 && num < 15 // true +// num < 10 || num === 10 // true +// false || true // t +// !true // false +// let greaterThan5 = num > 5 // +// !greaterThan5 // false +// !(num === 10) //false + + +// In pairs, write a function that checks a username is of an acceptable format for a user type. +// The function must: +// take two parameters: one for the username and one for the user type +// if the username starts with a capital letter and has length between 5 and 10 characters long, +// it must return "Username valid"; otherwise, it must return "Username invalid" +// if the user type is an admin or a manager, all usernames must return "Username valid" + +// function (userName, userType) { +// if userName[0].toUppercase() === +// } + +// let a; +// console.log(a) + +// const a; +// console.log(a); + +// function sayHello() { +// console.log("Hello"); +// //no return statement +// } + +// let a = sayHello(); + + +// console.log(a); + +// Create the following array: +// const fruits = ['banana', 'apple', 'strawberry', 'kiwi', 'fig', 'orange']; +// Using the correct index, get the following values from the array: +// strawberry +// kiwi +// orange +// banana +// Be curious! What if you use an index that doesn’t have a value? What if +//you use a negative index? Or a string instead of a number index? + +// let fuitArray = ["banana", "apple", "strawberry", "kiwi", "fig", "orange"]; + +// console.log(fuitArray[2]); +// console.log(fuitArray[3]); +// console.log(fuitArray[4]); +// console.log(fuitArray[5]); +// console.log(fuitArray[6]); +// console.log(fuitArray[-2]); + + +// We’re writing a ToDo List App. We need a function which: +// Takes an array of tasks (strings) as a parameter +// It should output the last task in the list in uppercase, after the words “Don’t forget to ” +// Hint: How do you make a string uppercase? MDN String +// Test your function: +// lastTask(['do the dishes', 'take out garbage', 'practice coding']); +// //should output: “Don't forget to PRACTICE CODING” + +// + +// function toDoListApp(myArray) { +// let lengthOf = myArray.length; +// return "Don't forget to " + myArray[lengthOf-1] +// } + +// console.log(toDoListApp(lastTask)) + +// Now write a function called randomTask which +// Takes an array of tasks (strings) as a parameter +// Returns a random task from the list (don’t use console.log in the function) +// Hint: Look for help here - MDN Math +// Hint: Google how to generate a random number in JavaScript +// Test your function: +// console.log(randomTask['do the dishes', 'take out garbage', 'practice coding', 'clean house']); +// //should output different tasks each time + +let lastTask = ["do the dishes", "take out garbage", "practice coding"]; + +function randomTask (arrayOfStrings) { + let randomElementNumber = Math.floor(Math.random()* arrayOfStrings.length) + return arrayOfStrings[randomElementNumber] + +} + console.log(randomTask(lastTask)) + + +// It's a simple one-liner: + +// const randomElement = array[Math.floor(Math.random() * array.length)]; +// For example: + +// const months = ["January", "February", "March", "April", "May", "June", "July"]; + +// const random = Math.floor(Math.random() * months.length); +// console.log(random, months[random]); \ No newline at end of file