Created
May 4, 2019 17:57
-
-
Save damiancipolat/e462439fab63a81117f0e014861a8143 to your computer and use it in GitHub Desktop.
Zoo excercise Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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