-
Notifications
You must be signed in to change notification settings - Fork 61
Paul JS Fundamentals #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
60df715
1ad3fe4
4f3c681
518d1ff
b59d8da
18b71ce
16b1f05
5e58721
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| // than or equal to lower AND less than or equal to upper. | ||
| // Implement this with a single condition. | ||
| function isInRange (num, lower, upper) { | ||
|
|
||
| return (lower<=num&&num<=upper); | ||
| // TODO: write code in this function body to pass the tests | ||
|
|
||
| } | ||
|
|
@@ -11,7 +11,11 @@ function isInRange (num, lower, upper) { | |
| // to "Hello" or "Goodbye". Implement this with a single | ||
| // if statement. | ||
| function isHelloOrGoodbye (val1) { | ||
|
|
||
| if (val1 === "Hello" || val1 === "Goodbye") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be simplified to return val1 === 'Hello' || val1 === 'Goodbye' |
||
| return true; | ||
| } else { | ||
| return false | ||
| } | ||
| // TODO: write code in this function body to pass the tests | ||
|
|
||
| } | ||
|
|
@@ -29,7 +33,17 @@ function isHelloOrGoodbye (val1) { | |
| // 13-19 | Teenager | ||
| // 20+ | Adult | ||
| function getAgeDescription (age) { | ||
|
|
||
| if (age === 0) { | ||
| return "Baby" | ||
| } else if (1<=age&&age<=4){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might just be a personal thing for me, but I find it harder to read conditions laid out like
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, initially I tried |
||
| return "Toddler" | ||
| } else if (5<=age&&age<=12){ | ||
| return "Child" | ||
| } else if (13<=age&&age<=19){ | ||
| return "Teenager" | ||
| } else if (age>=20){ | ||
| return "Adult" | ||
| } | ||
| // TODO: write code in this function body to pass the tests | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,21 +2,26 @@ | |
|
|
||
| // This function should return true if there are no elements in the array, false otherwise | ||
| function isArrayEmpty (array) { | ||
|
|
||
| return array.length === 0 ? true : false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the ternary operator here. You could just use 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) { | ||
|
|
||
| return num1 > num2 == true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the return num1 > num2 == true
return true == true
// or (depending on the values of num1/num2)
return false == trueSo you could simplify it to: return num1 > num2 |
||
| // TODO: write code in this function body to pass the tests | ||
|
|
||
| } | ||
|
|
||
| // This function should return the lowest number in the passed array | ||
| function findLowest (nums) { | ||
|
|
||
| let lowest = nums[0]; | ||
| for (i=1;i<=nums.length;i++){ | ||
| if (nums[i] < lowest) { | ||
| lowest = nums[i] | ||
| } | ||
| } return lowest | ||
| // TODO: write code in this function body to pass the tests | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,38 @@ | ||
| // This function should return true if the passed string is equal to "Hello" | ||
| function isHello (val1) { | ||
|
|
||
| return val1 === "Hello" | ||
| // 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) { | ||
|
|
||
| return !isHello(val1) | ||
| // 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) { | ||
|
|
||
| return val1.length>val2.length | ||
| // 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) { | ||
|
|
||
| let vowelCount=0; | ||
| for (i=0; i<val1.length; i++) { | ||
| if (val1.charAt(i).toLowerCase() === 'a'|| | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works well! Alternative way of doing this could be to have an array containing the vowels, and check if the current letter is inside that array. The advantage of this is that it's a little more readable. For example: const vowels = ['a', 'e', 'i', 'o', 'u']
for(let i = 0; i < val1.length; i++) {
if(vowels.includes(val1.charAt(i)) {
vowelCount++
}
} |
||
| val1.charAt(i).toLowerCase() === 'e'|| | ||
| val1.charAt(i).toLowerCase() === 'o'|| | ||
| val1.charAt(i).toLowerCase() === 'i'|| | ||
| val1.charAt(i).toLowerCase() === 'u'){ | ||
| vowelCount++ | ||
| }} | ||
| return vowelCount%2 === 1; | ||
| // TODO: write code in this function body to pass the tests | ||
|
|
||
| } | ||
|
|
@@ -34,8 +42,11 @@ function hasOddNumberVowels (val1) { | |
| // the middle two letters | ||
|
|
||
| function getMiddleLetter (val1) { | ||
| let sliceValue = val1.length/2; | ||
| // TODO: write code in this function body to pass the tests | ||
|
|
||
| return (val1.length%2===1) ? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great use of the ternary operator! |
||
| val1.slice(sliceValue,sliceValue+1) : | ||
| val1.slice(sliceValue-1,sliceValue+1) | ||
| } | ||
|
|
||
| // This function should return the name of the season for the provided | ||
|
|
@@ -48,7 +59,15 @@ function getMiddleLetter (val1) { | |
| // Autumn - September to November | ||
| // Winter - December to February | ||
| function seasonForMonth (monthName) { | ||
|
|
||
| if (monthName==="March"||monthName==="April"||monthName==="May"){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works well! As I mentioned above, it might be a little bit more readable if you used arrays containing the seasons and to check them like that. Both are good implementations though! |
||
| return "Spring" | ||
| } else if (monthName==="June"||monthName==="July"||monthName==="August"){ | ||
| return "Summer" | ||
| } else if (monthName==="September"||monthName==="October"||monthName==="November"){ | ||
| return "Autumn" | ||
| } else if (monthName==="December"||monthName==="January"||monthName==="February"){ | ||
| return "Winter" | ||
| } else return "" | ||
| // TODO: write code in this function body to pass the tests | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,16 +6,16 @@ const secondName = 'Smith' | |
| // TODO: Update the code using Javascript string operations and the variables above so that the tests pass. | ||
|
|
||
| // Set this variable to firstName and secondName concatenated | ||
| const fullName = null | ||
| const fullName = `${firstName} ${secondName}` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of the JS string interpolation! |
||
|
|
||
| // Set this variable to the 10th character of the alphabet variable | ||
| const tenthCharacterOfAlphabet = null | ||
| const tenthCharacterOfAlphabet = alphabet.charAt(9) | ||
|
|
||
| // Set this variable by calling a method on the alphabet variable to transform it to lower case | ||
| const lowerCaseAlphabet = null | ||
| const lowerCaseAlphabet = alphabet.toLowerCase() | ||
|
|
||
| // Set this variable by using a property on the alphabet variable to get it's length | ||
| const numberOfLettersInAlphabet = null | ||
| const numberOfLettersInAlphabet = alphabet.length | ||
|
|
||
| // do not edit the exported object. | ||
| module.exports = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note - the brackets aren't really needed here, but if you find it easier to read like this then that's no problem :)