Skip to content
This repository was archived by the owner on Oct 26, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions week-1/2-mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */
Expand All @@ -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") ;
10 changes: 5 additions & 5 deletions week-1/2-mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -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 =====
Expand All @@ -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)
20 changes: 18 additions & 2 deletions week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */
Expand All @@ -30,7 +46,7 @@ function test(test_name, expr) {
}

test(
"safeLevels function works - case 2",
"safeLevels function works - case 1",
safeLevels(oxygenLevels1) === "19.9%"
);

Expand Down
14 changes: 13 additions & 1 deletion week-3/2-mandatory/2-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */
Expand Down
12 changes: 11 additions & 1 deletion week-3/2-mandatory/3-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */
Expand Down
17 changes: 16 additions & 1 deletion week-3/2-mandatory/4-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;i<new_array.length;i++)
names.push(new_array[i][0]);
console.log(names);
return names;

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
19 changes: 18 additions & 1 deletion week-3/2-mandatory/5-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@

NOTE: only the names should be returned, not the means of transport.
*/
function check_mode(place,mode)
{
// console.log("place is "+place);
check=String(place).search(mode);
if(check!=-1 )
return true;

function journeyPlanner() {
return false;
}


function journeyPlanner(arr_places,mode) {

var new_array=arr_places.filter(el=>check_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<new_array.length;i++)
final_places.push(new_array[i][0]);

return final_places;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
14 changes: 13 additions & 1 deletion week-3/2-mandatory/6-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@
Write a function that will return all street names which contain 'Lane' in their name.
*/

function getLanes() {
function check_lane(street)
{
check=String(street).search("Lane");
if(check!=-1 )
return true;

return false;
}


function getLanes(arr_streets) {

var new_array=arr_streets.filter( el=> check_lane(el) ); // new_array will store the elements with "lane word"
return new_array;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
83 changes: 82 additions & 1 deletion week-3/2-mandatory/7-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;i<arr.length-1;i++)
{
store=arr[i];
for(let j=i+1;j<arr.length;j++)
{
if(arr[j]===store)
first_time[j]=false;
}
}
return first_time;
}

function validatePasswords(passwords)
{
isFirstTime=num_times_element(passwords); // contains boolean value for each password, true means the password is not repeated more than once, otherwise it repeats.

final_ans=[]; // will contain the final answer(Booleans) for each element
for(let i=0;i<passwords.length;i++)
{
final_ans[i]=check(passwords[i],passwords,isFirstTime[i]);

}
return final_ans;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down Expand Up @@ -67,3 +146,5 @@ test(
validatePasswords(passwords2), [true, true, false, false, false]
)
);


44 changes: 44 additions & 0 deletions week-3/3-extra/creditCard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Here are the rules for a valid number:

// - Number must be 16 digits, all of them must be numbers
// - You must have at least two different digits represented (all of the digits cannot be the same)
// - The final digit must be even
// - The sum of all the digits must be greater than 16
// - The following credit card numbers are valid:
// if(card_number.lenght===16 && isNumber && !isSame && sum(card_number)>16 && final_digit%2==0 )

function sum(arr)
{
var total=0;
for(let i=0;i<arr.length;i++)
{
total+=Number(arr[i]);
}
return total;
}

function card_validator(card_number) // card_number will be a string.
{
let isNumber = /^\d+$/.test(card_number); // checks if all digits are numbers.
let isSame = card_number.split('').every(char => 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");