diff --git a/Javascript/Strings/CheckCamelCase.js b/Javascript/Strings/CheckCamelCase.js new file mode 100644 index 00000000..941facc0 --- /dev/null +++ b/Javascript/Strings/CheckCamelCase.js @@ -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')) diff --git a/Javascript/Strings/CheckFlatCase.js b/Javascript/Strings/CheckFlatCase.js new file mode 100644 index 00000000..098a2663 --- /dev/null +++ b/Javascript/Strings/CheckFlatCase.js @@ -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')) diff --git a/Javascript/Strings/CheckKebabCase.js b/Javascript/Strings/CheckKebabCase.js new file mode 100644 index 00000000..a1d85627 --- /dev/null +++ b/Javascript/Strings/CheckKebabCase.js @@ -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')) diff --git a/Javascript/Strings/CheckPalindrome.js b/Javascript/Strings/CheckPalindrome.js new file mode 100644 index 00000000..dc1dbbba --- /dev/null +++ b/Javascript/Strings/CheckPalindrome.js @@ -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')) diff --git a/Javascript/Strings/CheckPascalCase.js b/Javascript/Strings/CheckPascalCase.js new file mode 100644 index 00000000..f8f1215f --- /dev/null +++ b/Javascript/Strings/CheckPascalCase.js @@ -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')) diff --git a/Javascript/Strings/CheckSnakeCase.js b/Javascript/Strings/CheckSnakeCase.js new file mode 100644 index 00000000..d465497a --- /dev/null +++ b/Javascript/Strings/CheckSnakeCase.js @@ -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')) diff --git a/Javascript/Strings/ConsonantCount.js b/Javascript/Strings/ConsonantCount.js new file mode 100644 index 00000000..594006a0 --- /dev/null +++ b/Javascript/Strings/ConsonantCount.js @@ -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)) diff --git a/Javascript/Strings/ReverseString.js b/Javascript/Strings/ReverseString.js new file mode 100644 index 00000000..6724a4b1 --- /dev/null +++ b/Javascript/Strings/ReverseString.js @@ -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')) diff --git a/Javascript/Strings/VowelCount.js b/Javascript/Strings/VowelCount.js new file mode 100644 index 00000000..9578b861 --- /dev/null +++ b/Javascript/Strings/VowelCount.js @@ -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))