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()); });