Skip to content

Instantly share code, notes, and snippets.

@damiancipolat
Created May 4, 2019 17:57
Show Gist options
  • Save damiancipolat/e462439fab63a81117f0e014861a8143 to your computer and use it in GitHub Desktop.
Save damiancipolat/e462439fab63a81117f0e014861a8143 to your computer and use it in GitHub Desktop.
Zoo excercise Javascript
class Animal{
constructor(sound){
this.sound = sound;
}
speak(text){
//Split the input text by space.
let chunks = text.split(' ');
//Append the sound to each word.
let newWords = chunks.map(word => `${word} ${this.sound}`);
return newWords.join(' ');
}
}
//Testing.
const lion = new Animal('roar');
const tiger = new Animal('grr');
console.log(lion.speak("I'm a lion"));
console.log(tiger.speak("Lion sucks"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment