Skip to content

Commit 72df856

Browse files
authored
Remove First and Last Character (8 kyu) in JS
1 parent 70f208e commit 72df856

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// 1 Plain solution
2+
function removeChar(str) {
3+
let result = '';
4+
5+
for (let i = 1; i < str.length - 1; i++) {
6+
result += str[i];
7+
}
8+
9+
return result;
10+
};
11+
12+
// 2 Optimized solution
13+
function removeChar(str) {
14+
return str
15+
.split('')
16+
.filter((s, i) => i && i < str.length - 1 && s)
17+
.join('');
18+
};
19+
20+
// 3 Clever solution
21+
function removeChar(str) {
22+
return str.slice(1, str.length - 1);
23+
};
24+
25+
// 4 Coding golf
26+
removeChar = str => str.slice(1, -1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Remove First and Last Character (8 kyu)
2+
3+
https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0
4+
5+
It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters.

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,17 @@ JS Video Tutorial) :
729729
/ [Java]
730730
/ [Go]
731731
<br>
732+
**[(8 kyu) Convert a string to an array](https://www.codewars.com/kata/57e76bc428d6fbc2d500036d/)** ([Description](<8kyu/(8%20kyu)%20Convert%20a%20string%20to%20an%20array/(8%20kyu)%20Convert%20a%20string%20to%20an%20array.md>) +
733+
JS Video Tutorial) :
734+
[[JavaScript](<8kyu/(8%20kyu)%20Convert%20a%20string%20to%20an%20array/(8%20kyu)%20Convert%20a%20string%20to%20an%20array.js>)]
735+
/ [TypeScript]
736+
/ [Python]
737+
/ [Rust]
738+
/ [C++]
739+
/ [C#]
740+
/ [Java]
741+
/ [Go]
742+
<br>
732743
**[(8 kyu) Convert boolean values to strings 'Yes' or 'No'.](https://www.codewars.com/kata/544675c6f971f7399a000e79/)** ([Description](<8kyu/(8%20kyu)%20Convert%20boolean%20values%20to%20strings%20Yes%20or%20No/(8%20kyu)%20Convert%20boolean%20values%20to%20strings%20Yes%20or%20No.md>) +
733744
JS Video Tutorial) :
734745
[[JavaScript](<8kyu/(8%20kyu)%20Convert%20boolean%20values%20to%20strings%20Yes%20or%20No/(8%20kyu)%20Convert%20boolean%20values%20to%20strings%20Yes%20or%20No.js>)]
@@ -882,6 +893,18 @@ JS Video Tutorial) :
882893
/ [[Java](<8kyu/(8%20kyu)%20Opposite%20number/(8%20kyu)%20Opposite%20number.java>)]
883894
/ [[Go](<8kyu/(8%20kyu)%20Opposite%20number/(8%20kyu)%20Opposite%20number.go>)]
884895
<br>
896+
**[(8 kyu) Remove First and Last Character](https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0)**
897+
([Description](<8kyu/(8%20kyu)%20Remove%20First%20and%20Last%20Character/(8%20kyu)%20Remove%20First%20and%20Last%20Character.md>) +
898+
JS Video Tutorial) :
899+
[[JavaScript](<8kyu/(8%20kyu)%20Remove%20First%20and%20Last%20Character/(8%20kyu)%20Remove%20First%20and%20Last%20Character.js>)]
900+
/ [TypeScript]
901+
/ [Python]
902+
/ [Rust]
903+
/ [C++]
904+
/ [C#]
905+
/ [Java]
906+
/ [Go]
907+
<br>
885908
**[(8 kyu) Remove String Spaces](https://www.codewars.com/kata/remove-string-spaces)**
886909
([Description](<8kyu/(8%20kyu)%20Remove%20String%20Spaces/(8%20kyu)%20Remove%20String%20Spaces.md>) +
887910
[JS Video Tutorial](https://www.youtube.com/watch?v=GgbU1aeeFmo)) :

0 commit comments

Comments
 (0)