Kevin A
Kevin A

Reputation: 83

How to achieve results without repeating code?

My goal is to prompt a question formatted exactly like this:

      What is your favourite programming language?
      0: JavaScript
      1: Python
      2: Rust
      3: C++

I achieved this by destructuring the options array inside the poll object inside the resisterNewAnswer() method. I used the \n but looks horrible repeating it. I tried using a for of loop but not sure how to implement it. I'm trying to achieve this inside the resisterNewAnswer() method.

const poll = {
  question: 'What is your favorite programming language?',
  options: ['0: JavaScript', '1: Python', '2: Rust', '3:c++'],
  answers: new Array(4).fill(0),
  registerNewAnswer() {
    const [j, p, r, c] = this.options;
    prompt(
      `What is your favourite programming language? \n ${j} \n ${p} \n ${r} \n ${c}`
    );
  },
};
poll.registerNewAnswer();

Upvotes: 0

Views: 98

Answers (4)

imvain2
imvain2

Reputation: 15867

One thing I would do is use this.question in the prompt instead of repeating it. Also combining the options using join will allow you to simplify while having the ability to use different number of options.

const poll = {
  question: '',
  options: [],
  answers: new Array(4).fill(0),
  registerNewAnswer() {
    return prompt(`${this.question}\n${this.options.join("\n")}`
    );
  },
};

poll.question = "What is your favorite programming language?";
poll.options = ['0: JavaScript', '1: Python', '2: Rust', '3:c++'];
console.log(poll.registerNewAnswer());

poll.question = "Question 2?";
poll.options = ['0: JavaScript', '1: Python', '2: Rust', '3:c++'];
console.log(poll.registerNewAnswer());

Upvotes: 0

The Bomb Squad
The Bomb Squad

Reputation: 4337

const poll = {
  question: 'What is your favorite programming language?',
  options: ['0: JavaScript', '1: Python', '2: Rust', '3: c++'],
  answers: new Array(4).fill(0),
  registerNewAnswer() {
    const jprc=`\n${this.options.join('\n')}`
    const x=()=>prompt(`What is your favourite programming language? ${jprc}\nOr -1 to end entries`) //x becomes a function that prompts you and returns what you input
    var i=x()
    while(i!=-1){this.answers[i%this.options.length]++;i=x()} //the % logic to prevent incorrect entry(or you can have an if statement that just won't record it if incorrect index)
  },
};
poll.registerNewAnswer();
console.log(poll.answers)

Upvotes: 0

3limin4t0r
3limin4t0r

Reputation: 21160

You could use any one of the popular JavaScript templating libraries: ejs, handlebars or mustache.

Here is an example using mustache:

const questionTemplate = `\
{{question}}
{{#options}}
{{index}}: {{option}}
{{/options}}
`;

const poll = {
  question: 'What is your favorite programming language?',
  options: ['JavaScript', 'Python', 'Rust', 'C++'],
  answers: new Array(4).fill(0),
  registerNewAnswer() {
    prompt(Mustache.render(questionTemplate, {
      question: this.question,
      options: this.options.map((option, index) => ({option, index})),
    }));
  },
};
poll.registerNewAnswer();
<script src="https://unpkg.com/mustache@4/mustache.min.js"></script>

The \ behind the initial ` is to ignore the first newline character.

Upvotes: 1

Pavel Alekseev
Pavel Alekseev

Reputation: 1222

Just use join method to achieve this:

const poll = {
  question: 'What is your favorite programming language?',
  options: ['0: JavaScript', '1: Python', '2: Rust', '3: C++'],
  answers: new Array(4).fill(0),
  registerNewAnswer() {
    const [j, p, r, c] = this.options;
    prompt(
      `What is your favourite programming language?\n${this.options.join('\n')}`
    );
  },
};
poll.registerNewAnswer();

Upvotes: 0

Related Questions