Skip to content

Commit 2a2f30c

Browse files
Add solutions for challenges josedlujan#1, josedlujan#2, and josedlujan#3
1 parent dbeaac2 commit 2a2f30c

File tree

7 files changed

+156
-1
lines changed

7 files changed

+156
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* highestDigit:
3+
* @param num: Number
4+
* @return : Number (single digit)
5+
* @example : highestDigit(12899) -> 9
6+
*
7+
* Receives a number as an argument.
8+
* Returns the highest digit in that number.
9+
*/
10+
function highestDigit(num) {
11+
// return Math.max(...num.toString()) -- shorter solution that I don't prefer
12+
return Array.prototype.reduce.call(
13+
num.toString(),
14+
(max, digit) => {
15+
return digit > max ? digit : max
16+
}
17+
)
18+
}
19+
20+
console.log(highestDigit(12899))
21+
console.log(highestDigit(8123))
22+
console.log(highestDigit(193))
23+
console.log(highestDigit(1))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* match:
3+
* @param str1: String
4+
* @param str2: String
5+
* @return : Boolean
6+
* @example : match("Jose", "JoSe") -> true
7+
*
8+
* Validates whether the two strings are identical.
9+
*/
10+
function match(str1, str2) {
11+
return str1.toLowerCase() === str2.toLowerCase()
12+
}
13+
14+
console.log(match("Jose", "JoSe"))
15+
console.log(match("water", "wait"))
16+
console.log(match("JOHN", "JOHn"))
17+
console.log(match("ElEmEnTs", "eLeMeNtS"))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* generatort:
3+
* @param n: Number(optional, defaults to 5)
4+
* @yield : n numbers
5+
* @return : undefined
6+
* @example: generatort(2) -> 130,38
7+
* @example: generatort() -> 0,29,82,1,39
8+
*
9+
* Recursive generator function.
10+
* Generates n random numbers in the range (1 - 100)
11+
* If n is not provided, it genetates 5 random numbers.
12+
*/
13+
function* generatort(n=5) {
14+
if (n === 0) return
15+
yield Math.floor(Math.random(100)*100)
16+
yield* generatort(n-1)
17+
}
18+
19+
for (const random of generatort()) {
20+
console.log(random)
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/********************** Helper Functions *********************/
2+
/**
3+
* arrayFromRange:
4+
* @param lowEnd : Number
5+
* @param highEnd: Number
6+
* @return : Array of Numbers
7+
* @example : arrayFromRange(34, 39) -> [34, 35, 36, 37, 38, 39]
8+
9+
* Returns an array of numbers from a range.
10+
* The range is represented by the two arguments.
11+
*/
12+
function arrayFromRange(lowEnd, highEnd) {
13+
return Array.from(
14+
{
15+
length: (highEnd - lowEnd) + 1
16+
},
17+
(_, i) => lowEnd + i
18+
)
19+
}
20+
/*************************************************************/
21+
22+
/**
23+
* rangeEvens:
24+
* @param lowEnd : Number
25+
* @param highEnd: Number
26+
* @return : Array (of numbers)
27+
* @example : rangeEvens(10,20) -> 10,12,14,16,18
28+
*
29+
* Assumes that lowEnd is a positive integer greater than 0.
30+
* Checks that highEnd is positive integer greater that lowEnd.
31+
* Returns all even numbers within the range of lowEnd and highEnd.
32+
*/
33+
function rangeEvens(lowEnd, highEnd) {
34+
if (lowEnd > highEnd) return []
35+
return arrayFromRange(lowEnd, highEnd)
36+
.filter(num => num % 2 == 0)
37+
}
38+
39+
console.log(rangeEvens(10, 20))
40+
console.log(rangeEvens(57, 80))

Community_Github_Challenges_3/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The function must return the sum of all positive even numbers in the list.
2828
### Example
2929
- sum(1,2,3,4,5,6,7,8,9,10,12) = 42
3030
- sum(2,2,2,5,4,2) = 12
31-
- sum(2,1,3,5,6,10) = 12
31+
- sum(2,1,3,5,6,10) = 18
3232

3333
#### Notes
3434
- No notes
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Map.prototype.filter:
3+
* @param {Function} predicate
4+
* @returns {Map}
5+
* @example new Map([['a', 1], ['b', 2]]).filter((key, value) => value === 1) -> Map { 'a' => 1 }
6+
*
7+
* Applies the predicate function to each entry of the map.
8+
* Returns a map with entries for which the predicate function returns true.
9+
*/
10+
Map.prototype.filter = function(predicate) {
11+
const results = new Map()
12+
13+
this.forEach((value, key) => {
14+
if (predicate(key, value, map=this)) {
15+
results.set(key, value)
16+
}
17+
})
18+
19+
return results
20+
}
21+
22+
/**
23+
* leftKeys:
24+
* @param {Map} map1
25+
* @param {Map} map2
26+
* @return {Map}
27+
* @example leftKeys({'a':1,'b':2,'c':3},{'a':1,'b':2,'d':9}) -> Map { 'c' => 3 }
28+
*
29+
* Returns a map of all the keys of map1 that are not found in map2.
30+
*/
31+
function leftKeys(map1, map2) {
32+
return map1.filter((key, _) => !map2.has(key))
33+
}
34+
35+
const map1 = new Map([['a', 1], ['b', 2], ['c', 3]])
36+
const map2 = new Map([['a', 1], ['b', 2], ['d', 9]])
37+
console.log(leftKeys(map1, map2))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* SumEvens:
3+
* @param {Array} numArray
4+
* @return {Number}
5+
* @example sumEvens([2,1,3,5,6,10]) = 18
6+
*
7+
* Returns the sum of all positive even numbers in the list.
8+
*/
9+
function sumEvens(numArray) {
10+
return numArray.reduce((sum, num) => {
11+
return num % 2 === 0 && num > 0 ? sum + num : sum
12+
}, 0)
13+
}
14+
15+
console.log(sumEvens([2,1,3,5,6,10]))
16+
console.log(sumEvens([1,2,3,4,5,6,7,8,9,10,12]))
17+
console.log(sumEvens([2,2,2,5,4,2]))

0 commit comments

Comments
 (0)