exercise javascript

-2

Good people, I'm starting in javascript and I came across this exercise. Is it possible to give some help in resolving?

We have a very poor user. We want him to ask at the end of each question. Make a function 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,

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

Help: Try splitting the function into several small functions! (For example, checking if it's a question can be a function only)

    
asked by anonymous 12.05.2018 / 14:35

1 answer

1

You can simply do a replace of ? with , please? , so this will put you in the sentences 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???'
    
12.05.2018 / 14:47