Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 97ef04d

Browse files
Merge pull request #334 from Koddi-Evangelista/feature/string-algorithms-javascript
feat: add string algorithm in javascript
2 parents e9e37f5 + 0fc481b commit 97ef04d

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed

‎Javascript/Strings/CheckCamelCase.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// CheckCamelCase method checks the given string is in camelCase or not.
2+
3+
const checkCamelCase = varName => {
4+
// firstly, check that input is a string or not.
5+
if (typeof varName !== 'string') {
6+
return 'not a string.'
7+
}
8+
9+
const reg = /^[a-z][A-Za-z]*$/
10+
return reg.test(varName)
11+
}
12+
13+
console.log(checkCamelCase('myVar'))
14+
console.log(checkCamelCase('MyVar'))
15+
console.log(checkCamelCase('my-var'))

‎Javascript/Strings/CheckFlatCase.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// checkFlatCase method checks if the given string is in flatcase or not.
2+
3+
const checkFlatCase = varname => {
4+
// firstly, check that input is a string or not.
5+
if (typeof varname !== 'string') {
6+
return 'not a string.'
7+
}
8+
9+
const reg = /^[a-z]*$/
10+
return reg.test(varname)
11+
}
12+
13+
console.log(checkFlatCase('myvar'))
14+
console.log(checkFlatCase('MyVar'))
15+
console.log(checkFlatCase('my-var'))

‎Javascript/Strings/CheckKebabCase.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// CheckKebabCase method checks the given string is in kebab-case or not.
2+
3+
const checkSnakeCase = varName => {
4+
// firstly, check that input is a string or not.
5+
if (typeof varName !== 'string') {
6+
return 'not a string.'
7+
}
8+
9+
const reg = /(.*?)-([a-zA-Z])*/
10+
return reg.test(varName)
11+
}
12+
13+
console.log(checkSnakeCase('myVar'))
14+
console.log(checkSnakeCase('my_var'))
15+
console.log(checkSnakeCase('my-var'))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Palindrome check is case sensitive; i.e. Aba is not a palindrome
2+
3+
const checkPalindrome = str => {
4+
// check that input is a string
5+
if (typeof str !== 'string') {
6+
return 'Not a string'
7+
}
8+
if (str.length === 0) {
9+
return 'Empty string'
10+
}
11+
// Reverse only works with array, thus convert the string to array, reverse it and convert back to string
12+
// return as palindrome if the reversed string is equal to the input string
13+
const reversed = [...str].reverse().join('')
14+
return str === reversed ? 'Palindrome' : 'Not a Palindrome'
15+
}
16+
17+
console.log(checkPalindrome('hello'))
18+
console.log(checkPalindrome(12))
19+
console.log(checkPalindrome(null))
20+
console.log(checkPalindrome(undefined))
21+
console.log(checkPalindrome('level'))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// CheckPascalCase method checks the given string is in PascalCase or not.
2+
3+
const checkPascalCase = VarName => {
4+
// firstly, check that input is a string or not.
5+
if (typeof VarName !== 'string') {
6+
return 'not a string.'
7+
}
8+
9+
const reg = /^[A-Z][A-Za-z]*$/
10+
return reg.test(VarName)
11+
}
12+
13+
console.log(checkPascalCase('myVar'))
14+
console.log(checkPascalCase('MyVar'))
15+
console.log(checkPascalCase('my-var'))

‎Javascript/Strings/CheckSnakeCase.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// CheckSnakeCase method checks the given string is in snake_case or not.
2+
3+
const checkSnakeCase = varName => {
4+
// firstly, check that input is a string or not.
5+
if (typeof varName !== 'string') {
6+
return 'not a string.'
7+
}
8+
9+
const reg = /(.*?)_([a-zA-Z])*/
10+
return reg.test(varName)
11+
}
12+
13+
console.log(checkSnakeCase('myVar'))
14+
console.log(checkSnakeCase('my_var'))
15+
console.log(checkSnakeCase('my-var'))

‎Javascript/Strings/ConsonantCount.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Given a string of words or phrases, count the number of consonants.
2+
3+
const vowelCount = value => {
4+
if (typeof value !== 'string') {
5+
return 'not a string'
6+
}
7+
const vowels = ['a', 'e', 'i', 'o', 'u']
8+
9+
let countConsonants = 0
10+
11+
for (let i = 0; i < value.length; i++) {
12+
const char = value[i].toLowerCase()
13+
if (!vowels.includes(char)) {
14+
countConsonants++
15+
}
16+
}
17+
return countConsonants
18+
}
19+
20+
console.log(vowelCount('hello'))
21+
console.log(vowelCount('rythm'))
22+
console.log(vowelCount(20))
23+
console.log(vowelCount(null))

‎Javascript/Strings/ReverseString.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Reverse a given string
2+
3+
function reverseString (string) {
4+
if (typeof string !== 'string') {
5+
return 'not a string.'
6+
}
7+
let reversedString = ''
8+
let index
9+
10+
for (index = string.length - 1; index >= 0; index--) {
11+
reversedString += string[index]
12+
}
13+
14+
return reversedString
15+
}
16+
17+
console.log(reverseString('hello'))
18+
console.log(reverseString(100))
19+
console.log(reverseString('world'))

‎Javascript/Strings/VowelCount.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Given a string of words or phrases, count the number of vowels.
2+
3+
const vowelCount = value => {
4+
if (typeof value !== 'string') {
5+
return 'not a string'
6+
}
7+
const vowels = ['a', 'e', 'i', 'o', 'u']
8+
9+
let countVowels = 0
10+
11+
for (let i = 0; i < value.length; i++) {
12+
const char = value[i].toLowerCase()
13+
if (vowels.includes(char)) {
14+
countVowels++
15+
}
16+
}
17+
return countVowels
18+
}
19+
20+
console.log(vowelCount('hello'))
21+
console.log(vowelCount('rythm'))
22+
console.log(vowelCount(20))
23+
console.log(vowelCount(null))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /