Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bishoplee/67883ad393e53cf41b0279355af48507 to your computer and use it in GitHub Desktop.
Save bishoplee/67883ad393e53cf41b0279355af48507 to your computer and use it in GitHub Desktop.
JavaScript String Manipulation Main
// Write a function to manipulate the provided string
// 1. Padded Number
// Pad given single numbers in array to look like "10", "05", "16", "02"
const nums = ['2', '4', '25', '10', '3'];
const padNumber = number => (+number < 10) ? `0${number}` : number;
const paddedNum = nums.map(padNumber);
console.log(paddedNum)
// 2. Camel-to-Title
// Convert a given sentence from camelCase to Title Case
const camelCaseText = "the simplestThings in LIFE are alwaysThe best"
const clean = text => text.toLowerCase().trim();
const capitalize = text => `${text[0].toUpperCase()}${text.slice(1)}`;
const splitOnCapital = text => text.replace(/([A-Z])/, (match, capture) => ` ${capture}`);
const noBlanks = text => !!text
const titleCase = text => {
return text
.split(" ")
.map(splitOnCapital)
.map(clean)
.join(' ')
.split(' ')
.filter(noBlanks)
.map(capitalize)
.join(" ")
}
console.log(titleCase(camelCaseText))
// 3. Title-to-Camel
// Convert the Title Case back to camelCase
const newTitle = "These Words Should Go In Pairs"
const convertToCamel = text => {
const words = text.toLowerCase().split(" ")
const newArr = []
// loop over every 2 words and insert as camel_case pair
for (i = 1; i <= words.length; i += 2) {
const wordIndex = i - 1;
const firstWord = words[wordIndex];
let secondWord = words[wordIndex + 1];
secondWord = `${secondWord[0].toUpperCase()}${secondWord.slice(1)}`
newArr.push(`${firstWord}${secondWord}`);
}
return newArr.join(" ")
}
console.log(convertToCamel(newTitle))
// 4. Passage Counter
// How many seconds will it take to read the provided text?
// If it goes past 60 seconds, quote in minutes!
const passage = `The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog. Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs. Waltz, bad nymph, for quick jigs vex! Fox nymphs grab quick-jived waltz. Brick quiz whangs jumpy veldt fox. Bright vixens jump; dozy fowl quack. Quick wafting zephyrs vex bold Jim. Quick zephyrs blow, vexing daft Jim. Sex-charged fop blew my junk TV quiz. How quickly daft jumping zebras vex. Two driven jocks help fax my big quiz. Quick, Baz, get my woven flax jodhpurs! "Now fax quiz Jack!" my brave ghost pled. Five quacking zephyrs jolt my wax bed. Flummoxed by job, kvetching W. zaps Iraq. Cozy sphinx waves quart jug of bad milk.`
const timeToRead = text => {
const averageReadingTime = 200 // Humans read an average of 200 words per minute
const cleanedPassage = passage.split(" ").filter(el => el.trim() !== "")
const timeInMinutes = cleanedPassage.length / averageReadingTime;
return timeInMinutes < 1
? duration = `${Math.round(timeInMinutes * 60)}s`
: duration = `${Math.round(timeInMinutes)}mins`
}
console.log(timeToRead(passage))
// 5. Pig Latin
// Convert any word to Pig Latin, see how to convert here => https://en.wikipedia.org/wiki/Pig_Latin
const word = "hamlet"
const pigLatin = word => {
const vowel = /[aeiou]/i;
const firstVowelIndex = word.indexOf(word.match(vowel)[0]);
const startsWithVowel = firstVowelIndex === 0;
return startsWithVowel
? `${word}ay`
: `${word.slice(firstVowelIndex)}${word.substr(0, firstVowelIndex)}ay`;
}
console.log(pigLatin(word));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment