mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
resolved conflicts
This commit is contained in:
@ -1,19 +1,23 @@
|
|||||||
/*
|
/*
|
||||||
Problem statement and Explanation : https://www.codeproject.com/Tips/162540/Letter-Case-Conversion-Algorithms-Title-Case-Toggl
|
Problem statement and Explanation : https://www.codeproject.com/Tips/162540/Letter-Case-Conversion-Algorithms-Title-Case-Toggl.
|
||||||
|
[Title case](https://en.wikipedia.org/wiki/Title_case) is a style where all words are capitalized. Officially, title case
|
||||||
|
does not capitalize some words, such as very short words like "a" or "is", but for the purposes of this function, a general approach
|
||||||
|
is taken where all words are capitalized regardless of length.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The TitleCaseConversion converts a string into a title case string.
|
* The titleCaseConversion function converts a string into a title case string.
|
||||||
* @param {String} inputString input string
|
* @param {string} inputString The input string which can have any types of letter casing.
|
||||||
* @returns {String}
|
* @returns {string} A string that is in title case.
|
||||||
*/
|
*/
|
||||||
const TitleCaseConversion = (inputString) => {
|
const titleCaseConversion = (inputString) => {
|
||||||
|
if (inputString === '') return ''
|
||||||
// Extract all space separated string.
|
// Extract all space separated string.
|
||||||
const stringCollections = inputString.split(' ').map(word => {
|
const stringCollections = inputString.split(' ').map(word => {
|
||||||
let firstChar = ''
|
let firstChar = ''
|
||||||
// Get a character code by the use charCodeAt method.
|
// Get the [ASCII](https://en.wikipedia.org/wiki/ASCII) character code by the use charCodeAt method.
|
||||||
const firstCharCode = word[0].charCodeAt()
|
const firstCharCode = word[0].charCodeAt()
|
||||||
// If the character code lies between 97 to 122 it means they are in the lower case so convert it.
|
// If the ASCII character code lies between 97 to 122 it means they are in the lowercase so convert it.
|
||||||
if (firstCharCode >= 97 && firstCharCode <= 122) {
|
if (firstCharCode >= 97 && firstCharCode <= 122) {
|
||||||
// Convert the case by use of the above explanation.
|
// Convert the case by use of the above explanation.
|
||||||
firstChar += String.fromCharCode(firstCharCode - 32)
|
firstChar += String.fromCharCode(firstCharCode - 32)
|
||||||
@ -22,9 +26,9 @@ const TitleCaseConversion = (inputString) => {
|
|||||||
firstChar += word[0]
|
firstChar += word[0]
|
||||||
}
|
}
|
||||||
const newWordChar = word.slice(1).split('').map(char => {
|
const newWordChar = word.slice(1).split('').map(char => {
|
||||||
// Get a character code by the use charCodeAt method.
|
// Get the ASCII character code by the use charCodeAt method.
|
||||||
const presentCharCode = char.charCodeAt()
|
const presentCharCode = char.charCodeAt()
|
||||||
// If the character code lies between 65 to 90 it means they are in the upper case so convert it.
|
// If the ASCII character code lies between 65 to 90, it means they are in the uppercase so convert it.
|
||||||
if (presentCharCode >= 65 && presentCharCode <= 90) {
|
if (presentCharCode >= 65 && presentCharCode <= 90) {
|
||||||
// Convert the case by use of the above explanation.
|
// Convert the case by use of the above explanation.
|
||||||
return String.fromCharCode(presentCharCode + 32)
|
return String.fromCharCode(presentCharCode + 32)
|
||||||
@ -32,11 +36,11 @@ const TitleCaseConversion = (inputString) => {
|
|||||||
// Else return the characters without any modification.
|
// Else return the characters without any modification.
|
||||||
return char
|
return char
|
||||||
})
|
})
|
||||||
// return the first converted character and remaining character string.
|
// Return the first converted character and remaining character string.
|
||||||
return firstChar + newWordChar.join('')
|
return firstChar + newWordChar.join('')
|
||||||
})
|
})
|
||||||
// convert all words in a string and return it.
|
// Convert all words in a string and return it.
|
||||||
return stringCollections.join(' ')
|
return stringCollections.join(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TitleCaseConversion
|
export { titleCaseConversion }
|
||||||
|
51
Conversions/test/TitleCaseConversion.test.js
Normal file
51
Conversions/test/TitleCaseConversion.test.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import { titleCaseConversion } from '../TitleCaseConversion'
|
||||||
|
|
||||||
|
describe(('Tests for the titleCaseConversion function'), () => {
|
||||||
|
it('should return an empty string when the input is an empty string', () => {
|
||||||
|
expect(titleCaseConversion('')).toEqual('')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return the input string when the input string is a title case string', () => {
|
||||||
|
expect(titleCaseConversion('A Proper Title Case String')).toEqual('A Proper Title Case String')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is an all-uppercase string', () => {
|
||||||
|
expect(titleCaseConversion('ALL UPPER CASE')).toEqual('All Upper Case')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is a title case string of with spaces', () => {
|
||||||
|
expect(titleCaseConversion('ALL UPPERCASE')).toEqual('All Uppercase')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is a title case string of with no spaces', () => {
|
||||||
|
expect(titleCaseConversion('ALLUPPERCASE')).toEqual('Alluppercase')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is a title case string with punctuation', () => {
|
||||||
|
expect(titleCaseConversion('All Title Case!')).toEqual('All Title Case!')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is an all-lowercase string with no spaces', () => {
|
||||||
|
expect(titleCaseConversion('lowercaseinput')).toEqual('Lowercaseinput')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is an all-lowercase string with spaces', () => {
|
||||||
|
expect(titleCaseConversion('lowercase input')).toEqual('Lowercase Input')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is an all-lowercase string with punctuation', () => {
|
||||||
|
expect(titleCaseConversion('lower, case, input.')).toEqual('Lower, Case, Input.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is an mixed-case string', () => {
|
||||||
|
expect(titleCaseConversion('mixeD CaSe INPuT')).toEqual('Mixed Case Input')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is an mixed-case string with no spaces', () => {
|
||||||
|
expect(titleCaseConversion('mixeDCaSeINPuT')).toEqual('Mixedcaseinput')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return a title case string when input is an mixed-case string with punctuation', () => {
|
||||||
|
expect(titleCaseConversion('mixeD, CaSe, INPuT!')).toEqual('Mixed, Case, Input!')
|
||||||
|
})
|
||||||
|
})
|
@ -56,6 +56,7 @@
|
|||||||
* test
|
* test
|
||||||
* [DecimalToHex](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/test/DecimalToHex.test.js)
|
* [DecimalToHex](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/test/DecimalToHex.test.js)
|
||||||
* [DecimalToRoman](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/test/DecimalToRoman.test.js)
|
* [DecimalToRoman](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/test/DecimalToRoman.test.js)
|
||||||
|
* [TitleCaseConversion](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/test/TitleCaseConversion.test.js)
|
||||||
* [TitleCaseConversion](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/TitleCaseConversion.js)
|
* [TitleCaseConversion](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/TitleCaseConversion.js)
|
||||||
* [UpperCaseConversion](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/UpperCaseConversion.js)
|
* [UpperCaseConversion](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/UpperCaseConversion.js)
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ Trie.prototype.remove = function (word, count) {
|
|||||||
// if the object forms some other objects prefix we dont delete it
|
// if the object forms some other objects prefix we dont delete it
|
||||||
// For checking an empty object
|
// For checking an empty object
|
||||||
// https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
|
// https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
|
||||||
if (child.count <= 0 && (Object.keys(child.children).length && child.childre.constructor === Object)) {
|
if (child.count <= 0 && (Object.keys(child.children).length && child.children.constructor === Object)) {
|
||||||
child.parent.children[child.key] = undefined
|
child.parent.children[child.key] = undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,14 +21,14 @@ const isValid = (board, row, col, k) => {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
const sodokoSolver = (data) => {
|
const sudokuSolver = (data) => {
|
||||||
for (let i = 0; i < 9; i++) {
|
for (let i = 0; i < 9; i++) {
|
||||||
for (let j = 0; j < 9; j++) {
|
for (let j = 0; j < 9; j++) {
|
||||||
if (data[i][j] === '.') {
|
if (data[i][j] === '.') {
|
||||||
for (let k = 1; k <= 9; k++) {
|
for (let k = 1; k <= 9; k++) {
|
||||||
if (isValid(data, i, j, k)) {
|
if (isValid(data, i, j, k)) {
|
||||||
data[i][j] = `${k}`
|
data[i][j] = `${k}`
|
||||||
if (sodokoSolver(data)) {
|
if (sudokuSolver(data)) {
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
data[i][j] = '.'
|
data[i][j] = '.'
|
||||||
@ -44,7 +44,7 @@ const sodokoSolver = (data) => {
|
|||||||
|
|
||||||
// testing
|
// testing
|
||||||
(() => {
|
(() => {
|
||||||
if (sodokoSolver(_board)) {
|
if (sudokuSolver(_board)) {
|
||||||
console.log(_board)
|
console.log(_board)
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
Reference in New Issue
Block a user