Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Javascript/Strings/CheckCamelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// CheckCamelCase method checks the given string is in camelCase or not.

const checkCamelCase = varName => {
// firstly, check that input is a string or not.
if (typeof varName !== 'string') {
return 'not a string.'
}

const reg = /^[a-z][A-Za-z]*$/
return reg.test(varName)
}

console.log(checkCamelCase('myVar'))
console.log(checkCamelCase('MyVar'))
console.log(checkCamelCase('my-var'))
15 changes: 15 additions & 0 deletions Javascript/Strings/CheckFlatCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// checkFlatCase method checks if the given string is in flatcase or not.

const checkFlatCase = varname => {
// firstly, check that input is a string or not.
if (typeof varname !== 'string') {
return 'not a string.'
}

const reg = /^[a-z]*$/
return reg.test(varname)
}

console.log(checkFlatCase('myvar'))
console.log(checkFlatCase('MyVar'))
console.log(checkFlatCase('my-var'))
15 changes: 15 additions & 0 deletions Javascript/Strings/CheckKebabCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// CheckKebabCase method checks the given string is in kebab-case or not.

const checkSnakeCase = varName => {
// firstly, check that input is a string or not.
if (typeof varName !== 'string') {
return 'not a string.'
}

const reg = /(.*?)-([a-zA-Z])*/
return reg.test(varName)
}

console.log(checkSnakeCase('myVar'))
console.log(checkSnakeCase('my_var'))
console.log(checkSnakeCase('my-var'))
21 changes: 21 additions & 0 deletions Javascript/Strings/CheckPalindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Palindrome check is case sensitive; i.e. Aba is not a palindrome

const checkPalindrome = str => {
// check that input is a string
if (typeof str !== 'string') {
return 'Not a string'
}
if (str.length === 0) {
return 'Empty string'
}
// Reverse only works with array, thus convert the string to array, reverse it and convert back to string
// return as palindrome if the reversed string is equal to the input string
const reversed = [...str].reverse().join('')
return str === reversed ? 'Palindrome' : 'Not a Palindrome'
}

console.log(checkPalindrome('hello'))
console.log(checkPalindrome(12))
console.log(checkPalindrome(null))
console.log(checkPalindrome(undefined))
console.log(checkPalindrome('level'))
15 changes: 15 additions & 0 deletions Javascript/Strings/CheckPascalCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// CheckPascalCase method checks the given string is in PascalCase or not.

const checkPascalCase = VarName => {
// firstly, check that input is a string or not.
if (typeof VarName !== 'string') {
return 'not a string.'
}

const reg = /^[A-Z][A-Za-z]*$/
return reg.test(VarName)
}

console.log(checkPascalCase('myVar'))
console.log(checkPascalCase('MyVar'))
console.log(checkPascalCase('my-var'))
15 changes: 15 additions & 0 deletions Javascript/Strings/CheckSnakeCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// CheckSnakeCase method checks the given string is in snake_case or not.

const checkSnakeCase = varName => {
// firstly, check that input is a string or not.
if (typeof varName !== 'string') {
return 'not a string.'
}

const reg = /(.*?)_([a-zA-Z])*/
return reg.test(varName)
}

console.log(checkSnakeCase('myVar'))
console.log(checkSnakeCase('my_var'))
console.log(checkSnakeCase('my-var'))
23 changes: 23 additions & 0 deletions Javascript/Strings/ConsonantCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Given a string of words or phrases, count the number of consonants.

const vowelCount = value => {
if (typeof value !== 'string') {
return 'not a string'
}
const vowels = ['a', 'e', 'i', 'o', 'u']

let countConsonants = 0

for (let i = 0; i < value.length; i++) {
const char = value[i].toLowerCase()
if (!vowels.includes(char)) {
countConsonants++
}
}
return countConsonants
}

console.log(vowelCount('hello'))
console.log(vowelCount('rythm'))
console.log(vowelCount(20))
console.log(vowelCount(null))
19 changes: 19 additions & 0 deletions Javascript/Strings/ReverseString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Reverse a given string

function reverseString (string) {
if (typeof string !== 'string') {
return 'not a string.'
}
let reversedString = ''
let index

for (index = string.length - 1; index >= 0; index--) {
reversedString += string[index]
}

return reversedString
}

console.log(reverseString('hello'))
console.log(reverseString(100))
console.log(reverseString('world'))
23 changes: 23 additions & 0 deletions Javascript/Strings/VowelCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Given a string of words or phrases, count the number of vowels.

const vowelCount = value => {
if (typeof value !== 'string') {
return 'not a string'
}
const vowels = ['a', 'e', 'i', 'o', 'u']

let countVowels = 0

for (let i = 0; i < value.length; i++) {
const char = value[i].toLowerCase()
if (vowels.includes(char)) {
countVowels++
}
}
return countVowels
}

console.log(vowelCount('hello'))
console.log(vowelCount('rythm'))
console.log(vowelCount(20))
console.log(vowelCount(null))