Skip to content

Commit

Permalink
added pigLatin
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Obosi committed Feb 19, 2019
1 parent 58a4c4f commit cf5a497
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Beginner/IntegerReversal/index-SOLUTION.js
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))
}
19 changes: 19 additions & 0 deletions Beginner/IntegerReversal/index-START.js
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;
23 changes: 23 additions & 0 deletions Beginner/IntegerReversal/test.js
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);
});
2 changes: 1 addition & 1 deletion Beginner/palindromeChecker/index-START.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ e.g palindromeChecker('racecar') // will return true


function palindromeChecker(text) {
v// Code goes here
// Code goes here
}


Expand Down
34 changes: 34 additions & 0 deletions Beginner/pigLatin/index-SOLUTION.js
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');
}
16 changes: 16 additions & 0 deletions Beginner/pigLatin/index-START.js
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 added Beginner/pigLatin/test.js
Empty file.
24 changes: 24 additions & 0 deletions Beginner/searchReplace/index-SOLUTION.js
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)
}
14 changes: 14 additions & 0 deletions Beginner/searchReplace/index-START.js
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
11 changes: 11 additions & 0 deletions Beginner/searchReplace/test.js
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")
})


0 comments on commit cf5a497

Please sign in to comment.