javascript exercise

Asked

Viewed 76 times

-2

Good people, I’m starting in javascript and I came across this exercise. it is possible to help in the resolution?

We have a very poorly created user. We want him to please ask at the end of each question. Do a feature that adds ", Please?" at the end of each question.

That is, the use of this would be:

const phrase = makePoliteQuestion('Can you open the door?');
//phrase seria 'Can you open the door, please?'

But only if it’s a question! That is to say,

const phrase = makePoliteQuestion('Open the door.');
//phrase seria 'Open the door.'

Help: Try to split the function into several small functions! (For example, checking if it is a question can be a single function)

1 answer

1

You can simply replace the ? for , please?, so this will put the please in the phrases that have questions

function makePoliteQuestion(str) {
  return str.replace('?', ', please?');
}

console.log(makePoliteQuestion('Can you open the door?'));
//phrase seria 'Can you open the door, please?'

console.log(makePoliteQuestion('Open the door.')); 
//phrase seria 'Open the door.'

console.log(makePoliteQuestion('Can you open the door???'));
//phrase seria 'Can you open the door, please???'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.