Skip to content

Commit 68f59b5

Browse files
committed
A bunch more completed code wars challenges
1 parent 38aa850 commit 68f59b5

File tree

8 files changed

+125
-1
lines changed

8 files changed

+125
-1
lines changed

alphabetPosition.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Given a string, replace every letter with its position in the alphabet.
3+
* If anything in the text isn't a letter, ignore it and don't return it.
4+
* "a" = 1, "b" = 2, etc.
5+
*/
6+
7+
const alphabetPosition = (text) => {
8+
const alphabet = "abcdefghijklmnopqrstuvwxyz".split(''),
9+
matchingChars = text.toLowerCase().split('').filter(char => alphabet.indexOf(char) >= 0)
10+
return matchingChars.map(char => alphabet.indexOf(char) + 1).join(' ')
11+
}
12+
13+
/**
14+
* Solution:
15+
*
16+
* Step 1: Split text into an array of characters and return only characters that exist in the alphabet array
17+
* i.e they are an index in the alphabet array that is 0 or greater.
18+
*
19+
* Note: The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
20+
*
21+
* Step 2: Return the index of each matching character in the alphabet array, and add 1 because the instructions specify a to start at 1 not 0.
22+
* Then use join to turn it back into a string, separated by a space (as per the specified output).
23+
*
24+
* Note: The join() method creates and returns a new string by concatenating all of the elements in an array.
25+
*/
26+
27+
console.log(alphabetPosition("The sunset sets at twelve o' clock.")) // -> "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"

filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const filter = [1,2,3,4].filter((num)=>{ return num > 3})
1+
const filter = [1,2,3,4].filter((num) => num > 3)
22

33
console.log(filter);

noSpaces.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Remove the spaces from the string, then return the resultant string.
2+
const noSpaces = str => str.split(' ').join('')
3+
4+
console.log(noSpaces('8 8 Bi fk8h B 8 BB8B B B B888 c hl8 BhB fd'));

numberReverse.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Convert number to reversed array of digits
3+
* Given a random non-negative number, you have to return the digits of this number within an array in reverse order.
4+
*
5+
* Example (Input => Output):
6+
* 35231 => [1,3,2,5,3]
7+
* 0 => [0]
8+
*/
9+
10+
const digitize = (num) => {
11+
return num.toString().split('').reverse().map(Number)
12+
// return num.toString().split('').reverse().map(n => Number(n))
13+
}
14+
15+
console.log(digitize(823947592046))
16+

pigLatin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.
3+
*
4+
* Examples
5+
* pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
6+
* pigIt('Hello world !'); // elloHay orldway !
7+
*/
8+
9+
const pigify = (str) => str.split(' ').map(word => word.slice(1) + word.charAt(0) + 'ay').join(' ')
10+
11+
console.log(pigify('Pig latin is cool'));

romanNumerals.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.
3+
* Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.
4+
* In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
5+
* Example: solution(1000); // should return 'M'
6+
*/
7+
8+
const romanNumeralGenerator = (num) => {
9+
let romanChars = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
10+
str = ''
11+
12+
for (key in romanChars) {
13+
while (num >= romanChars[key]) {
14+
str += key
15+
num -= romanChars[key]
16+
}
17+
}
18+
19+
return str
20+
}
21+
22+
console.log(romanNumeralGenerator(1990))
23+
24+
/**
25+
* The addition assignment (+=) operator adds the value of the right operand to a variable and assigns the result to the variable.
26+
* The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible.
27+
*
28+
* The subtraction assignment (-=) operator subtracts the value of the right operand from a variable and assigns the result to the variable.
29+
*/
30+

summation.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0.
3+
*
4+
* For example:
5+
* summation(2) -> 3
6+
* 1 + 2
7+
*
8+
* summation(8) -> 36
9+
* 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8
10+
*/
11+
12+
const summation = (num) => {
13+
const numArr = Array.from({length: num}).map((i, n) => n + 1),
14+
sumArr = numArr.reduce((accumulator, currentVal) => accumulator + currentVal, initialVal = 0)
15+
return sumArr
16+
}
17+
18+
console.log(summation(10))

textCorrect.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Character recognition software is widely used to digitise printed texts. Thus the texts can be edited, searched and stored on a computer.
3+
* When documents (especially old ones written with a typewriter) are digitised, character recognition softwares often make mistakes.
4+
*
5+
* Your task is to correct the errors in the digitised text. You only have to handle the following mistakes:
6+
* S is misinterpreted as 5
7+
* O is misinterpreted as 0
8+
* I is misinterpreted as 1
9+
*
10+
* E.g '51NGAP0RE' => 'SINGAPORE'
11+
*
12+
* The test cases contain numbers only by mistake.
13+
*/
14+
15+
const characters = { 5: 'S', 0: 'O', 1: 'I' }
16+
const correct = (str) => str.split('').map(char => characters[char] ? char.replace(char, characters[char]) : char).join('')
17+
18+
console.log(correct('51NGAP0RE'))

0 commit comments

Comments
 (0)