-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philip Obosi
committed
Feb 19, 2019
1 parent
58a4c4f
commit cf5a497
Showing
10 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
// Reverses a string | ||
function reverseString(text) { | ||
return [...text].reduce((acc, char) => char + acc, '') | ||
} | ||
|
||
// Reverses an integer | ||
function reverseInteger(num) { | ||
let reversedNumber = parseInt(reverseString(num.toString())) | ||
|
||
return (reversedNumber * Math.sign(num)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* CHALLENGE | ||
Given an integer, return an integer that is the reverse | ||
ordering of that which was received. E.g | ||
reverseInteger(-123) // should return -321 | ||
*/ | ||
|
||
|
||
function reverseString(text) { | ||
return [...text].reduce((acc, char) => char + acc, '') | ||
} | ||
|
||
function reverseInteger(num) { | ||
let reversedNumber = parseInt(reverseString(num.toString())) | ||
return (reversedNumber * Math.sign(num)) | ||
} | ||
|
||
|
||
|
||
module.exports = reverseInteger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const reverseInteger = require('./index-START'); | ||
|
||
test('reverseInteger function exists', () => { | ||
expect(reverseInteger).toBeDefined(); | ||
}); | ||
|
||
test('reverseInteger handles 0 as an input', () => { | ||
expect(reverseInteger(0)).toEqual(0); | ||
}); | ||
|
||
test('reverseInteger flips a positive number', () => { | ||
expect(reverseInteger(5)).toEqual(5); | ||
expect(reverseInteger(15)).toEqual(51); | ||
expect(reverseInteger(90)).toEqual(9); | ||
expect(reverseInteger(2359)).toEqual(9532); | ||
}); | ||
|
||
test('reverseInteger flips a negative number', () => { | ||
expect(reverseInteger(-5)).toEqual(-5); | ||
expect(reverseInteger(-15)).toEqual(-51); | ||
expect(reverseInteger(-90)).toEqual(-9); | ||
expect(reverseInteger(-2359)).toEqual(-9532); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// IMPERATIVE APPROACH | ||
function pigLatin(str) { | ||
// Convert string to lowercase | ||
str = str.toLowerCase(); | ||
// Initialize array of vowels | ||
const vowels = ["a", "e", "i", "o", "u"]; | ||
// Initialize vowel index to 0 | ||
let vowelIndex = 0; | ||
|
||
if (vowels.includes(str[0])) { | ||
// If first letter is a vowel | ||
return str + "way"; | ||
} else { | ||
// If the first letter isn't a vowel i.e is a consonant | ||
for (let char of str) { | ||
// Loop through until the first vowel is found | ||
if (vowels.includes(char)) { | ||
// Store the index at which the first vowel exists | ||
vowelIndex = str.indexOf(char); | ||
break; | ||
} | ||
} | ||
// Compose final string | ||
return str.slice(vowelIndex) + str.slice(0, vowelIndex) + "ay"; | ||
} | ||
} | ||
|
||
|
||
// DECLARATIVE APPROACH | ||
function pigLatin(str) { | ||
return str | ||
.replace(/^([aeiouy])(.*)/, '$1$2way') | ||
.replace(/^([^aeiouy]+)(.*)/, '$2$1ay'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
Translate the provided string to pig latin by following the rules below: | ||
- For words that begin with consonant sounds, the consonant before the initial vowel should be moved to the end of the word sequence and "ay" affixed. E.g | ||
- "pig" = "igpay" | ||
- For words that begin with consonant clusters, the clusters should be moved to the end of the word sequence and "ay" affixed. E.g | ||
- "glove" = "oveglay" | ||
- For words that begin with vowel sounds, simply add "way" to the end of the word. E.g | ||
- "explain" = "explainway” | ||
*/ | ||
|
||
function pigLatin(str) { | ||
// Code goes here | ||
} | ||
|
||
module.exports = pigLatin; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// USING .REPLACE() | ||
|
||
function searchReplace(str, word, newWord) { | ||
|
||
if (word[0] === word[0].toUpperCase()) { | ||
newWord = newWord[0].toUpperCase() + newWord.slice(1) | ||
} | ||
return str.replace(word, newWord) | ||
|
||
} | ||
|
||
|
||
// USING REGULAR EXPRESSIONS | ||
|
||
function searchReplace(str, word, newWord) { | ||
let regex = new RegExp(word, "gi"); | ||
|
||
if (/[A-Z]/.test(word[0])) { | ||
|
||
newWord = newWord.charAt(0).toUpperCase() + newWord.slice(1); | ||
} | ||
|
||
return str.replace(regex, newWord) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function searchReplace(str, word, newWord) { | ||
let regex = new RegExp(word, "gi") | ||
|
||
if (/[A-Z]/.test(word[0])) { | ||
|
||
newWord = newWord.charAt(0).toUpperCase() + newWord.slice(1); | ||
} | ||
|
||
return str.replace(regex, newWord) | ||
} | ||
|
||
|
||
|
||
module.exports = searchReplace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const searchReplace = require('./index-START') | ||
|
||
test('searchReplace is a function', () => { | ||
expect(typeof searchReplace).toEqual('function') | ||
}) | ||
|
||
test('replaces Sleeping with Sitting', () => { | ||
expect(searchReplace("He is Sleeping on the couch", "Sleeping", "sitting")).toEqual("He is Sitting on the couch") | ||
}) | ||
|
||
|