Skip to content

Instantly share code, notes, and snippets.

@khalid32
Last active July 15, 2017 05:27
Show Gist options
  • Select an option

  • Save khalid32/504e621d1976e7d62b15ad89a1913440 to your computer and use it in GitHub Desktop.

Select an option

Save khalid32/504e621d1976e7d62b15ad89a1913440 to your computer and use it in GitHub Desktop.
function towerBuilder(nFloors) {
let space = "", star = "", result = [];
for (let i = 1; i <= nFloors; i++) {
space = (" ").repeat(nFloors - i);
star = ("*").repeat((2 * i) - 1);
result.push(space + star + space);
}
return result;
}
/*
Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contains any char.
Examples input/output:
XO("ooxx") => true
XO("xooxx") => false
XO("ooxXm") => true
XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true
XO("zzoo") => false
*/
function XO(str) {
let count = 0;
for(let i = 0; i < str.length; i++){
if(str[i].toLowerCase() === 'o') count++;
else if(str[i].toLowerCase() === 'x') count--;
}
return count === 0;
}
//Sample Tests:
Test.assertEquals(XO('xo'),true);
Test.assertEquals(XO("xxOo"),true);
Test.assertEquals(XO("xxxm"),false);
Test.assertEquals(XO("Oo"),false);
Test.assertEquals(XO("ooom"),false);
/*
Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word.
Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.
*/
String.prototype.toJadenCase = function () {
return this.split(' ').map(function (item) {
return item.replace(item.charAt(0), item.charAt(0).toUpperCase());
}).join(' ');
};
//Sample Tests:
//var str = "How can mirrors be real if our eyes aren't real";
//Test.assertEquals(str.toJadenCase(), "How Can Mirrors Be Real If Our Eyes Aren't Real");
/*
replace every letter with its position in the alphabet. If anything in the text isn't a letter, ignore it and don't return it. a being 1, b being 2, etc.
*/
function alphabetPosition(text) {
var solution = [];
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"]
text = text.replace(/\W*\d+/g, '').toLowerCase().split('');
let j;
for (var i = 0; i < text.length; i++) {
j = alphabet.indexOf(text[i]) + 1;
if (j) solution.push(j);
}
return solution.join(' ');
}
//Sample Tests:
//Test.assertEquals(alphabetPosition("The narwhal bacons at midnight."), "20 8 5 14 1 18 23 8 1 12 2 1 3 15 14 19 1 20 13 9 4 14 9 7 8 20");
//Test.assertEquals(alphabetPosition("The sunset sets at twelve o' clock."), "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11");
/*
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For Example:
uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
uniqueInOrder([1,2,2,3,3]) == [1,2,3]
*/
let uniqueInOrder=function(iterable){
if(iterable.length === 0){ return []; }
let array = [iterable[0]];
if(typeof iterable === 'string'){
iterable = iterable.split('');
}
iterable.reduce(function(prev, current){
if(prev !== current){ array.push(current); }
return current;
});
return array;
}
//Sample Tests:
Test.assertSimilar(uniqueInOrder('AAAABBBCCDAABBB'), ['A','B','C','D','A','B'])
function likes(names) {
let temp = [
'no one likes this',
'{name} likes this',
'{name} and {name} like this',
'{name}, {name} and {name} like this',
'{name}, {name} and {n} others like this'
], countPeople = Math.min(names.length, 4);
return temp[countPeople].replace(/{name}|{n}/g, function (value) {
return value === '{name}' ? names.shift() : names.length;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment