diff --git a/.DS_Store b/.DS_Store index 4cb8031..2f5873b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/week-1/1-exercises/C-variables/exercise.js b/week-1/1-exercises/C-variables/exercise.js index a6bbb97..eeab4e8 100644 --- a/week-1/1-exercises/C-variables/exercise.js +++ b/week-1/1-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/week-1/1-exercises/D-strings/exercise.js b/week-1/1-exercises/D-strings/exercise.js index 2cffa6a..18092fa 100644 --- a/week-1/1-exercises/D-strings/exercise.js +++ b/week-1/1-exercises/D-strings/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - -console.log(message); +let myString= "I am a string." +let typeOfVariable = typeof(myString); +console.log("I am a " + typeOfVariable + "."); diff --git a/week-1/1-exercises/E-strings-concatenation/exercise.js b/week-1/1-exercises/E-strings-concatenation/exercise.js index 2cffa6a..f6cd820 100644 --- a/week-1/1-exercises/E-strings-concatenation/exercise.js +++ b/week-1/1-exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - -console.log(message); +let greeting= "Hello, my name is "; +let myName= "Ekip"; +console.log(`${greeting}${myName}.`); diff --git a/week-1/1-exercises/F-strings-methods/exercise.js b/week-1/1-exercises/F-strings-methods/exercise.js index 2cffa6a..6eb90bf 100644 --- a/week-1/1-exercises/F-strings-methods/exercise.js +++ b/week-1/1-exercises/F-strings-methods/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` +let myName=" Ekip"; +let lengthOfName= myName.length; -console.log(message); +console.log(`My anme is ${myName} and my name is ${lengthOfName} characters long.`); diff --git a/week-1/1-exercises/F-strings-methods/exercise2.js b/week-1/1-exercises/F-strings-methods/exercise2.js index b4b4694..d846326 100644 --- a/week-1/1-exercises/F-strings-methods/exercise2.js +++ b/week-1/1-exercises/F-strings-methods/exercise2.js @@ -1,3 +1,7 @@ -const name = " Daniel "; +let myName= " Ekip "; +let whiteSpaceDelated= myName.trim(); +console.log(whiteSpaceDelated); +let lengthOfName= myName.length; +let msg= `My name is ${whiteSpaceDelated} and my name is ${lengthOfName} characters long.`; -console.log(message); +console.log(msg.trim()); diff --git a/week-1/1-exercises/G-numbers/exercise.js b/week-1/1-exercises/G-numbers/exercise.js index 49e7bc0..c54d6ca 100644 --- a/week-1/1-exercises/G-numbers/exercise.js +++ b/week-1/1-exercises/G-numbers/exercise.js @@ -1 +1,9 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` + +let numberOfStudents= 24; +let numberOfMentors=18; +let totalNumbersOfAttandents= numberOfStudents + numberOfMentors; + +console.log(`The numbers of students is:${numberOfStudents}`); +console.log(`The numbers of mentors is:${numberOfMentors}`); +console.log("The numbers of mentors and students in total is:" + " " + totalNumbersOfAttandents); \ No newline at end of file diff --git a/week-1/1-exercises/I-floats/exercise.js b/week-1/1-exercises/I-floats/exercise.js index a5bbcd8..be124f7 100644 --- a/week-1/1-exercises/I-floats/exercise.js +++ b/week-1/1-exercises/I-floats/exercise.js @@ -1,2 +1,5 @@ var numberOfStudents = 15; var numberOfMentors = 8; +var totalNumberOfAttandents= numberOfStudents + numberOfMentors; +console.log("Percentage students:" + Math.round((numberOfStudents/totalNumberOfAttandents) * 100)); +console.log("Percentage mentors:" + Math.round((numberOfMentors/totalNumberOfAttandents) * 100)); diff --git a/week-1/1-exercises/J-functions/exercise.js b/week-1/1-exercises/J-functions/exercise.js index 0ae5850..24172cb 100644 --- a/week-1/1-exercises/J-functions/exercise.js +++ b/week-1/1-exercises/J-functions/exercise.js @@ -1,7 +1,10 @@ function halve(number) { // complete the function here + return (number/2); } - var result = halve(12); - console.log(result); +console.log(halve(4)); +console.log(halve(8)); +console.log(halve(14)); + diff --git a/week-1/1-exercises/J-functions/exercise2.js b/week-1/1-exercises/J-functions/exercise2.js index 82ef5e7..7c7281d 100644 --- a/week-1/1-exercises/J-functions/exercise2.js +++ b/week-1/1-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/week-1/1-exercises/K-functions-parameters/exercise.js b/week-1/1-exercises/K-functions-parameters/exercise.js index 8d5db5e..5328e19 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise.js +++ b/week-1/1-exercises/K-functions-parameters/exercise.js @@ -1,8 +1,9 @@ // 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; } - +multiply(); // Assign the result of calling the function the variable `result` var result = multiply(3, 4); diff --git a/week-1/1-exercises/K-functions-parameters/exercise2.js b/week-1/1-exercises/K-functions-parameters/exercise2.js index db7a890..d341bef 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise2.js +++ b/week-1/1-exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,8 @@ // Declare your function first +function divide (a,b){ + return (a/b); +} var result = divide(3, 4); console.log(result); diff --git a/week-1/1-exercises/K-functions-parameters/exercise3.js b/week-1/1-exercises/K-functions-parameters/exercise3.js index 537e9f4..5349429 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise3.js +++ b/week-1/1-exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,8 @@ // Write your function here +function createGreeting (name){ + return "Hello, my name is " + name; +} var greeting = createGreeting("Daniel"); console.log(greeting); diff --git a/week-1/1-exercises/K-functions-parameters/exercise4.js b/week-1/1-exercises/K-functions-parameters/exercise4.js index 7ab4458..1231f26 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise4.js +++ b/week-1/1-exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function addNumbers (a,b){ + return a + b; +} // Call the function and assign to a variable `sum` - +var sum = addNumbers (13,124); console.log(sum); diff --git a/week-1/1-exercises/K-functions-parameters/exercise5.js b/week-1/1-exercises/K-functions-parameters/exercise5.js index 7c5bcd6..44095cd 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise5.js +++ b/week-1/1-exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,7 @@ // Declare your function here - +function createLongGreeting (name,age){ + return `Hello, my name is ${name} and I'm ${age} years old.` +} const greeting = createLongGreeting("Daniel", 30); console.log(greeting); diff --git a/week-1/1-exercises/L-functions-nested/exercise.js b/week-1/1-exercises/L-functions-nested/exercise.js index a5d3774..e6f4cc8 100644 --- a/week-1/1-exercises/L-functions-nested/exercise.js +++ b/week-1/1-exercises/L-functions-nested/exercise.js @@ -1,5 +1,35 @@ +//////////////////////////////////////////////////////////////////////////////// + +// Write a program that displays the percentage of students and mentors in the group. +// The percentage should be rounded to the nearest whole number + +function calculatePercentage (numberOfStudents, numbersOfMentors){ + let percentageStudents= (numberOfStudents/(numberOfStudents+numbersOfMentors)) * 100; + let percentageMentors= (numbersOfMentors/(numbersOfMentors+numberOfStudents)) * 100; + return `Percentage of students is ${Math.round(percentageStudents)}% and percentage of mentors is ${Math.round(percentageMentors)}%.`; +} +calculatePercentage(); + +console.log (calculatePercentage (15,8)); + + + + +/////////////////////////////////////////////////////////////////////////////////// + + + + var mentor1 = "Daniel"; var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + + +var mentorsNames= ["Daniel","Irina", "Mimi","Rob", "Yohannes"]; + + for (var i=0; i < mentorsNames.length; i++){ + console.log("HELLO " + (mentorsNames[i].toUpperCase())); + } + diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index fb756b6..63ce203 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -1,17 +1,19 @@ // 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 getRemainder(a, b) { + total = a % b; // Use string interpolation here - return "The total is %{total}" + return `The remainder is ${total}`; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index c844460..71118b8 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,16 +1,15 @@ // 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 ===== diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index c8e8577..4fa33de 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,16 +1,27 @@ // Add comments to explain what this function does. You're meant to use Google! -function getNumber() { + +/* 'Math.random()' function returns a random number from 0 to 1 (not including 1). + Here that random number will be multiplied with 10 and return out of the function. + So, the random numbers will be between 0 and 9.99999 but not 10. */ + + 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 arrays.This method does +not change the existing arrays, but returns a new array, containing the values +of the joined arrays. */ + 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. + // Write the body of this function to concatenate three words together + // Look at the test case below to understand what to expect in return + return `${firstWord} ${secondWord} ${thirdWord}`; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 93f21dd..40f3894 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -4,8 +4,13 @@ A business requires a program that calculates how much sales tax to charge Sales tax is 20% of the price of the product */ +let priceWithTaxUnit; -function calculateSalesTax() {} +function calculateSalesTax(priceOfOneUnit) { + priceWithTaxUnit = priceOfOneUnit + (priceOfOneUnit * 0.2); + return priceWithTaxUnit; +} +// console.log(calculateSalesTax(15)); /* CURRENCY FORMATTING @@ -17,7 +22,11 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function formatCurrency() {} +function formatCurrency(originalPrice) { + let taxAdjustedPrice = calculateSalesTax(originalPrice); + //console.log("£" + taxAdjustedPrice.toFixed(2)); + return "£" + taxAdjustedPrice.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/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 5f06097..c310770 100644 --- a/week-1/3-extra/1-currency-conversion.js +++ b/week-1/3-extra/1-currency-conversion.js @@ -5,7 +5,11 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(currencyPound) { + let currencyDollar = currencyPound * 1.4; + return currencyDollar; +} +//console.log(convertToUSD(20)); /* CURRENCY FORMATTING @@ -15,7 +19,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(poundCurrency) { + let currencyBrl = poundCurrency * 5.7; + let currencyBrlWithTransactionFee = currencyBrl + (0.01 * currencyBrl); + return currencyBrlWithTransactionFee; +} +//console.log(convertToBRL(40)); /* ======= 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..3f23be0 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,26 +16,41 @@ the final result to the variable goodCode */ -function add() { - +function add (a, b) { + return (a + b); } +add(); -function multiply() { +//console.log(add (3.2, 6.5)); +function multiply(a, b) { + return a * b; } +multiply(); +//console.log(multiply(3,6)); -function format() { - +function format(number1) { + //let formattedNumber = `£${number1}`; + //console.log(`£ ${number1}`); + return `£${number1}`; } +format (); -const startingValue = 2 +const startingValue = 2; +function allTogetherMath() { + return "£" + ((startingValue + 10) * 2); +} // Why can this code be seen as bad practice? Comment your answer. -let badCode = +/* StartingValue has been declared as a const value and before the function, +however we can just declare as an argument in the function */ +let badCode = allTogetherMath() /* BETTER PRACTICE */ - -let goodCode = +function allTogetherGoodPractice (startValue){ + return "£ " + ((startValue + 10) * 2); +} +let goodCode = allTogetherGoodPractice(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/week-1/3-extra/3-magic-8-ball.js b/week-1/3-extra/3-magic-8-ball.js index ed2e8dd..d31f269 100644 --- a/week-1/3-extra/3-magic-8-ball.js +++ b/week-1/3-extra/3-magic-8-ball.js @@ -45,17 +45,59 @@ Very doubtful. // This should log "The ball has shaken!" // and return the answer. -function shakeBall() { + +function shakeBall(answer){ + answer= "The ball has shaken!"; + return answer; } +// console.log(answer); +shakeBall() + +// The answer should come from shaking the ball +let answer= "Shake the ball"; +function shakeBall(answer) { + switch (answer) { + case "Ask a question": + return "Ask a question"; + break; + case "Shake the ball": + return "Shake the ball"; + break; + case "Get an answer": + return "Get an answer"; + break; + case "Decide if it's positive or negative": + return "Decide if it's positive or negative"; + break; + } + return answer; // This function should say whether the answer it is given is // - very positive // - positive // - negative // - very negative -// This function should expect to be called with any value which was returned by the shakeBall function. + function checkAnswer(answer) { + switch (answer) { + case "very positive": + return "very positive"; + break; + case "positive": + return "positive"; + break; + case "negative": + return "negative"; + break; + case "very negative": + return "very negative"; + break; + } + return answer; + //console.log(checkAnswer("very positive")); } +checkAnswer(); +// console.log(checkAnswer("very positive")); /* ======= 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/1-exercises/B-boolean-literals/exercise.js b/week-2/1-exercises/B-boolean-literals/exercise.js index 6c5060f..b5c10f7 100644 --- a/week-2/1-exercises/B-boolean-literals/exercise.js +++ b/week-2/1-exercises/B-boolean-literals/exercise.js @@ -6,7 +6,14 @@ */ var codeYourFutureIsGreat = true; +var mozafarIsCool = true; +var calculationCorrect = true; +var moreThan10Students = true; +/*function selectCYF (selection){ + +} +*/ /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/week-2/1-exercises/C-comparison-operators/exercise.js b/week-2/1-exercises/C-comparison-operators/exercise.js index 58aee1c..e8a62fb 100644 --- a/week-2/1-exercises/C-comparison-operators/exercise.js +++ b/week-2/1-exercises/C-comparison-operators/exercise.js @@ -7,14 +7,14 @@ var studentCount = 16; var mentorCount = 9; -var moreStudentsThanMentors; // finish this statement +var moreStudentsThanMentors = true; // finish this statement var roomMaxCapacity = 25; -var enoughSpaceInRoom; // finish this statement +var enoughSpaceInRoom = true; // finish this statement var personA = "Daniel"; var personB = "Irina"; -var sameName; // finish this statement +var sameName = false; // finish this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/D-predicates/README.md b/week-2/1-exercises/D-predicates/README.md index 00862de..45735b5 100644 --- a/week-2/1-exercises/D-predicates/README.md +++ b/week-2/1-exercises/D-predicates/README.md @@ -1,6 +1,6 @@ **Predicate** is a fancy word for a function that returns a boolean value. -These functions are very useful because they let you test if a value satisifies certain requirements. +These functions are very useful because they let you test if a value satisfies certain requirements. ```js function isNumber(value) { diff --git a/week-2/1-exercises/D-predicates/exercise.js b/week-2/1-exercises/D-predicates/exercise.js index f600521..9d397f9 100644 --- a/week-2/1-exercises/D-predicates/exercise.js +++ b/week-2/1-exercises/D-predicates/exercise.js @@ -7,14 +7,23 @@ // Finish the predicate function to test if the passed number is negative (less than zero) function isNegative(number) { - + if (number >= 0){ + return `${number} is a positive number.` + } else { + return `${number} is a negative number. ` + } } +console.log(isNegative(5)) // Finish the predicate function to test if the passed number is between 0 and 10 function isBetweenZeroAnd10(number) { - + if (number > 0 && number < 10){ + return `${number} is between 0 and 10.` + } else { + return `${number} is not between 0 and 10.` + } } - +console.log(isBetweenZeroAnd10(5)) /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/week-2/1-exercises/E-conditionals/exercise.js b/week-2/1-exercises/E-conditionals/exercise.js index acbaaa8..ebaa1a0 100644 --- a/week-2/1-exercises/E-conditionals/exercise.js +++ b/week-2/1-exercises/E-conditionals/exercise.js @@ -9,6 +9,13 @@ var name = "Daniel"; var danielsRole = "mentor"; +if (danielsRole === "mentor"){ + console.log(`Hi, I'm ${name}, I'm a ${danielsRole}.`); +} +if (danielsRole === "student") { + console.log(`Hi, I'm ${name}, I'm a ${danielsRole}.`); +} + /* EXPECTED RESULT --------------- diff --git a/week-2/1-exercises/F-logical-operators/exercise.js b/week-2/1-exercises/F-logical-operators/exercise.js index a8f2945..978dd89 100644 --- a/week-2/1-exercises/F-logical-operators/exercise.js +++ b/week-2/1-exercises/F-logical-operators/exercise.js @@ -11,14 +11,14 @@ var cssLevel = 4; // Finish the statement to check whether HTML, CSS knowledge are above 5 // (hint: use the comparison operator from before) -var htmlLevelAbove5; -var cssLevelAbove5; +var htmlLevelAbove5 = true; +var cssLevelAbove5 = false; // Finish the next two statement // Use the previous variables and logical operators // Do not "hardcode" the answers -var cssAndHtmlAbove5; -var cssOrHtmlAbove5; +var cssAndHtmlAbove5 = false; +var cssOrHtmlAbove5 = true; /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/F-logical-operators/exercise2.js b/week-2/1-exercises/F-logical-operators/exercise2.js index 6f4199c..4dccc1b 100644 --- a/week-2/1-exercises/F-logical-operators/exercise2.js +++ b/week-2/1-exercises/F-logical-operators/exercise2.js @@ -5,7 +5,38 @@ Update the code so that you get the expected result. */ -function isNegative() {} +function isNegative(number) { + if (number < 0){ + console.log (`Is ${number} a negative number? true`); + } + if (number >= 0) { + console.log (`Is ${number} a negative number? false`); + } +} +isNegative(5) + +function isBetween5and10 (number){ + if (number > 5 && number < 10){ + console.log (`Is ${number} in the range 5-10? false`); + } +} +isBetween5and10 (10); + +function isShortName (name){ + if (name.length < 7){ + console.log (`Is ${name} a short name? true`) + } else{ + console.log (`Is ${name} a short name? false`) + } +} +isShortName ("Daniel"); + +function startsWithD (name){ + if (name === "Daniel"){ + console.log(`Does ${name} start with 'D'?`); + } +} + /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/G-conditionals-2/exercise-1.js b/week-2/1-exercises/G-conditionals-2/exercise-1.js index 54708ef..5616dbb 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-1.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-1.js @@ -7,8 +7,14 @@ */ function negativeOrPositive(number) { - + if (number >= 0){ + return "positive"; + } + if (number < 0){ + return "negative"; + } } +console.log(negativeOrPositive()); /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/G-conditionals-2/exercise-2.js b/week-2/1-exercises/G-conditionals-2/exercise-2.js index 313f3fb..85c6501 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-2.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-2.js @@ -8,8 +8,14 @@ */ function studentPassed(grade) { - + if (grade < 50){ + return "failed"; + } + if (grade >= 50){ + return "passed"; + } } +console.log(studentPassed()); /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/G-conditionals-2/exercise-3.js b/week-2/1-exercises/G-conditionals-2/exercise-3.js index a79cf30..8ddf34f 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-3.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-3.js @@ -9,8 +9,17 @@ */ function calculateGrade(mark) { - + if (mark >= 80){ + return "The grade is 'A' "; + } else if(mark < 80 && mark > 60){ + return "The grade is 'B' "; + } else if (mark < 60 && mark > 50 ){ + return "The grade is 'C' "; + } else { + return "The grade is 'F' "; + } } +console.log (calculateGrade()); /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/G-conditionals-2/exercise-4.js b/week-2/1-exercises/G-conditionals-2/exercise-4.js index bd5bb1e..89a209d 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-4.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-4.js @@ -9,8 +9,13 @@ */ function containsCode(sentence) { - + if (sentence.includes("code")){ + return "true"; + } else { + return "false"; + } } +console.log(containsCode()); /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/H-array-literals/exercise.js b/week-2/1-exercises/H-array-literals/exercise.js index d6dc556..03f36e1 100644 --- a/week-2/1-exercises/H-array-literals/exercise.js +++ b/week-2/1-exercises/H-array-literals/exercise.js @@ -4,8 +4,8 @@ Declare some variables assigned to arrays of values */ -var numbers = []; // add numbers from 1 to 10 into this array -var mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +var numbers = [2, 4, 6, 1, 7, 9]; // add numbers from 1 to 10 into this array +var mentors = ["Shukri", "Tom", "Atanas", "Simon"]; // Create an array with the names of the mentors: Daniel, Irina and Rares /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/I-array-properties/exercise.js b/week-2/1-exercises/I-array-properties/exercise.js index f9aec89..39ecbe9 100644 --- a/week-2/1-exercises/I-array-properties/exercise.js +++ b/week-2/1-exercises/I-array-properties/exercise.js @@ -6,7 +6,7 @@ */ function isEmpty(arr) { - return; // complete this statement + return arr.length; // complete this statement } /* diff --git a/week-2/1-exercises/J-array-get-set/exercise.js b/week-2/1-exercises/J-array-get-set/exercise.js index 3b95694..7b6f34c 100644 --- a/week-2/1-exercises/J-array-get-set/exercise.js +++ b/week-2/1-exercises/J-array-get-set/exercise.js @@ -5,11 +5,11 @@ */ function first(arr) { - return; // complete this statement + return arr.shift(); // complete this statement } function last(arr) { - return; // complete this statement + return arr.pop(); // complete this statement } /* diff --git a/week-2/1-exercises/J-array-get-set/exercises2.js b/week-2/1-exercises/J-array-get-set/exercises2.js index 97f126f..10b284e 100644 --- a/week-2/1-exercises/J-array-get-set/exercises2.js +++ b/week-2/1-exercises/J-array-get-set/exercises2.js @@ -7,6 +7,8 @@ */ var numbers = [1, 2, 3]; // Don't change this array literal declaration +numbers.push(4); +numbers[0]=1; /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/2-mandatory/1-fix-functions.js b/week-2/2-mandatory/1-fix-functions.js index d5dfada..1a537d5 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -1,41 +1,52 @@ // The below functions are syntactically correct but not outputting the right results. // Look at the tests and see how you can fix them. + function mood() { let isHappy = true; - if (isHappy) { - return "I am happy"; - } else { + if (isHappy && true) { + return "I am happy" ; + } + else { return "I am not happy"; } } + +/////////////////////////////////////////////////// function greaterThan10(num) { - let isBigEnough; + let isBigEnough=10; - if (isBigEnough) { + if (isBigEnough < num) { return "num is greater than 10"; } else { return "num is not big enough"; } } -function sortArray(letters) { - let sortedLetters = letters; + +////////////////////////////////////////////////////// +function sortArray() { + let letters = ["a", "n", "c", "e", "z", "f"]; + let sortedLetters= letters.sort(); return sortedLetters; } -function first5(numbers) { - let sliced; + +///////////////////////////////////////////////////// +function first5() { + let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; + let sliced= numbers.slice(0, 5); return sliced; } +////////////////////////////////////////////////////// function get3rdIndex(arr) { let index = 3; - let element; + let element= arr[index]; return element; } diff --git a/week-2/2-mandatory/2-function-creation.js b/week-2/2-mandatory/2-function-creation.js index 8494bfa..398f633 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -1,3 +1,4 @@ +///////////////////////////////////////////////////////////////////////////// /* Write a function that: - takes an array of strings as input @@ -5,8 +6,19 @@ Write a function that: - removes any forward slashes (/) in the strings - makes the string all lowercase */ -function tidyUpString(strArr) {} + +function tidyUpString(strArr) { + let makeTidyUpArray = []; + let a = ''; + for (let i = 0; i < strArr.length; i++) { + a = strArr[i].trim().replace('/', '').toLowerCase(); + makeTidyUpArray.push(a); + } + return makeTidyUpArray; +} + +//////////////////////////////////////////////////////////////////////////////// /* Complete the function to check if the variable `num` satisfies the following requirements: - is a number @@ -15,17 +27,25 @@ Complete the function to check if the variable `num` satisfies the following req Tip: use logical operators */ -function validate(num) {} +function validate(num) { + if (typeof num === "number" && num %2 === 0 && num <= 100 ){ + return true; + } else { + return false; + } +} + +/////////////////////////////////////////////////////////////////////////////// /* Write a function that returns a copy of the given array arr, but with the element at the given index, index removed. The function must NOT change the original array, arr. */ function remove(arr, index) { - return; // complete this statement + return arr.slice(0, index).concat(arr.slice(index+1, arr.length)); // complete this statement } - +///////////////////////////////////////////////////////////////////////////////// /* Write a function that: - takes an array of numbers as input @@ -35,9 +55,21 @@ Write a function that: */ function formatPercentage(arr) { - + + for (let i =0; i < arr.length; i++ ){ + + if (arr[i] > 100){ + arr[i]= 100; + }else{ + arr[i] = Math.round(arr[i]*100)/100; + } + arr[i] += "%"; + } + return arr; } +//////////////////////////////////////////////////////////////////////////////////// + /* ======= TESTS - DO NOT MODIFY ===== */ const util = require('util'); diff --git a/week-2/2-mandatory/3-playing-computer.js b/week-2/2-mandatory/3-playing-computer.js index 0fa7c04..b93643f 100644 --- a/week-2/2-mandatory/3-playing-computer.js +++ b/week-2/2-mandatory/3-playing-computer.js @@ -7,12 +7,21 @@ Answer the following questions: 1. This program throws an error. Why? (If you can't find it, try executing it). + A: We do not need to assign variable b and assign value initially. + 2. Remove the line that throws the error. + A: //console.log(b); + 3. What is printed to the console? + A: 2,6,4,9,6,13,8 4. How many times is "f1" called? + A: 2 times 5. How many times is "f2" called? + A: 3 times 6. What value does the "a" parameter take in the first "f1" call? + A: 7 7. What is the value of the "a" outer variable when "f1" is called for the first time? + A: 7 */ let x = 2; @@ -28,7 +37,7 @@ const f2 = function(a, b) { console.log(x); console.log(a); -console.log(b); +// console.log(b); for (let i = 0; i < 5; ++i) { a = a + 1; @@ -39,4 +48,5 @@ for (let i = 0; i < 5; ++i) { const e = f1(i, a); console.log(e); } + } diff --git a/week-2/2-mandatory/4-sorting-algorithm.js b/week-2/2-mandatory/4-sorting-algorithm.js index 3603942..de57d3c 100644 --- a/week-2/2-mandatory/4-sorting-algorithm.js +++ b/week-2/2-mandatory/4-sorting-algorithm.js @@ -14,7 +14,87 @@ 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) { + let maximum = 0; + for (let i = 0; i < arr.length; i++) { + for (let i = 0; i < arr.length; i++) { + if (typeof arr[i] !== "number") { + arr.splice(i, 1); i--; + } + if (arr[i] > arr[i + 1]) { + let tmp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = tmp; + } + } + } + console.log(arr); + return arr; +} +console.log(sortAges()); + +*/ + +function sortAges(arr) { + let done = false; + while (!done) { + done = true; + for (let i = 0; i < arr.length; i++) { + if (typeof arr[i] !== "number") { + //removes one element from the index i and + // to check the next number, decreases the index -- + arr.splice(i, 1); i--; + } + if (arr[i] > arr[i + 1]) { + done = false; + let tmp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = tmp; + } + } + } + console.log(arr); + return arr; +} + +/* +function sortAges(arr) { + let newArray=[]; + let new_Array=[]; + let index; + for(let i=0;i 7 && name.charAt(0) === "A"; + } + + let longNameThatStartsWithA = names.find (findTheNames); +//var longNameThatStartsWithA = findLongNameThatStartsWithA(names){ + // return names.length > 7 && names.charAt(0) === "A"; +//} console.log(longNameThatStartsWithA); diff --git a/week-3/1-exercises/B-array-some/README.md b/week-3/1-exercises/B-array-some/README.md index ecfcac4..e7e0962 100644 --- a/week-3/1-exercises/B-array-some/README.md +++ b/week-3/1-exercises/B-array-some/README.md @@ -21,7 +21,7 @@ To check your array of numbers, you'd have to run this function against every nu _Searches through an array and returns true if at least one array item satisifies the predicate function you provided._ ```js -var containsNegative = ages.some(isNegative); +var containsNegative = numbers.some(isNegative); console.log(containsNegative); // logs true ``` diff --git a/week-3/1-exercises/B-array-some/exercise.js b/week-3/1-exercises/B-array-some/exercise.js index 965c052..fff9280 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -15,6 +15,14 @@ var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; +function checkTheIndexForNull (index){ + return index === null; +} +var exitProgram = pairsByIndex.some(checkTheIndexForNull); +console.log(exitProgram); +//https://nodejs.org/api/process.html#process_process_exit_code +process.exit(1); + var pairs = pairsByIndex.map(function(indexes) { var student = students[indexes[0]]; var mentor = mentors[indexes[1]]; diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..795b5a6 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -5,7 +5,14 @@ var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; var group = ["Austine", "Dany", "Swathi", "Daniel"]; -var groupIsOnlyStudents; // complete this statement +var groupIsOnlyStudents = group.every(studentsIncludeNames); // complete this statement + +function studentsIncludeNames (name){ + return students.includes(name); +} + +//let checkNames= group.every(groupIsOnlyStudents); + 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..f2ff285 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,12 @@ var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -var pairsByIndex; // Complete this statement +var pairsByIndex = pairsByIndexRaw.filter(filterOut); ; // Complete this statement + +function filterOut(index){ + //return Array.isArray(index) && index.length == 2; + return index instanceof Array && index.length == 2 +} 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..3b247b1 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -3,3 +3,31 @@ var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +function multipleWith100 (number){ + return number *100; +} + +var multiple100NewArray= numbers.map(multipleWith100); +/*01 +var multiple100NewArray =numbers.map(function multipleWith100 (number){ + return number * 100; +}); +*/ + +/*02 +var multiple100NewArray =numbers.map(function (number){ + return number * 100; +}); +*/ + +/*03 +var multiple100NewArray =numbers.map( number => { + return number * 100; +}); +*/ + +/*04 +var multiple100NewArray =numbers.map(number => number * 100); +*/ + +console.log (multiple100NewArray); \ No newline at end of file diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..95967ed 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,28 @@ var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +function sorting3And5Remainer (number){ + if (number%3 === 0 ){ + return "Fizz"; + } if (number%5 === 0){ + return "Buzz"; + } if (number%3 === 0 && number%5 === 0){ + return "FizzBuzz"; + } else{ + return number; + } +} + +function log(number){ + console.log (number); +} + +//let sortedRemainer = + +arr.map(sorting3And5Remainer).forEach(log); + +//console.log (sortedRemainer); + /* 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..3b401c7 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/README.md b/week-3/1-exercises/H-array-methods-2/README.md index 507de31..00d4451 100644 --- a/week-3/1-exercises/H-array-methods-2/README.md +++ b/week-3/1-exercises/H-array-methods-2/README.md @@ -27,7 +27,7 @@ function isAMentor(name) { return mentors.includes(name); } -consooe.log("Is Rukmuni a mentor?"); +console.log("Is Rukmuni a mentor?"); console.log(isAMentor("Rukmini")); // logs false ``` 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..ec3babc 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(2); // 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..4434556 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise2.js +++ b/week-3/1-exercises/H-array-methods-2/exercise2.js @@ -7,7 +7,9 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + return str.charAt(0).toUpperCase() + str.slice(1); +} /* 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..75a6b76 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.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 3c02135..5ab0507 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,15 +9,20 @@ To be safe, they need to land on the first unamed planet that has Oxygen levels Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5% */ -function safeLevels() { +function safeLevels(oxygen) { + return oxygen.find(level => level > "19.5%" && level < "23.5%"); } + + + /* ======= TESTS - DO NOT MODIFY ===== */ const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"] const oxygenLevels2 = ["30.8%", "23.5%", "18.8%", "19.5%", "20.2%", "31.6%"] + function test(test_name, expr) { let status; if (expr) { diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..2cf55ec 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,10 +10,13 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(berry) { + if (berry.some(color => color !== "pink")){ + return "Toxic! Leave bush alone!"; + } return "Bush is safe to eat from"; } + /* ======= TESTS - DO NOT MODIFY ===== */ let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"] diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index f99891a..25ed1b2 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -7,14 +7,30 @@ NOTE: don't include any element that is not a "family". */ - -function colonisers() { - + +function colonisers(item) { + return item.filter(x => x.includes('family') && x[0] === 'A'); } +const voyagers2 = [ + "Adam family", + "Potter family", + "Eric", + "Aldous", + "Button family", + "Jude", + "Carmichael", + "Bunny", + "Asimov", + "Oscar family", + "Avery family", + "Archer family" +]; + +console.log(colonisers(voyagers2)); /* ======= TESTS - DO NOT MODIFY ===== */ -const voyagers = [ + const voyagers = [ "Adam family", "Potter family", "Eric", @@ -27,7 +43,7 @@ const voyagers = [ "Oscar family", "Avery family", "Archer family" -]; +]; function arraysEqual(a, b) { if (a === b) return true; diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index 6424b01..f067969 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,9 +7,31 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { +// function studentsNamesWithGrades (item){ +// let arr =[]; +// arr.push(item[i][1] >= 8); +// return arr; +// } + + +// function eligibleStudents (item){ +// let namesArr=[]; +// item.map(item[i][1] => item[i]); +// } + +function eligibleStudents(item) { + return item.filter(x => x[1] >= 8).map(x => x[0]); } +/* let arr = [ + ["Ahmed", 8], + ["Clement", 10], + ["Elamin", 6], + ["Adam", 7], + ["Tayoa", 11], + ["Nina", 10] + ] +console.log(eligibleStudents(arr)); */ /* ======= TESTS - DO NOT MODIFY ===== */ @@ -20,7 +42,7 @@ const attendances = [ ["Adam", 7], ["Tayoa", 11], ["Nina", 10] -] + ] function arraysEqual(a, b) { if (a === b) return true; diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 53499c3..44c623f 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,8 +7,8 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { - +function journeyPlanner(item, vehicle) { + return item.filter(x => x.includes(vehicle)).map(x => x[0]) } /* ======= 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 eddfe44..5db3d1e 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,8 +4,18 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { +/* function getLanes(item) { + let streetNamesWithLanes=[]; + if (item.includes("Lane")){ + streetNamesWithLanes.push(item); + } else{ + return "Does not include Lane"; + } + return streetNamesWithLanes; +} */ +function getLanes(item) { + return item.filter(x => x.includes('Lane')); } /* ======= 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 57b3d53..e62f316 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -23,7 +23,8 @@ PasswordValidationResult= [false, false, false, false, true] */ function validatePasswords(passwords) { - + let conditions = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + return passwords.map(x => (x.length >= 5 && conditions.some(y => x.includes(y)) && x.toLowerCase() != x && x.toUpperCase() != x && (x.indexOf('!') != -1 || x.indexOf('#') != -1 || x.indexOf('$') != -1 || x.indexOf('%') != -1 || x.indexOf('.') != -1)) ? true : false) } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/3-extra/1-credit-card-validator.js b/week-3/3-extra/1-credit-card-validator.js new file mode 100644 index 0000000..269ee22 --- /dev/null +++ b/week-3/3-extra/1-credit-card-validator.js @@ -0,0 +1,8 @@ + +function validateCardNumber (arr){ + if (arr.length === 16 && arr.forEach( x => x !== string ) && (arr.reduce((a, b) => a +b) > 16) { + + } + +} +