Skip to content
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 24 additions & 24 deletions spec/conditional-flow/numeric-conditions.spec.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
const {a, b, c} = require('../../src/conditional-flow/numeric-conditions')
const { a, b, c } = require("../../src/conditional-flow/numeric-conditions");

describe("Numeric Conditions isArrayEmpty:", () => {
it("[] is empty", () => {
expect(a([])).toEqual(true)
})
expect(a([])).toEqual(true);
});
it("[1] is not empty", () => {
expect(a([1])).toEqual(false)
})
expect(a([1])).toEqual(false);
});

it("['hello', 'Ed'] is not empty", () => {
expect(a(['hello', 'Ed'])).toEqual(false)
})
})
expect(a(["hello", "Ed"])).toEqual(false);
});
});

describe("Numeric conditions isGreaterThan:", () => {
it("3 is greater than 2", () => {
expect(b(3, 2)).toEqual(true)
})
expect(b(3, 2)).toEqual(true);
});

it("0 is not greater than 10", () => {
expect(b(0, 10)).toEqual(false)
})
expect(b(0, 10)).toEqual(false);
});

it("42 is not greater than 42", () => {
expect(b(42, 42)).toEqual(false)
})
expect(b(42, 42)).toEqual(false);
});

it("-1 is greater than -3", () => {
expect(b(-1, -3)).toEqual(true)
})
})
expect(b(-1, -3)).toEqual(true);
});
});

describe("Numeric Conditions findLowest:", () => {
it("1 is lowest in [10, 8, 4, 1, 8]", () => {
expect(c([10, 8, 4, 1, 8])).toEqual(1)
})
expect(c([10, 8, 4, 1, 8])).toEqual(1);
});

it("-10 is lowest in [0, 0, -10]", () => {
expect(c([0, 0, -10])).toEqual(-10)
})
expect(c([0, 0, -10])).toEqual(-10);
});

