diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3f42da0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/week-1/2-mandatory/1-syntax-errors.js" + } + ] +} \ No newline at end of file diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index fb756b6..0b10167 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -1,19 +1,24 @@ // 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; + +function getTotal(a, b) { + total = a + b; // Use string interpolation here - return "The total is %{total}" + return `The total is ${total}` } + /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -22,6 +27,11 @@ To run these tests type `node 1-syntax-errors.js` into your terminal const util = require('util'); + +/* ======= TESTS - DO NOT MODIFY ===== */ + +// To run these tests type `node 1-syntax-errors.js` into your terminal + function test(test_name, actual, expected) { let status; if (actual === expected) { diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index c844460..462cc32 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,23 +1,35 @@ // The syntax for this function is valid but it has an error, find it and fix it. -function trimWord(word) { - return wordtrim(); +function trimWord(Word) { + + return Word.trim(); } +console.log(trimWord(" CodeYourFuture ")); + + function getWordLength(word) { - return "word".length() + + let wordLength = "A wild sentence appeared!"; + return wordLength.length; } + + +console.log(getWordLength()); + function multiply(a, b, c) { - a * b * c; - return; + + return a * b * c; + } +console.log(multiply(2,3,6)); + -/* ======= TESTS - DO NOT MODIFY ===== +/* ======= 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 2-logic-error` into your terminal -*/ +To run these tests type `node 2-logic-error` into your terminal*/ const util = require('util'); diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index c8e8577..495a4dc 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,20 +1,30 @@ -// Add comments to explain what this function does. You're meant to use Google! +// This function round the decimal number to the nearset whole number function getNumber() { return Math.random() * 10; } -// Add comments to explain what this function does. You're meant to use Google! +// The concat() method is used to join two or more strings. 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. +function concatenate(firstword, secondWord, thirdWord) { + + const join = firstword.concat(" "+secondWord +" " + thirdWord); + + + return join; } -/* ======= TESTS - DO NOT MODIFY ===== -There are some Tests in this file that will help you work out if your code is working. +console.log(concatenate("code", "your", "future")); + + + + + +/* ======= 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/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 79db540..ecc42b8 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -5,7 +5,14 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(productPrice) { + saleTax= 20/100; + return productPrice*saleTax+productPrice ; + +} + + +console.log(calculateSalesTax(15)); /* CURRENCY FORMATTING @@ -14,12 +21,27 @@ function calculateSalesTax() {} They must also start with the currency symbol Write a function that adds tax to a number, and then transforms the total into the format £0.00 - Remember that the prices must include the sales tax (hint: you already wrote a function for this!) + Remember that the prices must include the sales tax (hint: you already + wrote a function for this!) */ -function addTaxAndFormatCurrency() {} -/* ======= TESTS - DO NOT MODIFY ===== +function addTaxAndFormatCurrency(productPrice) { + + saleTax= 20/100; + currency= "£" ; + + + salePrice= productPrice* saleTax+ productPrice; + return currency + salePrice.toFixed(2) ; + + } + + + + + +/* ======= 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 4-tax.js` into your terminal */ diff --git a/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 5f06097..31983dc 100644 --- a/week-1/3-extra/1-currency-conversion.js +++ b/week-1/3-extra/1-currency-conversion.js @@ -5,7 +5,12 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(pounds) { + +let exchange = pounds * 1.4 ; +return exchange; + +} /* CURRENCY FORMATTING @@ -15,7 +20,13 @@ 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 currency = (pound* 5.7) * 1.01; + + return currency ; + +} /* ======= 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/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index 067dd0f..b43138f 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,26 +16,34 @@ 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(a) { + + return `£${a}` } 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 */ +let adds= add(startingValue,10); +let multiplies = multiply(adds , 2); + -let goodCode = +let goodCode = format(multiplies); /* ======= 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/week-2/2-mandatory/1-fix-functions.js b/week-2/2-mandatory/1-fix-functions.js index d5dfada..be4c045 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -2,7 +2,7 @@ // Look at the tests and see how you can fix them. function mood() { - let isHappy = true; + let isHappy = false; if (isHappy) { return "I am happy"; @@ -11,8 +11,15 @@ function mood() { } } + function greaterThan10(num) { let isBigEnough; +} +console.log(mood()); + +function greaterThan10() { + let num = 10; + let isBigEnough= num; if (isBigEnough) { return "num is greater than 10"; @@ -21,24 +28,43 @@ function greaterThan10(num) { } } + function sortArray(letters) { - let sortedLetters = letters; + let sortedLetters = letters;} + +console.log(greaterThan10()); + + +function sortArray() { + let letters = ["a", "n", "c", "e", "z", "f"]; + let sortedLetters= ["a", "c", "e", "f", "n", "z"]; return sortedLetters; } + function first5(numbers) { let sliced; +} + +console.log(sortArray()); + + +function first5() { + let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; + let sliced= [1, 2, 3, 4, 5]; return sliced; } +console.log(first5()); -function get3rdIndex(arr) { - let index = 3; - let element; - return element; +function get3rdIndex(array) { + + return array[3]; } +console.log(get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"])); +console.log(get3rdIndex([11, 37, 62, 18, 19, 3, 30])); /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-2/2-mandatory/2-function-creation.js b/week-2/2-mandatory/2-function-creation.js index 8494bfa..e1fc223 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -5,7 +5,17 @@ Write a function that: - removes any forward slashes (/) in the strings - makes the string all lowercase */ -function tidyUpString(strArr) {} + +function tidyUpString(strArr) { +let tidU = strArr.to; + + return; + +} + +console.log(tidyUpString(["/Daniel ", "irina ", " Gordon", "ashleigh "])); + + /* Complete the function to check if the variable `num` satisfies the following requirements: @@ -15,7 +25,18 @@ Complete the function to check if the variable `num` satisfies the following req Tip: use logical operators */ -function validate(num) {} +function validate(num) { + +if (num <=100 && typeof num === "number" && num % 2 == 0) + { + return true; +} +else { + return false; +} +} +console.log(validate(20)); + /* Write a function that returns a copy of the given array arr, but with the element at the given index, index removed. @@ -23,9 +44,14 @@ The function must NOT change the original array, arr. */ function remove(arr, index) { - return; // complete this statement + if (arr.length > index) { + return arr; + } + +// complete this statement } + /* Write a function that: - takes an array of numbers as input @@ -35,11 +61,14 @@ Write a function that: */ function formatPercentage(arr) { + + } + -/* ======= TESTS - DO NOT MODIFY ===== */ +/* ======= TESTS - DO NOT MODIFY ===== */ const util = require('util'); function test(test_name, actual, expected) { @@ -61,6 +90,7 @@ function test(test_name, actual, expected) { console.log(`${test_name}: ${status}`); } + function arraysEqual(a, b) { if (a === b) return true; if (a == null || b == null) return false; @@ -117,5 +147,7 @@ test( "18.1%", "100%", "0.37%" + ] ); + diff --git a/week-2/2-mandatory/3-playing-computer.js b/week-2/2-mandatory/3-playing-computer.js index 0fa7c04..f5179d9 100644 --- a/week-2/2-mandatory/3-playing-computer.js +++ b/week-2/2-mandatory/3-playing-computer.js @@ -28,7 +28,7 @@ const f2 = function(a, b) { console.log(x); console.log(a); -console.log(b); + for (let i = 0; i < 5; ++i) { a = a + 1; @@ -40,3 +40,14 @@ for (let i = 0; i < 5; ++i) { console.log(e); } } + + + +/* My asnwers +1. The error was console.log(b); +2. The console print out 2 6 4 9 6 13 8 ; +3. f1 called three times. +4. f2 called four times. +5. "a" is equal to 6 ; +6. "a" is equal to 7 ; +*/ \ No newline at end of file diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index f9d745f..4022db8 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,7 +9,13 @@ To be safe to land on, a planet needs to have an Oxygen level between 19.5% and Write a function that finds the first safe oxygen level in the array - Oxygen between 19.5% and 23.5% */ -function safeLevels() { +function safeLevels(oxygenL1) { + return oxygenL1.find(planet); +} +function planet(safeToliveOn){ + if(parseFloat(safeToliveOn) >19.5 && parseFloat(safeToliveOn) < 23.5) { + return safeToliveOn + "%" ; + } } diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index aca45ad..bf416b8 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,8 +10,20 @@ Use the tests to confirm which message to return */ -function bushChecker() { +function bushChecker(bushBerryColours) { + let checkResult = bushBerryColours.every(checkColours); + if (checkResult === true){ + return "Bush is safe to eat from"; + } + else { + return "Toxic! Leave bush alone!"; + } +} +function checkColours(berryColour){ + if (berryColour === "pink"){ + return true ; + } } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index 239eddd..cb6aec3 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,9 +8,15 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { - +function colonisers(voyagers) { + var voyagers = voyagers.filter(chosenFamilies); + + return voyagers; } + function chosenFamilies(familyLastName){ + return familyLastName.includes("family") && familyLastName.charAt(0) === "A"; + } + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index d8fe052..9530807 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,8 +7,14 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - +function eligibleStudents(studentsWorks) { + let studentsWhoSitForTest= []; + for(i =0; i < studentsWorks.length; i++){ + if (studentsWorks[i][1] >= 8){ + studentsWhoSitForTest.push(studentsWorks[i][0]) + } + } + return studentsWhoSitForTest ; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 816637d..c0dffbd 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,7 +7,15 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { +function journeyPlanner(londonLocations, typesOfTransports) { + + let availbleLocation=[]; + + for(i=0;i< londonLocations.length; i++ ){ + if(londonLocations[i].includes(typesOfTransports)){ + availbleLocation.push(londonLocations[i][0]); + } + } return availbleLocation; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index ef4e1c5..3d658c4 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,10 +4,15 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(streetNames) { + let streetEndLane= streetNames.filter(lane); + return streetEndLane; } +function lane(input){ +return input.includes("Lane"); + +} /* ======= TESTS - DO NOT MODIFY ===== */ const streetNames = [ diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 8f9e597..800b532 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -22,8 +22,58 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) { +function fiveOrMoreCharacters(passwordString) { + if (passwordString.length >= 5) { + return true; + } else { + return false; + }; +} +function containsUpperCase(password) { + let regex = /[A-Z]/; +return regex.test(password); + +} + +function containsLowerCase(password) { +let regex =/[a-z]/; +return regex.test(password); +} + +function containsNumbers(password) { + let regex =/[0-9]/; + return regex.test(password) +} +function containsSymbols(password) { +let regex =/[!#$%.*&]/; +return regex.test(password); + + +} + +function hasDuplicates(array) { + var valuesSoFar = []; + for (var i = 0; i < array.length; ++i) { + var value = array[i]; + if (valuesSoFar.indexOf(value) !== -1) { + return true; + } + valuesSoFar.push(value); + } + return false; +} + +function validatePasswords(passwords) { + let returnArray = []; + for(let i =0; i < passwords.length; i++) { + if (fiveOrMoreCharacters(passwords[i]) === true && containsUpperCase(passwords[i]) === true && containsLowerCase(passwords[i]) === true && containsNumbers(passwords[i]) + === true && containsSymbols(passwords[i]) === true && hasDuplicates(passwords[i]) === true) { + returnArray.push(true); + } else { + returnArray.push(false); + } + } return returnArray; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/3-extra/3-sorting-algorithm.js b/week-3/3-extra/3-sorting-algorithm.js index 3603942..dbe7eb9 100644 --- a/week-3/3-extra/3-sorting-algorithm.js +++ b/week-3/3-extra/3-sorting-algorithm.js @@ -14,7 +14,15 @@ You don't have to worry about making this algorithm work fast! The idea is to ge "think" like a computer and practice your knowledge of basic JavaScript. */ -function sortAges(arr) {} +function sortAges(arr) { + if (arr !== Number){ + return Number ; + } + if() + + +} + /* ======= TESTS - DO NOT MODIFY ===== */