diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 696fd85..f907535 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,10 +1,10 @@ # Your Details -Your Name: -Your City: -Your Slack Name: +Your Name:Abdennour Hachemi +Your City:Birmingham +Your Slack Name:nouri89 # Homework Details -Module: -Week: +Module:javaScript +Week:3 diff --git a/week-1/1-exercises/B-hello-world/exercise.js b/week-1/1-exercises/B-hello-world/exercise.js index b179ee9..6f2e311 100644 --- a/week-1/1-exercises/B-hello-world/exercise.js +++ b/week-1/1-exercises/B-hello-world/exercise.js @@ -1 +1,3 @@ -console.log("Hello world"); +console.log('Hello world. i have just started learning javaScript.' + 4 + ` testing the new methode over ${24} hours`); + +console.log(1); diff --git a/week-1/1-exercises/C-variables/exercise.js b/week-1/1-exercises/C-variables/exercise.js index a6bbb97..40c6ca0 100644 --- a/week-1/1-exercises/C-variables/exercise.js +++ b/week-1/1-exercises/C-variables/exercise.js @@ -1,3 +1,4 @@ -// Start by creating a variable `greeting` - -console.log(greeting); +let greeting = 'Hello World'; +for (i = 0; i < 3; i++) { + 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..02ec126 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` +let message = 'Learning javaScript is Fun '; +let myMessage = typeof message; -console.log(message); +console.log(`${message} \n${myMessage}`); diff --git a/week-1/1-exercises/E-strings-concatenation/exercise.js b/week-1/1-exercises/E-strings-concatenation/exercise.js index 2cffa6a..ddda811 100644 --- a/week-1/1-exercises/E-strings-concatenation/exercise.js +++ b/week-1/1-exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,5 @@ -// Start by creating a variable `message` + let greeting = "Hello, my name is "; + let myName = "Abdennour"; + let message = greeting + myName; -console.log(message); + console.log(message); diff --git a/week-1/1-exercises/F-strings-methods/exercise.js b/week-1/1-exercises/F-strings-methods/exercise.js index 2cffa6a..997f64b 100644 --- a/week-1/1-exercises/F-strings-methods/exercise.js +++ b/week-1/1-exercises/F-strings-methods/exercise.js @@ -1,3 +1,8 @@ // Start by creating a variable `message` - +var myName = "Abdennour"; +var myNameLowerCase=myName.toLowerCase(); +var myNameLength = myName.length; +var message = + "My Name is "+ myName+" and iss " + myNameLength + " Characters Long."; console.log(message); +console.log(myNameLowerCase); diff --git a/week-1/1-exercises/F-strings-methods/exercise2.js b/week-1/1-exercises/F-strings-methods/exercise2.js index b4b4694..c6eca72 100644 --- a/week-1/1-exercises/F-strings-methods/exercise2.js +++ b/week-1/1-exercises/F-strings-methods/exercise2.js @@ -1,3 +1,6 @@ -const name = " Daniel "; +const name = " Daniel ".trim(); +let nameLength = name.length; +var message = + "My Name is " + name + "and is " + nameLength + " Characters Long."; console.log(message); diff --git a/week-1/1-exercises/G-numbers/exercise.js b/week-1/1-exercises/G-numbers/exercise.js index 49e7bc0..6a25180 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` +var numberOfStudents = 15; +var numberOfMentors = 8; +console.log( + `Number of students: ${numberOfStudents}\nNumber of mentors: ${numberOfMentors}\nTotal number of students and mentors: ${ + numberOfStudents + numberOfMentors + }` +); + diff --git a/week-1/1-exercises/I-floats/exercise.js b/week-1/1-exercises/I-floats/exercise.js index a5bbcd8..d4181f3 100644 --- a/week-1/1-exercises/I-floats/exercise.js +++ b/week-1/1-exercises/I-floats/exercise.js @@ -1,2 +1,20 @@ var numberOfStudents = 15; var numberOfMentors = 8; +/* Using the variables provided in the exercise calculate the percentage of mentors and students in the group + +## Expected result + +``` +Percentage students: 65% +Percentage mentors: 35%*/ +var numberOfStudents = 15; +var numberOfStudents = 15; +var studentsPercentage = Math.round( + (numberOfStudents / (numberOfStudents + numberOfMentors)) * 100 +); +var numberOfMentors = 8; +var mentorsPercentage = Math.round( + (numberOfMentors / (numberOfStudents + numberOfMentors)) * 100 +); +console.log(`Percentage students: ${studentsPercentage}%`); +console.log(`Percentage mentors: ${mentorsPercentage}%`); diff --git a/week-1/1-exercises/J-functions/exercise.js b/week-1/1-exercises/J-functions/exercise.js index 0ae5850..e36df72 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(111)); +console.log(halve(20.5)); diff --git a/week-1/1-exercises/J-functions/exercise2.js b/week-1/1-exercises/J-functions/exercise2.js index 82ef5e7..54ca144 100644 --- a/week-1/1-exercises/J-functions/exercise2.js +++ b/week-1/1-exercises/J-functions/exercise2.js @@ -1,5 +1,5 @@ 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..c618777 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise.js +++ b/week-1/1-exercises/K-functions-parameters/exercise.js @@ -1,5 +1,7 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(a,b) { + var multiple= a*b; + return multiple; // Calculate the result of the function and return it } diff --git a/week-1/1-exercises/K-functions-parameters/exercise2.js b/week-1/1-exercises/K-functions-parameters/exercise2.js index db7a890..5cee723 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise2.js +++ b/week-1/1-exercises/K-functions-parameters/exercise2.js @@ -1,4 +1,8 @@ // Declare your function first +function divide(a,b){ + var division=a/b; + return division; +} var result = divide(3, 4); diff --git a/week-1/1-exercises/K-functions-parameters/exercise3.js b/week-1/1-exercises/K-functions-parameters/exercise3.js index 537e9f4..5fe983d 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise3.js +++ b/week-1/1-exercises/K-functions-parameters/exercise3.js @@ -1,4 +1,7 @@ // Write your function here +function createGreeting(name){ + return `Hello, my name is ${name}` +} var greeting = createGreeting("Daniel"); diff --git a/week-1/1-exercises/K-functions-parameters/exercise4.js b/week-1/1-exercises/K-functions-parameters/exercise4.js index 7ab4458..a17bb1e 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise4.js +++ b/week-1/1-exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,9 @@ // Declare your function first +function addition(num1, num2) { + let result = num1 + num2; + return result; +} // Call the function and assign to a variable `sum` - +let sum = addition(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..64c7d2c 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise5.js +++ b/week-1/1-exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,16 @@ // Declare your function here +/* * Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) + +## Expected result + +``` +Hello, my name is Daniel and I'm 30 years old +``` + */ +function createLongGreeting(string, num) { + let greet = `Hello, my name is ${string} and I'm ${num} years old `; + return greet; +} const greeting = createLongGreeting("Daniel", 30); diff --git a/week-1/1-exercises/L-functions-nested/exercise.js b/week-1/1-exercises/L-functions-nested/exercise.js index a5d3774..a54bfc3 100644 --- a/week-1/1-exercises/L-functions-nested/exercise.js +++ b/week-1/1-exercises/L-functions-nested/exercise.js @@ -3,3 +3,32 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; +var students = 15; +var mentors = 8; +function percentage(calculatePercentage) { + var percent = Math.round((calculatePercentage / (students + mentors)) * 100); + return percent; +} + +function message() { + console.log( + `Percentage Mentors: ${percentage( + students + )}%\nPercentage mentors: ${percentage(mentors)}%` + ); +} +message(); +/* In `exercise2.js` you have been provided with the names of some mentors. Write a program that logs a shouty greeting to each one. +- Your program should include a function that spells their name in uppercase, and a function that creates a shouty greeting. +- Log each greeting to the console. */ +function upperCase(name) { + mentorName = name.toUpperCase(); + return mentorName; +} +mentors = [mentor1, mentor2, mentor3, mentor3, mentor4, mentor5]; +function greeting() { + for (i = 0; i < 5; i++) { + console.log(`HEllO ${upperCase(mentors[i])}`); + } +} +greeting(); diff --git a/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 7f321d9..cd11196 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(AmountToConvert) { + var usDollarEquivlent = AmountToConvert * 1.4; + return usDollarEquivlent; //.toFixed(2); +} /* CURRENCY FORMATTING @@ -16,7 +19,16 @@ function convertToUSD() {} Find a way to add 1% to all currency conversions (think about the DRY principle) */ -function convertToBRL() {} +function convertToBRL(AmountToConvert) { + var realBrazilianEquivlent = AmountToConvert * 5.7; + realBrazilianEquivlent = addOnePercentFee(realBrazilianEquivlent); + return realBrazilianEquivlent; //.toFixed(2); +} +function addOnePercentFee(transactionAmount) { + var fee = transactionAmount * 0.01; + transactionPlusFee = transactionAmount + fee; + return transactionPlusFee; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -25,14 +37,14 @@ To run these tests type `node 1-currency-conversion` into your terminal */ function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; + } + + console.log(`${test_name}: ${status}`); } test("convertToUSD function works", convertToUSD(32) === 44.8); diff --git a/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index 93c0bf7..c93566f 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,26 +16,30 @@ the final result to the variable goodCode */ -function add() { - +function add(x, y) { + let sum = (x * 10 + y * 10) / 10; + return sum; } - -function multiply() { - +console.log(add(2.4, 5.3)); +function multiply(x, y) { + let multiple = x * y; + return multiple; } -function format() { - +function format(number) { + return `£${number}`; } -const startingValue = 2 +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 goodCode = +let goodCode = add(startingValue, 10); +goodCode = multiply(goodCode, 2); +goodCode = format(goodCode); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -44,19 +48,19 @@ 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}`) + 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") \ No newline at end of file +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"); diff --git a/week-2/0-freecodecamp/0-freecodecamp.md b/week-2/0-freecodecamp/0-freecodecamp.md index b21a59a..7a02c44 100644 --- a/week-2/0-freecodecamp/0-freecodecamp.md +++ b/week-2/0-freecodecamp/0-freecodecamp.md @@ -1,4 +1,4 @@ -## FreeCodeCamp +## FreeCodeCamp done If you haven't already, you should complete all of these lessons on FreeCodeCamp - https://www.freecodecamp.org/learn diff --git a/week-2/1-exercises/A-expressions/README.md b/week-2/1-exercises/A-expressions/README.md index 9503b6b..0dadf8b 100644 --- a/week-2/1-exercises/A-expressions/README.md +++ b/week-2/1-exercises/A-expressions/README.md @@ -1,3 +1,4 @@ +/***********************DONE***************************************************************/ In JavaScript there are **expressions** and **statements**. We will use these words frequently to describe code. ### Expression diff --git a/week-2/1-exercises/B-boolean-literals/exercise.js b/week-2/1-exercises/B-boolean-literals/exercise.js index 6c5060f..1d2d6a3 100644 --- a/week-2/1-exercises/B-boolean-literals/exercise.js +++ b/week-2/1-exercises/B-boolean-literals/exercise.js @@ -6,15 +6,18 @@ */ var codeYourFutureIsGreat = true; +var mozafarIsCool = false; +var calculationCorrect = true; +var moreThan10Students = false; /* DO NOT EDIT BELOW THIS LINE --------------------------- */ -console.log("Is Code Your Future great?", codeYourFutureIsGreat); -console.log("Is Mozafar cool?", mozafarIsCool); -console.log("Does 1 + 1 = 2?", calculationCorrect); -console.log("Are there more than 10 students?", moreThan10Students); +console.log('Is Code Your Future great?', codeYourFutureIsGreat); +console.log('Is Mozafar cool?', mozafarIsCool); +console.log('Does 1 + 1 = 2?', calculationCorrect); +console.log('Are there more than 10 students?', moreThan10Students); /* EXPECTED RESULT diff --git a/week-2/1-exercises/C-comparison-operators/exercise.js b/week-2/1-exercises/C-comparison-operators/exercise.js index 58aee1c..959475c 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= studentCount>mentorCount; // finish this statement var roomMaxCapacity = 25; -var enoughSpaceInRoom; // finish this statement +var enoughSpaceInRoom=roomMaxCapacity>=(studentCount+mentorCount); // finish this statement var personA = "Daniel"; var personB = "Irina"; -var sameName; // finish this statement +var sameName=personA===personB; // finish this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/D-predicates/exercise.js b/week-2/1-exercises/D-predicates/exercise.js index f600521..7d0c074 100644 --- a/week-2/1-exercises/D-predicates/exercise.js +++ b/week-2/1-exercises/D-predicates/exercise.js @@ -7,12 +7,13 @@ // Finish the predicate function to test if the passed number is negative (less than zero) function isNegative(number) { + return number<0; } // Finish the predicate function to test if the passed number is between 0 and 10 function isBetweenZeroAnd10(number) { - + return (number >= 0 && number <=10) ; } /* diff --git a/week-2/1-exercises/E-conditionals/exercise.js b/week-2/1-exercises/E-conditionals/exercise.js index acbaaa8..0b76162 100644 --- a/week-2/1-exercises/E-conditionals/exercise.js +++ b/week-2/1-exercises/E-conditionals/exercise.js @@ -6,8 +6,13 @@ If Daniel is a student, print out "Hi, I'm Daniel, I'm a student." */ -var name = "Daniel"; -var danielsRole = "mentor"; +var name = 'Daniel'; +var danielsRole = 'mentor'; +if (danielsRole === 'mentor') { + console.log(`Hi, I'm ${name}, I'm a mentor.`); +} else if (danielsRole === 'stuent') { + 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..4c27024 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 =htmlLevel>5; +var cssLevelAbove5=cssLevel>5; // Finish the next two statement // Use the previous variables and logical operators // Do not "hardcode" the answers -var cssAndHtmlAbove5; -var cssOrHtmlAbove5; +var cssAndHtmlAbove5=htmlLevelAbove5 && cssLevelAbove5 >5; +var cssOrHtmlAbove5=htmlLevelAbove5 || cssLevelAbove5 >5; /* 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..387c946 100644 --- a/week-2/1-exercises/F-logical-operators/exercise2.js +++ b/week-2/1-exercises/F-logical-operators/exercise2.js @@ -5,17 +5,29 @@ Update the code so that you get the expected result. */ -function isNegative() {} - +function isNegative(num) { + return num < 0; +} +function isBetween5and10(num) { + return num >= 0 && num <= 10; +} +function isShortName(name) { + if (name.length <= 6) return true; + else return false; +} +function startsWithD(name) { + if (x.charAt(0) === 'D') return true; + else return false; +} /* DO NOT EDIT BELOW THIS LINE --------------------------- */ -console.log("Is -10 is a negative number?", isNegative(-10)); -console.log("Is 5 a negative number?", isNegative(5)); -console.log("Is 10 in the range 5-10?", isBetween5and10(10)); -console.log("Is Daniel a short name?", isShortName("Daniel")); -console.log("Does Daniel start with 'D'?", startsWithD("Daniel")); +console.log('Is -10 is a negative number?', isNegative(-10)); +console.log('Is 5 a negative number?', isNegative(5)); +console.log('Is 10 in the range 5-10?', isBetween5and10(10)); +console.log('Is Daniel a short name?', isShortName('Daniel')); +console.log("Does Daniel start with 'D'?", startsWithD('Daniel')); /* EXPECTED RESULT 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..0b851e0 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-1.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-1.js @@ -7,7 +7,11 @@ */ function negativeOrPositive(number) { - + if (number < 0) { + return 'negative'; + } else if (number >= 0) { + return 'positive'; + } } /* @@ -17,9 +21,9 @@ var number1 = 5; var number2 = -1; var number3 = 0; -console.log(number1 + " is " + negativeOrPositive(number1)); -console.log(number2 + " is " + negativeOrPositive(number2)); -console.log(number3 + " is " + negativeOrPositive(number3)); +console.log(number1 + ' is ' + negativeOrPositive(number1)); +console.log(number2 + ' is ' + negativeOrPositive(number2)); +console.log(number3 + ' is ' + negativeOrPositive(number3)); /* EXPECTED RESULT 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..0c9b590 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-2.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-2.js @@ -8,7 +8,8 @@ */ function studentPassed(grade) { - + if (grade < 50) return 'failed'; + else if (grade >= 50) return 'passed'; } /* @@ -18,9 +19,9 @@ var grade1 = 49; var grade2 = 50; var grade3 = 100; -console.log("'" + grade1 + "': " + studentPassed(grade1)) -console.log("'" + grade2 + "': " + studentPassed(grade2)) -console.log("'" + grade3 + "': " + studentPassed(grade3)) +console.log("'" + grade1 + "': " + studentPassed(grade1)); +console.log("'" + grade2 + "': " + studentPassed(grade2)); +console.log("'" + grade3 + "': " + studentPassed(grade3)); /* EXPECTED RESULT 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..97b3318 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-3.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-3.js @@ -9,7 +9,15 @@ */ function calculateGrade(mark) { - + if (mark >= 80) { + return 'A'; + } else if (mark < 80 && mark > 60) { + return 'B'; + } else if (mark <= 60 && mark >= 50) { + return 'C'; + } else { + return 'F'; + } } /* @@ -25,7 +33,7 @@ console.log("'" + grade2 + "': " + calculateGrade(grade2)); console.log("'" + grade3 + "': " + calculateGrade(grade3)); console.log("'" + grade4 + "': " + calculateGrade(grade4)); - /* +/* EXPECTED RESULT --------------- '49': F 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..a2b8f35 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-4.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-4.js @@ -9,21 +9,23 @@ */ function containsCode(sentence) { - + if (sentence.search('code') === -1) { + return false; + } else return true; } /* DO NOT EDIT BELOW THIS LINE --------------------------- */ -var sentence1 = "code your future"; -var sentence2 = "draw your future"; -var sentence3 = "design your future"; +var sentence1 = 'code your future'; +var sentence2 = 'draw your future'; +var sentence3 = 'design your future'; -console.log("'" + sentence1 + "': " + containsCode(sentence1)) -console.log("'" + sentence2 + "': " + containsCode(sentence2)) -console.log("'" + sentence3 + "': " + containsCode(sentence3)) +console.log("'" + sentence1 + "': " + containsCode(sentence1)); +console.log("'" + sentence2 + "': " + containsCode(sentence2)); +console.log("'" + sentence3 + "': " + containsCode(sentence3)); - /* +/* EXPECTED RESULT --------------- 'code your future': true diff --git a/week-2/1-exercises/H-array-literals/exercise.js b/week-2/1-exercises/H-array-literals/exercise.js index d6dc556..99500cc 100644 --- a/week-2/1-exercises/H-array-literals/exercise.js +++ b/week-2/1-exercises/H-array-literals/exercise.js @@ -6,7 +6,8 @@ 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 - +numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; +mentors = [ 'Daniel', 'Irina', '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..420f75a 100644 --- a/week-2/1-exercises/I-array-properties/exercise.js +++ b/week-2/1-exercises/I-array-properties/exercise.js @@ -6,13 +6,13 @@ */ function isEmpty(arr) { - return; // complete this statement + return arr.length === 0; // complete this statement } /* DO NOT EDIT BELOW THIS LINE --------------------------- */ -var numbers = [1, 2, 3]; +var numbers = [ 1, 2, 3 ]; var names = []; console.log(isEmpty(numbers)); 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..2e397b5 100644 --- a/week-2/1-exercises/J-array-get-set/exercise.js +++ b/week-2/1-exercises/J-array-get-set/exercise.js @@ -5,18 +5,18 @@ */ function first(arr) { - return; // complete this statement + return arr[0]; // complete this statement } function last(arr) { - return; // complete this statement + return arr[arr.length - 1]; // complete this statement } /* DO NOT EDIT BELOW THIS LINE --------------------------- */ -var numbers = [1, 2, 3]; -var names = ["Irina", "Ashleigh", "Mozafar", "Joe"]; +var numbers = [ 1, 2, 3 ]; +var names = [ 'Irina', 'Ashleigh', 'Mozafar', 'Joe' ]; console.log(first(numbers)); console.log(last(numbers)); 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..1c0faef 100644 --- a/week-2/1-exercises/J-array-get-set/exercises2.js +++ b/week-2/1-exercises/J-array-get-set/exercises2.js @@ -6,8 +6,9 @@ - change the first value in the array to the number 1 */ -var numbers = [1, 2, 3]; // Don't change this array literal declaration - +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 6316fad..6461fc1 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -2,89 +2,81 @@ // Look at the tests and see how you can fix them. function mood() { - let isHappy = true; - - if (isHappy) { - return "I am happy"; - } else { - return "I am not happy"; - } + let isHappy = false; + + if (isHappy) { + console.log(' hppy is called'); + return 'I am happy'; + } else { + console.log(' not haapy is called'); + return 'I am not happy'; + } } function greaterThan10() { - let num = 10; - let isBigEnough; - - if (isBigEnough) { - return "num is greater than or equal to 10"; - } else { - return "num is not big enough"; - } + let num = 10; + let isBigEnough = num; + + if (isBigEnough >= num) { + return 'num is greater than or equal to 10'; + } else { + return 'num is not big enough'; + } } function sortArray() { - let letters = ["a", "n", "c", "e", "z", "f"]; - let sortedLetters; + let letters = [ 'a', 'n', 'c', 'e', 'z', 'f' ]; + let sortedLetters = letters.sort(); - return sortedLetters; + return sortedLetters; } function first5() { - let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; - let sliced; + let numbers = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; + let sliced = numbers.slice(0, 5); - return sliced; + return sliced; } function get3rdIndex(arr) { - let index = 3; - let element; + let index = 3; + let element = arr[index]; - return element; + return element; } /* ======= TESTS - DO NOT MODIFY ===== */ function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + return true; } -test("mood function works", mood() === "I am not happy"); -test( - "greaterThanTen function works", - greaterThan10() === "num is greater than or equal to 10" -); -test( - "sortArray function works", - arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"]) -); -test("first5 function works", arraysEqual(first5(), [1, 2, 3, 4, 5])); +test('mood function works', mood() === 'I am not happy'); +test('greaterThanTen function works', greaterThan10() === 'num is greater than or equal to 10'); +test('sortArray function works', arraysEqual(sortArray(), [ 'a', 'c', 'e', 'f', 'n', 'z' ])); +test('first5 function works', arraysEqual(first5(), [ 1, 2, 3, 4, 5 ])); test( - "get3rdIndex function works - case 1", - get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) === - "strawberry" -); -test( - "get3rdIndex function works - case 2", - get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18 + 'get3rdIndex function works - case 1', + get3rdIndex([ 'fruit', 'banana', 'apple', 'strawberry', 'raspberry' ]) === 'strawberry' ); +test('get3rdIndex function works - case 2', get3rdIndex([ 11, 37, 62, 18, 19, 3, 30 ]) === 18); diff --git a/week-2/2-mandatory/2-function-creation.js b/week-2/2-mandatory/2-function-creation.js index bf7ecfd..914170c 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -5,7 +5,27 @@ Write a function that: - removes any forward slashes (/) in the strings - makes the string all lowercase */ -function tidyUpString(strArr) {} + +/* + var forwardSlachIndex = strArr[i].search('/'); + if (forwardSlachIndex >= 0) { + strArr[i] = + strArr[i].slice(0, forwardSlachIndex) + strArr[i].slice(forwardSlachIndex + 1, strArr[i].lenght); + } + +*/ +function tidyUpString(strArr) { + var forwardSlachIndex; + for (var i = 0; i < strArr.length; i++) { + strArr[i] = strArr[i].trim(); + forwardSlachIndex = strArr[i].search('/'); + if (forwardSlachIndex >= 0) { + strArr[i] = strArr[i].slice(forwardSlachIndex + 1, strArr[i].lenght); + } + strArr[i] = strArr[i].toLowerCase(); + } + return strArr; +} /* Complete the function to check if the variable `num` satisfies the following requirements: @@ -15,7 +35,13 @@ 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 removes an element from an array @@ -26,7 +52,7 @@ The function must: */ function remove(arr, index) { - return; // complete this statement + return arr.slice(0, index).concat(arr.slice(index + 1, arr.length)); // complete this statement } /* @@ -38,79 +64,75 @@ Write a function that: */ function formatPercentage(arr) { - + for (var 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; } +console.log(formatPercentage([ 23, 18, 187.2, 0.372 ])); /* ======= TESTS - DO NOT MODIFY ===== */ function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } test( - "tidyUpString function works - case 1", - arraysEqual(tidyUpString(["/Daniel ", "irina ", " Gordon", "ashleigh "]), [ - "daniel", - "irina", - "gordon", - "ashleigh" - ]) + 'tidyUpString function works - case 1', + arraysEqual(tidyUpString([ '/Daniel ', 'irina ', ' Gordon', 'ashleigh ' ]), [ + 'daniel', + 'irina', + 'gordon', + 'ashleigh' + ]) ); test( - "tidyUpString function works - case 2", - arraysEqual( - tidyUpString([" /Sanyia ", " Michael ", "AnTHonY ", " Tim "]), - ["sanyia", "michael", "anthony", "tim"] - ) + 'tidyUpString function works - case 2', + arraysEqual(tidyUpString([ ' /Sanyia ', ' Michael ', 'AnTHonY ', ' Tim ' ]), [ + 'sanyia', + 'michael', + 'anthony', + 'tim' + ]) ); -test("validate function works - case 1", validate(10) === true); -test("validate function works - case 2", validate(18) === true); -test("validate function works - case 3", validate(17) === false); -test("validate function works - case 4", validate("Ten") === false); -test("validate function works - case 5", validate(108) === false); +test('validate function works - case 1', validate(10) === true); +test('validate function works - case 2', validate(18) === true); +test('validate function works - case 3', validate(17) === false); +test('validate function works - case 4', validate('Ten') === false); +test('validate function works - case 5', validate(108) === false); +test('remove function works - case 1', arraysEqual(remove([ 10, 293, 292, 176, 29 ], 3), [ 10, 293, 292, 29 ])); test( - "remove function works - case 1", - arraysEqual(remove([10, 293, 292, 176, 29], 3), [10, 293, 292, 29]) -); -test( - "remove function works - case 1", - arraysEqual(remove(["a", "b", "c", "d", "e", "f", "g"], 6), [ - "a", - "b", - "c", - "d", - "e", - "f" - ]) + 'remove function works - case 1', + arraysEqual(remove([ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ], 6), [ 'a', 'b', 'c', 'd', 'e', 'f' ]) ); test( - "formatPercentage function works - case 1", - arraysEqual(formatPercentage([23, 18, 187.2, 0.372]), [ - "23%", - "18%", - "100%", - "0.37%" - ]) -); \ No newline at end of file + 'formatPercentage function works - case 1', + arraysEqual(formatPercentage([ 23, 18, 187.2, 0.372 ]), [ '23%', '18%', '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..810091d 100644 --- a/week-2/2-mandatory/3-playing-computer.js +++ b/week-2/2-mandatory/3-playing-computer.js @@ -15,28 +15,39 @@ 7. What is the value of the "a" outer variable when "f1" is called for the first time? */ +//Answers: +// 1. Varible b is not defined which cause an error to be thrown. +// 2. Line 40 has been removed. +// 3. The console will print 2 6 4 9 6 13 8. +// 4. f1 will be executed 2 times, when the loop counter i has odd value of 1 and 3. +// 5. f2 will be executed 3 times when the loop counter i has even value of 0,2 and 4. +// 6. The "a" parameter take in the first "f1" call the value of i which is 1. +// 7. The value "a" outer variable when "f1" is called for the first time is 8 ? + let x = 2; let a = 6; const f1 = function(a, b) { - return a + b; + return a + b; }; + const f2 = function(a, b) { - return a + b + x; + return a + b + x; }; console.log(x); console.log(a); -console.log(b); +//console.log(b); for (let i = 0; i < 5; ++i) { - a = a + 1; - if (i % 2 === 0) { - const d = f2(i, x); - console.log(d); - } else { - const e = f1(i, a); - console.log(e); - } + a = a + 1; + if (i % 2 === 0) { + // if i is an even number call f2:it will be executed when i =0,2,4 . + const d = f2(i, x); + console.log(d); + } else { + const e = f1(i, a); // if i is an odd number call f1: it will be executed when i =1,3 . + 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..2dfd8c8 100644 --- a/week-2/2-mandatory/4-sorting-algorithm.js +++ b/week-2/2-mandatory/4-sorting-algorithm.js @@ -14,57 +14,58 @@ 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) { + + var ArrayOfNumbers = []; + for (var i = 0; i < arr.length; i++) { + if (typeof arr[i] === 'number') { + ArrayOfNumbers.push(arr[i]); + } + } + + arr = ArrayOfNumbers; + var temp; + for (var k = 0; k < arr.length; k++) { + for (var j = k + 1; j < arr.length; j++) { + if (arr[k] > arr[j]) { + temp = arr[k]; + arr[k] = arr[j]; + arr[j] = temp; + } + } + } + + return arr; +} /* ======= TESTS - DO NOT MODIFY ===== */ -const agesCase1 = [ - "🎹", - 100, - "💩", - 55, - "🥵", - "🙈", - 45, - "🍕", - "Sanyia", - 66, - "James", - 23, - "🎖", - "Ismeal", -]; -const agesCase2 = ["28", 100, 60, 55, "75", "🍕", "Elamin"]; +const agesCase1 = [ '🎹', 100, '💩', 55, '🥵', '🙈', 45, '🍕', 'Sanyia', 66, 'James', 23, '🎖', 'Ismeal' ]; +const agesCase2 = [ '28', 100, 60, 55, '75', '🍕', 'Elamin' ]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } - console.log(`${test_name}: ${status}`); + console.log(`${test_name}: ${status}`); } -test( - "sortAges function works - case 1", - arraysEqual(sortAges(agesCase1), [23, 45, 55, 66, 100]) -); +test('sortAges function works - case 1', arraysEqual(sortAges(agesCase1), [ 23, 45, 55, 66, 100 ])); -test( - "sortAges function works - case 2", - arraysEqual(sortAges(agesCase2), [55, 60, 100]) -); +test('sortAges function works - case 2', arraysEqual(sortAges(agesCase2), [ 55, 60, 100 ])); diff --git a/week-2/3-extra/1-radio-stations.js b/week-2/3-extra/1-radio-stations.js index 95c0e56..efef574 100644 --- a/week-2/3-extra/1-radio-stations.js +++ b/week-2/3-extra/1-radio-stations.js @@ -25,74 +25,149 @@ * - Return only the frequencies that are radio stations. */ // `getStations` goes here +/* +function getStations() { + var af = getAllFrequencies(); + var currentStation = Math.floor(Math.random() * (108 - 87 + 1) + 87); + console.log(currentStation); + isRadioFrequency(currentStation); + return currentStation; +} +function isRadioFrequency(frequency) { + found = false; + var af = getAllFrequencies(); + for (var i = 0; i < af.length; i++) { + if (af[i] === frequency) { + found = true; + } + } + + return found; +} +*/ + +//---------------------------------------------------------------------- +/** + * Finding a radio station, and a good one, can be hard manually. + * Let's use some code to help us build a program that helps us scan + * the radio waves for some good music. + */ + +/** + * First, let's create a function that creates a list of all the frequencies. + * Call this function `getAllFrequencies`. + * + * This function should: + * - Create an array starting at 87 and ending in 108 + * - Should return this array to use in other functions + */ +// `getAllFrequencies` goes here +function getAllFrequencies() { + frequenciesArray = []; + for (var i = 0; i <= 108 - 87; i++) { + frequenciesArray[i] = 87 + i; + } + return frequenciesArray; +} + +/** + * Next, let's write a function that gives us only the frequencies that are radio stations. + * Call this function `getStations`. + * + * This function should: + * - Get the available frequencies from `getAllFrequencies` + * - There is a helper function called isRadioFrequency that takes an integer as an argument and returns a boolean. + * - Return only the frequencies that are radio stations. + */ +// `getStations` goes here +function getStations() { + let radioStations = getAllFrequencies(); + let validStations = []; + for (let i = 0; i < radioStations.length; i++) { + if (isRadioStation(radioStations[i])) { + validStations.push(radioStations[i]); + } + } + return validStations; +} + +function isRadioFrequency(frequency) { + found = false; + var af = getAllFrequencies(); + for (var i = 0; i < af.length; i++) { + if (af[i] === frequency) { + found = true; + } + } + + return found; +} /* ======= TESTS - DO NOT MODIFY ======= */ function getAvailableStations() { - // Using `stations` as a property as defining it as a global variable wouldn't - // always make it initialized before the function is called - if (!getAvailableStations.stations) { - const stationCount = 4; - getAvailableStations.stations = new Array(stationCount) - .fill(undefined) - .map(function() { - return Math.floor(Math.random() * (108 - 87 + 1) + 87); - }) - .sort(function(frequencyA, frequencyB) { - return frequencyA - frequencyB; - }); - } - - return getAvailableStations.stations; + // Using `stations` as a property as defining it as a global variable wouldn't + // always make it initialized before the function is called + if (!getAvailableStations.stations) { + const stationCount = 4; + getAvailableStations.stations = new Array(stationCount) + .fill(undefined) + .map(function() { + return Math.floor(Math.random() * (108 - 87 + 1) + 87); + }) + .sort(function(frequencyA, frequencyB) { + return frequencyA - frequencyB; + }); + } + + return getAvailableStations.stations; } function isRadioStation(frequency) { - return getAvailableStations().includes(frequency); + return getAvailableStations().includes(frequency); } -const assert = require("assert"); +const assert = require('assert'); function test(testName, fn) { - try { - fn(); - console.log(`\n✅ ${testName}: PASS`); - } catch (error) { - console.log( - `\n❌ ${testName}: FAIL (see details below)\n\n${error.message}` - ); - } + try { + fn(); + console.log(`\n✅ ${testName}: PASS`); + } catch (error) { + console.log(`\n❌ ${testName}: FAIL (see details below)\n\n${error.message}`); + } } -test("getAllFrequencies() returns all frequencies between 87 and 108", function() { - const frequencies = getAllFrequencies(); - assert.deepStrictEqual(frequencies, [ - 87, - 88, - 89, - 90, - 91, - 92, - 93, - 94, - 95, - 96, - 97, - 98, - 99, - 100, - 101, - 102, - 103, - 104, - 105, - 106, - 107, - 108 - ]); +test('getAllFrequencies() returns all frequencies between 87 and 108', function() { + const frequencies = getAllFrequencies(); + assert.deepStrictEqual(frequencies, [ + 87, + 88, + 89, + 90, + 91, + 92, + 93, + 94, + 95, + 96, + 97, + 98, + 99, + 100, + 101, + 102, + 103, + 104, + 105, + 106, + 107, + 108 + ]); }); -test("getStations() returns all the available stations", () => { - const stations = getStations(); - assert.deepStrictEqual(stations, getAvailableStations()); +test('getStations() returns all the available stations', () => { + const stations = getStations(); + assert.deepStrictEqual(stations, getAvailableStations()); }); diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..760f38f 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -4,12 +4,15 @@ */ // write your code here +function isLongerNameAndStartWithA(names) { + return names.charAt(0) === 'A' && names.length > 7; +} -var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; - -var longNameThatStartsWithA = findLongNameThatStartsWithA(names); - -console.log(longNameThatStartsWithA); +var names = [ 'Rakesh', 'Antonio', 'Alexandra', 'Andronicus', 'Annam', 'Mikey', 'Anastasia', 'Karim', 'Ahmed' ]; +//names.find(isLongerNameAndStartWithA(names)); +//var longNameThatStartsWithA = findLongNameThatStartsWithA(names); +var nameFound = names.find(isLongerNameAndStartWithA); +console.log(nameFound); /* 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..698de86 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -5,20 +5,26 @@ - Add a check for null values, and if one exists, exit the program - 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 ] ]; // 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"]; +var students = [ 'Islam', 'Lesley', 'Harun', 'Rukmini' ]; +var mentors = [ 'Daniel', 'Irina', 'Mozafar', 'Luke' ]; var pairs = pairsByIndex.map(function(indexes) { - var student = students[indexes[0]]; - var mentor = mentors[indexes[1]]; - return [student, mentor]; + if (indexes === null) { + console.log('Error: A null value found'); + process.exit(1); + + } + var student = students[indexes[0]]; + var mentor = mentors[indexes[1]]; + return [ student, mentor ]; }); console.log(pairs); + diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..6c7157c 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -1,18 +1,26 @@ /* - This program should check if the array `group` contains only students + 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 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 */ -var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; -var group = ["Austine", "Dany", "Swathi", "Daniel"]; +var pairsByIndexRaw = [ [ 0, 3 ], [ 1, 2 ], [ 2, 1 ], null, [ 1 ], false, 'whoops' ]; -var groupIsOnlyStudents; // complete this statement +var pairsByIndex = pairsByIndexRaw.filter(function(pair) { + return Array.isArray(pair) && pair.length === 2; +}); -if (groupIsOnlyStudents) { - console.log("The group contains only students"); -} else { - console.log("The group does not contain only students"); -} +// Complete this statement -/* EXPECTED RESULT */ +var students = [ 'Islam', 'Lesley', 'Harun', 'Rukmini' ]; +var mentors = [ 'Daniel', 'Irina', 'Mozafar', 'Luke' ]; -// The group does not contain only students +var pairs = pairsByIndex.map(function(indexes) { + var student = students[indexes[0]]; + var mentor = mentors[indexes[1]]; + return [ student, mentor ]; +}); + +console.log(pairs); diff --git a/week-3/1-exercises/D-array-filter/exercise.js b/week-3/1-exercises/D-array-filter/exercise.js index 6e32cc6..6c7157c 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -6,17 +6,21 @@ - Do not edit any of the existing code */ -var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; +var pairsByIndexRaw = [ [ 0, 3 ], [ 1, 2 ], [ 2, 1 ], null, [ 1 ], false, 'whoops' ]; -var pairsByIndex; // Complete this statement +var pairsByIndex = pairsByIndexRaw.filter(function(pair) { + return Array.isArray(pair) && pair.length === 2; +}); + +// Complete this statement -var students = ["Islam", "Lesley", "Harun", "Rukmini"]; -var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; +var students = [ 'Islam', 'Lesley', 'Harun', 'Rukmini' ]; +var mentors = [ 'Daniel', 'Irina', 'Mozafar', 'Luke' ]; var pairs = pairsByIndex.map(function(indexes) { - var student = students[indexes[0]]; - var mentor = mentors[indexes[1]]; - return [student, mentor]; + var student = students[indexes[0]]; + var mentor = mentors[indexes[1]]; + return [ student, mentor ]; }); console.log(pairs); diff --git a/week-3/1-exercises/E-array-map/exercise.js b/week-3/1-exercises/E-array-map/exercise.js index 2835e92..bb7804d 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -1,5 +1,28 @@ // Using the .map() method, create a new array with `numbers` multiplied by 100 // Write multiple solutions using different syntax (as shown in the README) -var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +var numbers = [ 0.1, 0.2, 0.3, 0.4, 0.5 ]; +var numbersX100 = numbers.map(multiplyByCent); +function multiplyByCent(number) { + return number * 100; +} +console.log(numbersX100 + ' First way'); +var numbersX100nd = numbers.map(function multiplyByCent(number) { + return number * 100; +}); +console.log(numbersX100nd + ' secound way'); + +var numbersX100rd = numbers.map(number => number * 100); + +console.log(numbersX100rd + ' third way'); + +var numbersX100th = numbers.map(number => { + return number * 100; +}); +console.log(numbersX100th + ' fourth way'); + +var numberX100last = numbers.map(function(number) { + return number * 100; +}); +console.log(numberX100last + ' fifth way'); diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..92280d7 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -7,7 +7,16 @@ An array with numbers 1-15 has been provided. */ -var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +var arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]; + +var alteredArray = arr.forEach(function(name) { + if (name % 3 === 0 && name % 5 === 0) console.log('FizzBuzz'); + else if (name % 3 === 0) console.log('Fizz'); + else if (name % 5 === 0) console.log('Buzz'); + else console.log(name); +}); + +console.log(alteredArray); /* 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..461b0d7 100644 --- a/week-3/1-exercises/G-array-methods/exercise2.js +++ b/week-3/1-exercises/G-array-methods/exercise2.js @@ -4,10 +4,10 @@ The variable everyone should be an array containing both mentors and students. */ -var mentors = ["Daniel", "Irina", "Rares"]; -var students = ["Rukmini", "Abdul", "Austine", "Swathi"]; +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..73b9a49 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise.js +++ b/week-3/1-exercises/H-array-methods-2/exercise.js @@ -5,18 +5,10 @@ The variable `lastFive` should contain the last five items of `everyone` */ -var everyone = [ - "Daniel", - "Irina", - "Rares", - "Rukmini", - "Abdul", - "Austine", - "Swathi" -]; +var everyone = [ 'Daniel', 'Irina', 'Rares', 'Rukmini', 'Abdul', 'Austine', 'Swathi' ]; -var firstFive; // complete this statement -var lastFive; // complete this statement +var firstFive = everyone.slice(0, 5); // complete this statement +var lastFive = everyone.slice(everyone.length - 5, everyone.length); // 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..72be0fb 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise2.js +++ b/week-3/1-exercises/H-array-methods-2/exercise2.js @@ -7,15 +7,21 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + let stringToarray = str.split(''); + stringToarray[0] = stringToarray[0].toUpperCase(); + let orginalString = stringToarray.join(''); + + return orginalString; +} /* DO NOT EDIT BELOW THIS LINE --------------------------- */ -var name = "daniel"; +var name = 'daniel'; console.log(capitalise(name)); -console.log(capitalise("hello")); +console.log(capitalise('hello')); /* EXPECTED RESULT 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..8074da4 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,8 @@ 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..5975e29 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,32 +9,31 @@ 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(arr) { + let test = arr.sort(); + var level1 = test.find(function(level) { + return parseFloat(level) > 19.5; + }); + return level1; + } /* ======= 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%"] +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) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } -test( - "safeLevels function works - case 2", - safeLevels(oxygenLevels1) === "19.9%" -); +test('safeLevels function works - case 2', safeLevels(oxygenLevels1) === '19.9%'); -test( - "safeLevels function works - case 2", - safeLevels(oxygenLevels2) === "20.2%" -); \ No newline at end of file +test('safeLevels function works - case 2', safeLevels(oxygenLevels2) === '20.2%'); diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..9374170 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,25 +10,33 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(arr) { + var test1 = arr.every(function(item) { + return item === 'pink'; + }); + + if (test1 === true) { + return 'Bush is safe to eat from'; + } else { + return 'Toxic! Leave bush alone!'; + } } /* ======= TESTS - DO NOT MODIFY ===== */ -let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"] -let bushBerryColours2 = ["pink", "pink", "pink", "pink"] +let bushBerryColours1 = [ 'pink', 'pink', 'pink', 'neon', 'pink', 'transparent' ]; +let bushBerryColours2 = [ 'pink', 'pink', 'pink', 'pink' ]; function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } -test("bushChecker funtion works - case 1", bushChecker(bushBerryColours1) === "Toxic! Leave bush alone!") -test("bushChecker funtion works - case 1", bushChecker(bushBerryColours2) === "Bush is safe to eat from") +test('bushChecker funtion works - case 1', bushChecker(bushBerryColours1) === 'Toxic! Leave bush alone!'); +test('bushChecker funtion works - case 1', bushChecker(bushBerryColours2) === 'Bush is safe to eat from'); diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index f99891a..be234e0 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -1,4 +1,5 @@ /* + The voyagers decide that they quite like this planet, and some of them want to settle there and colonise it. They call the planet "Alpha" and they decide that the FAMILIES whose last names start with 'A' should stay, while the others go on in search of other planets to call home. @@ -8,50 +9,53 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { - +function colonisers(arr) { + return arr.filter(function(members) { + return members[0] === 'A' && members.includes('family'); + }); } /* ======= TESTS - DO NOT MODIFY ===== */ const voyagers = [ - "Adam family", - "Potter family", - "Eric", - "Aldous", - "Button family", - "Jude", - "Carmichael", - "Bunny", - "Asimov", - "Oscar family", - "Avery family", - "Archer family" + 'Adam family', + 'Potter family', + 'Eric', + 'Aldous', + 'Button family', + 'Jude', + 'Carmichael', + 'Bunny', + 'Asimov', + 'Oscar family', + 'Avery family', + 'Archer family' ]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } -test("colonisers function works", - arraysEqual(colonisers(voyagers), ["Adam family", "Avery family", "Archer family"]) -) \ No newline at end of file +test( + 'colonisers function works', + arraysEqual(colonisers(voyagers), [ 'Adam family', 'Avery family', 'Archer family' ]) +); diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index 6424b01..39d3534 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,46 +7,52 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { +function eligibleStudents(arr) { + let accepted = []; + for (var i = 0; i < arr.length; i++) { + if (arr[i][1] >= 8) { + accepted.push(arr[i][0]); + } + } + return accepted; } /* ======= TESTS - DO NOT MODIFY ===== */ const attendances = [ - ["Ahmed", 8], - ["Clement", 10], - ["Elamin", 6], - ["Adam", 7], - ["Tayoa", 11], - ["Nina", 10] -] + [ 'Ahmed', 8 ], + [ 'Clement', 10 ], + [ 'Elamin', 6 ], + [ 'Adam', 7 ], + [ 'Tayoa', 11 ], + [ 'Nina', 10 ] +]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; - - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } - - return true; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; + + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } + + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } -test("eligibleStudents function works", - arraysEqual( - eligibleStudents(attendances), ["Ahmed", "Clement", "Tayoa", "Nina"] - ) -) \ No newline at end of file +test( + 'eligibleStudents function works', + arraysEqual(eligibleStudents(attendances), [ 'Ahmed', 'Clement', 'Tayoa', 'Nina' ]) +); diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 53499c3..880b3cb 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,59 +7,68 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { +function journeyPlanner(location, means) { + let locations = []; + if (means === 'bus') { + for (var i = 0; i < location.length; i++) { + if (location[i].includes('bus')) locations.push(location[i][0]); + } + } else if (means === 'tube') { + for (var i = 0; i < location.length; i++) { + if (location[i].includes('tube')) locations.push(location[i][0]); + } + } else if (means == 'river boat') { + for (var i = 0; i < location.length; i++) { + if (location[i].includes('river boat')) locations.push(location[i][0]); + } + } + return locations; } /* ======= TESTS - DO NOT MODIFY ===== */ const londonLocations = [ - ["Angel", "tube", "bus"], - ["London Bridge", "tube", "river boat"], - ["Tower Bridge", "tube", "bus"], - ["Greenwich", "bus", "river boat"] -] + [ 'Angel', 'tube', 'bus' ], + [ 'London Bridge', 'tube', 'river boat' ], + [ 'Tower Bridge', 'tube', 'bus' ], + [ 'Greenwich', 'bus', 'river boat' ] +]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } -test("journeyPlanner function works - case 1", - arraysEqual( - journeyPlanner(londonLocations, "river boat"), - ["London Bridge", "Greenwich"] - ) -) +test( + 'journeyPlanner function works - case 1', + arraysEqual(journeyPlanner(londonLocations, 'river boat'), [ 'London Bridge', 'Greenwich' ]) +); -test("journeyPlanner function works - case 2", - arraysEqual( - journeyPlanner(londonLocations, "bus"), - ["Angel", "Tower Bridge", "Greenwich"] - ) -) +test( + 'journeyPlanner function works - case 2', + arraysEqual(journeyPlanner(londonLocations, 'bus'), [ 'Angel', 'Tower Bridge', 'Greenwich' ]) +); -test("journeyPlanner function works - case 3", - arraysEqual( - journeyPlanner(londonLocations, "tube"), - ["Angel", "London Bridge", "Tower Bridge"] - ) -) +test( + 'journeyPlanner function works - case 3', + arraysEqual(journeyPlanner(londonLocations, 'tube'), [ 'Angel', 'London Bridge', 'Tower Bridge' ]) +); diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index eddfe44..1200bc2 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,43 +4,35 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(array) { + return streetNames.filter(name => name.includes('Lane')); } /* ======= TESTS - DO NOT MODIFY ===== */ -const streetNames = [ - "Abchurch Lane", - "Adam's Court", - "Addle Hill", - "Addle Lane", - "Alban Highwalk" -] +const streetNames = [ 'Abchurch Lane', "Adam's Court", 'Addle Hill', 'Addle Lane', 'Alban Highwalk' ]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; - - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } - - return true; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; + + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } + + return true; } - + function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } -test("getLanes function works", - arraysEqual(getLanes(streetNames), ["Abchurch Lane", "Addle Lane"]) -) \ No newline at end of file +test('getLanes function works', arraysEqual(getLanes(streetNames), [ 'Abchurch Lane', 'Addle Lane' ])); diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 57b3d53..a5a47f0 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -23,47 +23,110 @@ PasswordValidationResult= [false, false, false, false, true] */ function validatePasswords(passwords) { + var validationArray = [ passwords.length ]; + let word; + for (var i = 0; i < passwords.length; i++) { + word = passwords[i]; + if ( + isNotPreviousPassword(i, word, passwords) && + isLong(passwords[i]) && + hasUpperCase(passwords[i]) && + hasLowerCase(passwords[i]) && + hasNumber(passwords[i]) && + hasNoAlphanumeric(passwords[i]) + ) + validationArray[i] = true; + else validationArray[i] = false; + } + return validationArray; } +function isLong(password) { + return password.length >= 5; +} + +function isNotPreviousPassword(i, word, array) { + return i === array.indexOf(word); +} + +function hasUpperCase(string) { + var upperCaseFound = false; + + for (var i = 0; i < string.length; i++) { + if (string.charCodeAt(i) >= 'A'.charCodeAt(0) && string.charCodeAt(i) <= 'Z'.charCodeAt(0)) + upperCaseFound = true; + } + return upperCaseFound; +} +function hasLowerCase(string) { + var lowerCaseFound = false; + + for (var i = 0; i < string.length; i++) { + if (string.charCodeAt(i) >= 'a'.charCodeAt(0) && string.charCodeAt(i) <= 'z'.charCodeAt(0)) + lowerCaseFound = true; + } + return lowerCaseFound; +} +function hasNumber(string) { + var numberFound = false; + + for (var i = 0; i < string.length; i++) { + if (string.charCodeAt(i) >= '0'.charCodeAt(0) && string.charCodeAt(i) <= '9'.charCodeAt(0)) numberFound = true; + } + return numberFound; +} +function hasNoAlphanumeric(string) { + var symbolFound = false; + + for (var i = 0; i < string.length; i++) { + if ( + string.charAt(i) === '!' || + string.charAt(i) === '#' || + string.charAt(i) === '$' || + string.charAt(i) === '%' || + string.charAt(i) === '.' + ) + symbolFound = true; + } + return symbolFound; +} +//Have non - alphanumeric symbols + /* ======= TESTS - DO NOT MODIFY ===== */ -const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"] -const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"] +const passwords1 = [ 'Se%5', 'TktE.TJTU', '384#HsHF', 'dvyyeyy!5', 'tryT3729' ]; +const passwords2 = [ 'StUFf27%', 'Pl3nty!', 'Jai33', 'shajsaUA**&&', 'Pl3nty!' ]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; - - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } - - return true; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; + + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } + + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = 'PASSED'; + } else { + status = 'FAILED'; + } + + console.log(`${test_name}: ${status}`); } test( - "validatePasswords function works - case 1", - arraysEqual( - validatePasswords(passwords1), [false, false, true, false, false] - ) - ); - - test( - "validatePasswords function works - case 2", - arraysEqual( - validatePasswords(passwords2), [true, true, false, false, false] - ) - ); + 'validatePasswords function works - case 1', + arraysEqual(validatePasswords(passwords1), [ false, false, true, false, false ]) +); + +test( + 'validatePasswords function works - case 2', + arraysEqual(validatePasswords(passwords2), [ true, true, false, false, false ]) +); diff --git a/week-3/2-mandatory/8-codewars.md b/week-3/2-mandatory/8-codewars.md index 8bd2b85..c0aa019 100644 --- a/week-3/2-mandatory/8-codewars.md +++ b/week-3/2-mandatory/8-codewars.md @@ -1,4 +1,4 @@ -# Codewars Exercises +# Codewars Exercises => Done. Today, you'll be using a platform called [CodeWars](https://codewars.com) to help you recap the materials you learnt in JS1. CodeWars is an excellent platform for going through interesting JavaScript exercises, and allows you to communicate with the wider community to learn about the best way of writing JavaScript code. @@ -30,4 +30,4 @@ Today, you'll be using a platform called [CodeWars](https://codewars.com) to hel - [people in bus](https://www.codewars.com/kata/number-of-people-in-the-bus/train/javascript) - [sum without highest and lowest](https://www.codewars.com/kata/sum-without-highest-and-lowest-number/train/javascript) - [reveersed array of digits](https://www.codewars.com/kata/convert-number-to-reversed-array-of-digits/train/javascript) -- [slash sum of negatives](https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives/train/javascript) \ No newline at end of file +- [slash sum of negatives](https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives/train/javascript)