it("100 is lowest in [100,100,100,100]", () => {
expect(c([100, 100, 100])).toEqual(100)
})
})
expect(c([100, 100, 100])).toEqual(100);
});
});
12 changes: 8 additions & 4 deletions src/conditional-flow/boolean-conditions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// This function should accept a boolean value and return the string
// "Well done, you passed!" if the value is true, or "Sorry, try again"
// if the value is false.
function getResult (didPass) {
function getResult(didPass) {
if (didPass == true) {
return "Well done, you passed!";
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't really need the else here, you could just have the

if (didPass == true) {
  return "Well done, you passed!";
}
return "Sorry, try again";

This is because didPass will either be true or false. So in the case it is true, your function will stop when you return Well done. In the case it is false, it will skip the if statement and go onto the next line; in the code I showed above, it'll return Sorry.

Hope that makes sense!

return "Sorry, try again";
}

// TODO: write code in this function body to pass the tests

}

module.exports = {
a: getResult
}
a: getResult,
};
28 changes: 18 additions & 10 deletions src/conditional-flow/multiple-conditions.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// This function should return true if num is greater
// than or equal to lower AND less than or equal to upper.
// Implement this with a single condition.
function isInRange (num, lower, upper) {

function isInRange(num, lower, upper) {
return num >= lower && num <= upper ? true : false;
// TODO: write code in this function body to pass the tests

}

// This function should return true if the passed string is equal
// to "Hello" or "Goodbye". Implement this with a single
// if statement.
function isHelloOrGoodbye (val1) {

function isHelloOrGoodbye(val1) {
return val1 === "Hello" || val1 === "Goodbye" ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. The ternary operator is not needed. This can be simplified to

return val1 === 'Hello' || val1 === 'Goodbye'

// TODO: write code in this function body to pass the tests

}

// This function should return a string that describes the provided age value. The
Expand All @@ -28,13 +26,23 @@ function isHelloOrGoodbye (val1) {
// 5-12 | Child
// 13-19 | Teenager
// 20+ | Adult
function getAgeDescription (age) {

function getAgeDescription(age) {
return age === 0
? "Baby"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better as an if statement. Nested ternarys are really hard to read and even harder to maintain.

: age >= 1 && age <= 4
? "Toddler"
: age >= 5 && age <= 12
? "Child"
: age >= 13 && age <= 19
? "Teenager"
: age >= 20
? "Adult"
: false;
// TODO: write code in this function body to pass the tests
}

module.exports = {
a: isInRange,
b: isHelloOrGoodbye,
c: getAgeDescription
}
c: getAgeDescription,
};
30 changes: 19 additions & 11 deletions src/conditional-flow/numeric-conditions.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
// TODO: Implement the functions below to make the tests pass

// This function should return true if there are no elements in the array, false otherwise
function isArrayEmpty (array) {

function isArrayEmpty(array) {
return array.length == 0 ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to:

return array.length == 0

// TODO: write code in this function body to pass the tests

}

// This function should return true if num1 is greater than num2, false otherwise
function isGreaterThan (num1, num2) {

function isGreaterThan(num1, num2) {
// TODO: write code in this function body to pass the tests

if (num1 > num2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if statement is not needed here as num1 > num2 is already a boolean condition, so you can just return that value such as:

return num1 > num2

return true;
} else {
return false;
}
}

// This function should return the lowest number in the passed array
function findLowest (nums) {

function findLowest(nums) {
// TODO: write code in this function body to pass the tests

let i = 0;
var lowest = nums[i];
for (i = 0; i < nums.length; i++) {
if (lowest > nums[i]) {
lowest = nums[i];
}
}
return lowest;
}

module.exports = {
a: isArrayEmpty,
b: isGreaterThan,
c: findLowest
}
c: findLowest,
};
86 changes: 69 additions & 17 deletions src/conditional-flow/string-conditions.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,73 @@
// This function should return true if the passed string is equal to "Hello"
function isHello (val1) {

function isHello(val1) {
if (val1 === "Hello") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val1 === 'Hello' is a boolean statement, so you don't need the if statement here. You could just return

return val1 === "Hello"

return true;
} else {
return false;
}
// TODO: write code in this function body to pass the tests

}

// This function should return true if the passed string is not equal to "Hello"
function isNotHello (val1) {

function isNotHello(val1) {
if (val1 !== "Hello") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, the if statement here isn't required as val1 !== "Hello" is a boolean statement.

return true;
} else {
return false;
}
// TODO: write code in this function body to pass the tests

}

// This function should return true if the string val1 is is longer
// than string val2
function isLongerThan (val1, val2) {

function isLongerThan(val1, val2) {
if (val1.length > val2.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is not needed :)

return true;
} else {
return false;
}
// TODO: write code in this function body to pass the tests

}

// This function should return true if the string passed in the function's first
// argument has an odd number of vowels

function hasOddNumberVowels (val1) {
function hasOddNumberVowels(val1) {
let count = 0;
let smallVal = val1.toLowerCase();
for (i = 0; i < smallVal.length; i++) {
if (
smallVal[i] == "a" ||
smallVal[i] == "i" ||
smallVal[i] == "e" ||
smallVal[i] == "o" ||
smallVal[i] == "u"
) {
count++;
}
}
if (count % 2 === 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement isn't required as count % 2 === 1 is a boolean statement, so you could just return it :)

return true;
} else {
return false;
}

// TODO: write code in this function body to pass the tests

}

// this function should return the middle character of a string if it has an odd number
// of characters. If there are an even number of characters the function should return
// the middle two letters

function getMiddleLetter (val1) {
function getMiddleLetter(val1) {
// TODO: write code in this function body to pass the tests

if (val1.length % 2 === 1) {
let oddIndex = Math.floor(val1.length / 2);
return val1[oddIndex];
} else {
let evenIndex = val1.length / 2;
return val1[evenIndex - 1] + val1[evenIndex];
}
}

// This function should return the name of the season for the provided
Expand All @@ -47,9 +79,29 @@ function getMiddleLetter (val1) {
// Summer - June to August
// Autumn - September to November
// Winter - December to February
function seasonForMonth (monthName) {

function seasonForMonth(monthName) {
// TODO: write code in this function body to pass the tests
let LowerCaseMonthName = monthName.toLowerCase();
switch (LowerCaseMonthName) {
case "march":
case "april":
case "may":
return "Spring";
case "june":
case "july":
case "august":
return "Summer";
case "september":
case "october":
case "november":
return "Autumn";
case "december":
case "january":
case "february":
return "Winter";
default:
return "";
}
}

module.exports = {
Expand All @@ -58,5 +110,5 @@ module.exports = {
c: isLongerThan,
d: hasOddNumberVowels,
e: getMiddleLetter,
f: seasonForMonth
}
f: seasonForMonth,
};
14 changes: 7 additions & 7 deletions src/data-types/arrays/accessing-elements.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// do not edit this section
const cities = ['London', 'Shanghai', 'New York', 'Delhi', 'Kuala Lumpur']
const cities = ["London", "Shanghai", "New York", "Delhi", "Kuala Lumpur"];

// TODO: write code to pass the tests

// Set names equal to an array containing 'Bob', 'Jane', 'Joanna' in that order
const names = null
const names = ["Bob", "Jane", "Joanna"];

// Set fourthCity to the 4th element in the cities array
const fourthCity = ''
const fourthCity = "Delhi";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. You need to get the "Delhi" and the "London" values via the cities array.


// Set firstCity to the 1st element in the cities array
const firstCity = ''
const firstCity = "London";

// Set lengthOfCitiesArray to the length of the cities array
const lengthOfCitiesArray = NaN
const lengthOfCitiesArray = 5;

// Do not edit this exported object
module.exports = {
a: names,
b: fourthCity,
c: firstCity,
d: lengthOfCitiesArray
}
d: lengthOfCitiesArray,
};
Loading