Skip to content

Instantly share code, notes, and snippets.

@soner8
Created June 7, 2018 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soner8/6380bd7f4ffc7dba25e7f745992d37d2 to your computer and use it in GitHub Desktop.
Save soner8/6380bd7f4ffc7dba25e7f745992d37d2 to your computer and use it in GitHub Desktop.
CodeWars Maco
You are going to be given a word. Your job is to return the middle character of the word.
If the word's length is odd, return the middle character.
If the word's length is even, return the middle 2 characters.
String.prototype.toJadenCase = function () {
return this.split(' ').map(function (item) {
return item.replace(item.charAt(0), item.charAt(0).toUpperCase());
}).join(' ');
};
You have to implement a class Hangman that receives a word in it's constructor and has the method guess,
that will be used by the player to try to guess the word.
Your method guess will receive a letter as parameter and has this return behaviour:
if the player found the word: You found the word! ({word})
if the player got hung: You got hung! The word was {word}.
if the game still on: {game state}
if the game has ended already: The game has ended.
class Hangman {
constructor(word) {
this.word = word;
this.missed = '';
this.found = '';
this.ended = false;
this.MESSAGE = {
found: `You found the word! (${this.word})`,
hung: `You got hung! The word was ${this.word}.`,
ended: `The game has ended.`
};
}
guess(letter) {
if (this.ended) {
return this.MESSAGE.ended;
}
if (new RegExp(letter,'gi').test(this.word)) {
this.found += letter;
if (this.word.replace(new RegExp(`[${this.found}]`,'gi'),'').length === 0) {
this.ended = true;
return this.MESSAGE.found;
}
} else {
if (this.missed.indexOf(letter) === -1) {
this.missed += letter;
if (this.missed.length >= 7) {
this.ended = true;
return this.MESSAGE.hung;
}
}
}
return this._buildState();
}
_buildState() {
let word = this.word.split('').map(c => this.found.indexOf(c) === -1 ? '_' : c).join(' ');
if (this.missed.length) {
word += ` # ${this.missed}`
}
return word;
}
}
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.
function getMiddle(s)
{
if(s.length % 2 == 0){
return s.substr(s.length/2-1,2);
}
return s.substr(s.length/2, 1 );
}
Write a function that accepts an array of 10 integers (between 0 and 9),
that returns a string of those numbers in the form of a phone number.
function createPhoneNumber(numbers){
return numbers.join('').replace(/(\d{3})(\d{3})(\d{4})/,'($1) $2-$3');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment