Javascript Bytes (6): Asking Politely

Create a function that checks to see if the argument string contains a question mark. If it does, make sure that the question ends with a “please?”. In all other scenarios, simply return the original argument.

 

function askPolitely(question) {

//find the last char of the string
var lastChar = question.slice(-1);

if (lastChar === “?”) {
if (question.indexOf(“please”) > -1) {
return question;

} else if (question.indexOf(“please”) === -1) {
return question.substring(0, question.length – 1) + ” please?”;
}

} else {
return question;
}

}

console.log(askPolitely(“May I ask a question?”));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s