Skip to content

Commit 45ba75c

Browse files
author
juanantonioledesma
committed
Add "Find Cracker." kata
1 parent eec7ae4 commit 45ba75c

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

6-kyu/find-cracker.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<h1>Find Cracker. <sup><sup>6 Kyu</sup></sup></h1>
2+
3+
<sup>
4+
<a href="https://www.codewars.com/kata/59f70440bee845599c000085">
5+
<strong>LINK TO THE KATA</strong>
6+
</a> - <code>FUNDAMENTALS</code> <code>ARRAYS</code>
7+
</sup>
8+
9+
## Description
10+
11+
Someone was hacking the score. Each student's record is given as an array The objects in the array are given again as an array of scores for each name and total score.
12+
13+
Example:
14+
15+
```javascript
16+
var array = [
17+
['name1', 445, ['B', 'A', 'A', 'C', 'A', 'A']],
18+
['name2', 110, ['B', 'A', 'A', 'A']],
19+
['name3', 200, ['B', 'A', 'A', 'C']],
20+
['name4', 200, ['A', 'A', 'A', 'A', 'A', 'A', 'A']],
21+
]
22+
```
23+
24+
The scores for each grade is:
25+
26+
- A: 30 points
27+
- B: 20 points
28+
- C: 10 points
29+
- D: 5 points
30+
- Everything else: 0 points
31+
32+
If there are 5 or more courses and all courses has a grade of B or above, additional 20 points are awarded. After all the calculations, the total score should be capped at 200 points.
33+
34+
Returns the name of the hacked name as an array when scoring with this rule.
35+
36+
```javascript
37+
var array = [
38+
['name1', 445, ['B', 'A', 'A', 'C', 'A', 'A']], // name1 total point is over 200 => hacked
39+
['name2', 110, ['B', 'A', 'A', 'A']], // name2 point is right
40+
['name3', 200, ['B', 'A', 'A', 'C']], // name3 point is 200 but real point is 90 => hacked
41+
,
42+
['name4', 200, ['A', 'A', 'A', 'A', 'A', 'A', 'A']], // name4 point is right
43+
]
44+
45+
return ['name1', 'name3']
46+
```
47+
48+
## Solution
49+
50+
```javascript
51+
const scoreTable = {
52+
A: 30,
53+
B: 20,
54+
C: 10,
55+
D: 5,
56+
}
57+
58+
const calculateGradeScoresSum = array => {
59+
return array.reduce((acc, cur) => {
60+
const currentValue = scoreTable[cur] || 0
61+
return acc + currentValue
62+
}, 0)
63+
}
64+
65+
const hasBonus = array => {
66+
return (
67+
array.length >= 5 && array.every(letter => letter === 'A' || letter === 'B')
68+
)
69+
}
70+
71+
const findHack = array => {
72+
const result = []
73+
74+
for (let i = 0; i < array.length; i++) {
75+
const [studentName, totalScore, gradeScores] = array[i]
76+
77+
if (totalScore > 200) {
78+
result.push(studentName)
79+
continue
80+
}
81+
82+
let gradeScoresSum = calculateGradeScoresSum(gradeScores)
83+
84+
if (hasBonus(gradeScores)) gradeScoresSum += 20
85+
86+
if (gradeScoresSum !== totalScore) result.push(studentName)
87+
}
88+
89+
return result
90+
}
91+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ JavaScript katas on Codewars are programming challenges that help you improve yo
5454
- **[Does my number look big in this?](./6-kyu/does-my-number-look-big-in-this.md)**<br><sup>`ALGORITHMS`</sup>
5555
- **[Duplicate Encoder](./6-kyu/duplicate-encoder.md)**<br><sup>`ALGORITHMS`</sup>
5656
- **[Emotional Sort ( ︶︿︶)](./6-kyu/emotional-sort.md)**<br><sup>`ARRAYS` `FUNDAMENTALS` `SORTING`</sup>
57+
- **[Find Cracker.](./6-kyu/find-cracker.md)**<br><sup>`FUNDAMENTALS` `ARRAYS`</sup>
5758
- **[Find the missing letter](./6-kyu/find-the-missing-letter.md)**<br><sup>`MATHEMATICS` `ALGORITHMS`</sup>
5859
- **[Find the odd int](./6-kyu/find-the-odd-int.md)**<br><sup>`FUNDAMENTALS`</sup>
5960
- **[Find The Parity Outlier](./6-kyu/find-the-parity-outlier.md)**<br><sup>`ALGORITHMS`</sup>

0 commit comments

Comments
 (0)