diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 696fd85..c7da2f1 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,10 +1,8 @@ # Your Details -Your Name: -Your City: +Your Name:zubeda +Your City:birminghan Your Slack Name: - # Homework Details - Module: Week: diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index fb756b6..1d06a2d 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -1,17 +1,23 @@ // 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 getRemainder(a, b) { + function getTotal(a, b) { total = a ++ b; + // Use string interpolation here - return "The total is %{total}" + let Reminder=a%b; + return `The remainder is ${a%b}`; } /* ======= TESTS - DO NOT MODIFY ===== @@ -24,6 +30,10 @@ const util = require('util'); function test(test_name, actual, expected) { let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; if (actual === expected) { status = "PASSED"; } else { @@ -33,6 +43,10 @@ function test(test_name, actual, expected) { console.log(`${test_name}: ${status}`); } +test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13); +test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old"); +test("fixed getRemainder function", getRemainder(23,5) === "The remainder is 3"); +======= test("fixed addNumbers function - case 1", addNumbers(3, 4, 6), 13); test("fixed introduceMe function", introduceMe("Sonjide", 27), "Hello, my name is Sonjide and I am 27 years old"); test("fixed getTotal function", getTotal(23, 5), "The total is 28"); diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index c844460..8d9cce6 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-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 getWordLength(word) { - return "word".length() + return word.length; } function multiply(a, b, c) { - a * b * c; - return; + + return a * b * c; } /* ======= TESTS - DO NOT MODIFY ===== @@ -19,6 +19,20 @@ There are some Tests in this file that will help you work out if your code is wo To run these tests type `node 2-logic-error` into your terminal */ +function test(test_name, expr) { + let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; + } + + console.log(`${test_name}: ${status}`); +} + +test("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture"); +test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25); +test("fixed multiply function", multiply(2,3,6) === 36); const util = require('util'); function test(test_name, actual, expected) { diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index c8e8577..6f2301b 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,14 +1,17 @@ // Add comments to explain what this function does. You're meant to use Google! -function getNumber() { +function getNumber() {//MAth.random()*10 return random numbers between 1 and 10 return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! -function s(w1, w2) { +function s(w1, w2) {//w1.concat(w2) return a new string containing the text of joined string return w1.concat(w2); } function concatenate(firstWord, secondWord, thirdWord) { + + console.log(firstWord.concat(" ").concat(secondWord).concat(" ").concat(thirdWord)); + return firstWord.concat(" ").concat(secondWord).concat(" ").concat(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. } @@ -36,11 +39,13 @@ test( concatenate('code', 'your', 'future'), "code your future" ); + test( "concatenate function - case 2 works", concatenate('I', 'like', 'pizza'), "I like pizza" ); + test( "concatenate function - case 3 works", concatenate('I', 'am', 13), diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 79db540..72b37d1 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -5,7 +5,11 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(priceOfProduct) { + + let salesTax=(20/100*priceOfProduct)+priceOfProduct; + return salesTax; +} /* CURRENCY FORMATTING @@ -17,6 +21,12 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ +function formatCurrency(a) { +var price=calculateSalesTax(a); + +return `£${price.toFixed(2)}`; +} +======= function addTaxAndFormatCurrency() {} /* ======= TESTS - DO NOT MODIFY ===== diff --git a/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 5f06097..3b52fcf 100644 --- a/week-1/3-extra/1-currency-conversion.js +++ b/week-1/3-extra/1-currency-conversion.js @@ -5,7 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(price) { + let convertToUs=1.4*price;; + return convertToUs; +} /* CURRENCY FORMATTING @@ -15,7 +18,12 @@ 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) { + let percentOfPrice=(1/100)*(5.7*price); + let convertToBrazil=5.7*price; + let total=percentOfPrice+convertToBrazil; + return total; +} /* ======= 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..9750923 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,26 +16,27 @@ 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 +const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = format((startingValue+10)*2);//no use of variables /* BETTER PRACTICE */ -let goodCode = +let goodCode = badCode;//fisrt put result in variable and the assign to 2nd variable /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -43,6 +44,24 @@ There are some Tests in this file that will help you work out if your code is wo To run these tests type `node 2-piping.js` into your terminal */ +function test(test_name, expr) { + let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; + } + + console.log(`${test_name}: ${status}`); +} + +test('add function - case 1 works', add(1,3) === 4); +test('add function - case 2 works', add(2.4,5.3) === 7.7); +test('multiply function works', multiply(2,3) === 6); +test('format function works', format(16) === "£16"); +test('badCode variable correctly assigned', badCode === "£24"); +test('goodCode variable correctly assigned', goodCode === "£24"); +======= const util = require('util'); function test(test_name, actual, expected) { diff --git a/week-1/3-extra/3-magic-8-ball.js b/week-1/3-extra/3-magic-8-ball.js index edb75fe..f46c5f9 100644 --- a/week-1/3-extra/3-magic-8-ball.js +++ b/week-1/3-extra/3-magic-8-ball.js @@ -35,7 +35,7 @@ Better not tell you now. Cannot predict now. Concentrate and ask again. -## Very negative +## Negative Don't count on it. My reply is no. My sources say no. @@ -46,6 +46,18 @@ Very doubtful. // This should log "The ball has shaken!" // and return the answer. function shakeBall() { + + logged="The ball has shaken!"; + answers=["Very positive","Positive","Negative","Negative"] +// + //answers[Math.floor(Math.random()*answers.length)] +//console.log(Math.floor(Math.random()*4)); + answer=Math.floor(Math.random()*answers.length) + return answer.toString(); +} +//answers[Math.floor(Math.random()*answers.length)] +// The answer should come from shaking the ball +//let answer; } // This function should say whether the answer it is given is @@ -53,6 +65,55 @@ function shakeBall() { // - positive // - negative // - very negative +function checkAnswer() { + // console.log("ans from main"+answer); + let subAns; + +//let no=shakeBall(); +//console.log("answer again from checkS"+no); +if(answer==0){ + // "); + subAns=["It is certain." + ,"It is decidedly so." + ,"Without a doubt" + ,"Yes - definitely." + ,"You may rely on it."]; + console.log("very positive"); + console.log(subAns[Math.floor(Math.random()*subAns.length)]); + return "very positive"; +} +if(answer==1){ + subAns=["As I see it, yes." + ,"Most likely." + ,"Outlook good." + ,"Yes." + ,"Signs point to yes."]; + console.log("positive"); + console.log(subAns[Math.floor(Math.random()*subAns.length)]); + return "positive"; +} +if(answer==2){ + subAns=["Reply hazy, try again." + ,"Ask again later." + ,"Better not tell you now." + ,"Cannot predict now." + ,"Concentrate and ask again."]; + console.log("negative"); + console.log(subAns[Math.floor(Math.random()*subAns.length)]); + return "negative"; +} +if(answer==3){ + subAns=["Don't count on it." + ,"My reply is no." + ,"My sources say no." + ,"Outlook not so good." + ,"Very doubtful."]; + console.log("very nagative"); + console.log(subAns[Math.floor(Math.random()*subAns.length)]); + return "very negative"; +} + +======= // This function should expect to be called with any value which was returned by the shakeBall function. function checkAnswer(answer) { } @@ -89,6 +150,9 @@ function testAll() { `shakeBall logs "The ball has shaken!"`, logged === "The ball has shaken!" ); + + test(`shakeBall returns an string answer"`, typeof answer === "string"); + test(`shakeBall returns an string answer`, typeof answer === "string"); test( diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..da18d4d 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -3,7 +3,14 @@ Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters. */ + // write your code here +function findLongNameThatStartsWithA(names){ + let output=names.find(function(name){ + return name.charAt(0)==='A' && name.length>7; + }) + return output; +} var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; @@ -13,3 +20,4 @@ console.log(longNameThatStartsWithA); /* EXPECTED OUTPUT */ // "Alexandra" + diff --git a/week-3/1-exercises/B-array-some/exercise.js b/week-3/1-exercises/B-array-some/exercise.js index 965c052..8f51db6 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -6,12 +6,21 @@ - Do not edit any of the existing code */ -var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; +var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; +let test=pairsByIndex.some(function(value){ + return value===null; +}) +if(test){ + console.log("array contain null value"); + process.exit(1) +} +else{ // If there is a null value in the array exit the program with the error code // https://nodejs.org/api/process.html#process_process_exit_code // process.exit(1); + var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; @@ -22,3 +31,4 @@ var pairs = pairsByIndex.map(function(indexes) { }); console.log(pairs); +} \ No newline at end of file diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..1cd95e2 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -5,7 +5,9 @@ var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; var group = ["Austine", "Dany", "Swathi", "Daniel"]; -var groupIsOnlyStudents; // complete this statement +var groupIsOnlyStudents=group.every(function(name){ + return students.includes(name); +}); // complete this statement if (groupIsOnlyStudents) { console.log("The group contains only students"); diff --git a/week-3/1-exercises/D-array-filter/exercise.js b/week-3/1-exercises/D-array-filter/exercise.js index 6e32cc6..936c295 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -1,6 +1,7 @@ /* You are given a program that logs pairings between mentors and students - It fails because the array `pairsById` can contain different values that break the program + It fails because the array `pairsById` can contain different values that + break the program It is decided that array items which are not pairs should be filtered out - Finish the statement on line 11 to produce an array with valid content - Do not edit any of the existing code @@ -8,7 +9,9 @@ var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -var pairsByIndex; // Complete this statement +var pairsByIndex=pairsByIndexRaw.filter(function(value){ + return Array.isArray(value) && value.length>1; +}); // Complete this statement var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/week-3/1-exercises/E-array-map/exercise.js b/week-3/1-exercises/E-array-map/exercise.js index 2835e92..379381e 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -2,4 +2,12 @@ // Write multiple solutions using different syntax (as shown in the README) var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; - +var brandNewArray; +brandNewArray=numbers.map(function(num){ + return num*100; +}) +brandNewArray=numbers.map(num=>{ + return num*100; +}) +brandNewArray=numbers.map(num=>num*100); +console.log(brandNewArray); diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..05d00b6 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -8,7 +8,21 @@ */ var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; - +var output=arr.forEach(function(num,index){ + if(num%3===0 && num%5===0){ + console.log("FizzBuzz"); + } + else if(num%5===0){ + console.log("Buzz"); + } + else if(num%3===0){ + console.log("Fizz"); + } + else{ + console.log(num); + } +}) +console.log(output); /* EXPECTED OUTPUT */ /* diff --git a/week-3/1-exercises/G-array-methods/exercise.js b/week-3/1-exercises/G-array-methods/exercise.js index 44e9c80..5dc3c88 100644 --- a/week-3/1-exercises/G-array-methods/exercise.js +++ b/week-3/1-exercises/G-array-methods/exercise.js @@ -4,7 +4,7 @@ */ var numbers = [3, 2, 1]; -var sortedNumbers; // complete this statement +var sortedNumbers=numbers.sort(); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/G-array-methods/exercise2.js b/week-3/1-exercises/G-array-methods/exercise2.js index 3dd24a1..a39171f 100644 --- a/week-3/1-exercises/G-array-methods/exercise2.js +++ b/week-3/1-exercises/G-array-methods/exercise2.js @@ -7,7 +7,7 @@ var mentors = ["Daniel", "Irina", "Rares"]; var students = ["Rukmini", "Abdul", "Austine", "Swathi"]; -var everyone; // complete this statement +var everyone=mentors.concat(students);// complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise.js b/week-3/1-exercises/H-array-methods-2/exercise.js index d36303b..286f892 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise.js +++ b/week-3/1-exercises/H-array-methods-2/exercise.js @@ -15,8 +15,8 @@ var everyone = [ "Swathi" ]; -var firstFive; // complete this statement -var lastFive; // complete this statement +var firstFive=everyone.slice(0,5); // complete this statement +var lastFive=everyone.slice(-5); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise2.js b/week-3/1-exercises/H-array-methods-2/exercise2.js index b7be576..e347306 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise2.js +++ b/week-3/1-exercises/H-array-methods-2/exercise2.js @@ -6,8 +6,11 @@ For example, capitailise("hello") should return "Hello" Tip: use the string method .split() and the array method .join() */ +//return name.split("")[0].toUpperCase() + name.slice(1); +function capitalise(str) { + return str.split("")[0].toUpperCase().concat(str.slice(1)); -function capitalise(str) {} +} /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise3.js b/week-3/1-exercises/H-array-methods-2/exercise3.js index 82e9dd8..3bb1abd 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise3.js +++ b/week-3/1-exercises/H-array-methods-2/exercise3.js @@ -7,7 +7,7 @@ var ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.some(name=>name.includes(country)); // complete this statement } /* diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index f9d745f..49f9db4 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,8 +9,11 @@ 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(arr) { + var findLevel=arr.find(function(level){ + return parseFloat(level)>19.5 && parseFloat(level)<23.5; + }) + return findLevel; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index aca45ad..8903f6e 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,8 +10,16 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(arr) { + if(arr.every(function(barries){ + return barries==="pink"; + })){ + return "Bush is safe to eat from"; + } + else{ + return "Toxic! Leave bush alone!"; + + } } /* ======= 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..54e79a7 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,8 +8,11 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { - +function colonisers(arr) { + let output=arr.filter(function(names){ + return names.charAt(0)==='A' && names.includes("family"); + }) + return output; } /* ======= 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..7d71aaa 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,8 +7,16 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - +function eligibleStudents(arr) { + var output=arr.filter(function(record){ + + return parseFloat(record[1])>=8; + }).map(function(record){ + return record[0]; + }) + + //console.log(final); + return output; } /* ======= 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..27ce5be 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,7 +7,19 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { +function journeyPlanner(arr,transport) { + let output=arr.filter(function(rout){ + if(rout.includes(transport)){ + //console.log(rout[0]) + return rout; + //return rout; + }; + }).map(function(rout){ + return rout[0]; + }) + + + return output; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -20,6 +32,7 @@ const londonLocations = [ ] const util = require('util'); +const { runInContext } = require('vm'); function test(test_name, actual, expected) { let status; diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index ef4e1c5..4cb9e67 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,8 +4,11 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(arr) { + let output=arr.filter(function(name){ + return name.includes("Lane"); + }) + return output; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 8f9e597..9650fc8 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -23,8 +23,29 @@ PasswordValidationResult= [false, false, false, false, true] */ function validatePasswords(passwords) { - -} + //exclude the duplicate value + for(let i=0;i16) + { + return true; + } + else{ + isValid=false; + } + + +} +//main program send card number to isValidateCard(cardNumber) function check the number +let cardNumber="6666666666666661"; +var isValid=isValidateCard(cardNumber); +if(Boolean(isValid)){ + console.log("credit card number is valid"); +} +else{ + console.log("credit card number is not valid"); +} \ No newline at end of file