diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 6910f28..11d4224 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -2,18 +2,20 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { +function addNumbers(a,b,c) { return a + b + c; } function introduceMe(name, age) -return "Hello, my name is " + name "and I am " age + "years old"; +{ + return `Hello, my name is ${name} and I am ${age} years old`; +} function getAddition(a, b) { - total = a ++ b + total = a + b; // Use string interpolation here - return "The total is %{total}" + return `The total is ${total}` ; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -28,9 +30,9 @@ function test(test_name, expr) { status = "FAILED" } - console.log(`${test_name}: ${status}`) + console.log(`${test_name}: ${status}`); } -test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13) -test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old") -test("fixed getRemainder function", getRemainder(23,5) === "The remainder is 3") +test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13) ; +test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old") ; +test("fixed getAddition function", getAddition(23,5) === "The total is 28") ; diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 1e0a9d4..2dd2781 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,16 +1,16 @@ // The syntax for this function is valid but it has an error, find it and fix it. function trimWord(word) { - return wordtrim(); + return word; } function getWordLength(word) { - return "word".length() + return word.length; } function multiply(a, b, c) { - a * b * c; - return; + + return a * b * c; } /* ======= TESTS - DO NOT MODIFY ===== @@ -30,6 +30,6 @@ function test(test_name, expr) { console.log(`${test_name}: ${status}`) } -test("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture") +test("fixed trimWord function", trimWord("CodeYourFuture") === "CodeYourFuture") test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25) test("fixed multiply function", multiply(2,3,6) === 36) \ No newline at end of file diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index 3c02135..a9cfac6 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,8 +9,24 @@ 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() { +var first=true; + +function check(element) +{ + if( parseFloat(element) > 19.5 && parseFloat(element) < 23.5 && first ) + { + console.log("first element found is "+element); + first=false; + return element; + } +} + +function safeLevels(arr) { + first=true; + value=arr.filter(check); + final_value= String(value); + return final_value; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -30,7 +46,7 @@ function test(test_name, expr) { } test( - "safeLevels function works - case 2", + "safeLevels function works - case 1", safeLevels(oxygenLevels1) === "19.9%" ); diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..a445887 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,8 +10,20 @@ Use the tests to confirm which message to return */ -function bushChecker() { +function allEqual(arr) { + return new Set(arr).size === 1; +} + +function bushChecker(arr) { + + if( new Set(arr).size === 1 ) // creates a set and if all the elements are same, size of the set will be 1. + { + answer="Bush is safe to eat from"; + } + else + answer= "Toxic! Leave bush alone!"; + return answer; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index f99891a..28ff109 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,8 +8,18 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { +function check_family_name(fam_name) +{ + check=fam_name.search("family"); + if(check!=-1 && fam_name[0]==="A") + return true; + + return false; +} + +function colonisers(voyagers) { + return voyagers.filter(check_family_name); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index 6424b01..fac5bce 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,8 +7,23 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { +function check (student) +{ + if(student[1]>=8) + { + return student[0]; + } +} + +function eligibleStudents(arr_students) { + new_array = arr_students.filter(check); + var names=[]; + for(let i=0;icheck_mode(el,mode)); // new_array will store the elements with given mode. + var final_places=[]; // will retieve only the names of the places. + + for(let i=0;i check_lane(el) ); // new_array will store the elements with "lane word" + return new_array; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 57b3d53..2bc5ad0 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -22,8 +22,87 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) { +//-----------------------------------------Explanation of the parameters.---------------- +// passw is the password +// passwords is the array +// isFirstTime is a boolean for the passw. if it is true it means that => passw is not repeated in the array +// else it is repeated more than once in the array. +function check(passw,passwords,isFirstTime) +{ + + if( passw.length>=5 && + hasNumbers(passw) && + hasLowerCase(passw) && + hasUpperCase(passw) && + hasChars(passw) && + isFirstTime + ) + { + return true; + } + return false; + +} + +function hasNumbers(pass) // This function will check if a string has a number. +{ +var regex = /\d/g; +return regex.test(pass); +} + + +function hasLowerCase(str) { + return (/[a-z]/.test(str)); +} +function hasUpperCase(str) { + return (/[A-Z]/.test(str)); +} + +// this function checks if the pass argument has a series of characters. +function hasChars(pass) +{ + var value_1 = /!|#|%/.test(pass); + var value_2=pass.includes("."); + var value_3=pass.includes("$"); + + if(value_1 || value_2 || value_3) + return true; + + return false; +} + + +// returns an array with boolean value for each password. +// true means that the element at that position did not repeat in the array. +// false means that element at that index is more than one time in that array. +function num_times_element(arr) +{ + let store=0; + var first_time= Array(arr.length).fill(true); + for(let i=0;i16 && final_digit%2==0 ) + +function sum(arr) +{ + var total=0; + for(let i=0;i char === card_number[0]); // checks if all the digits are same + let final_digit = card_number[ card_number.length-1 ]; + + if( + card_number.length===16 && + isNumber && + !isSame && + final_digit%2==0 && + sum(card_number)>16 + ) + { + console.log("Card number is valid => " + card_number); + return true; + } + else + { + console.log("Invalid card number, enter another card number please.") + return false; + } +} + +card_validator("9999777788880000"); \ No newline at end of file