0

I'm working on a project and would like to know if there's an easy way to define and use multiple variables without having to write a bunch of code.

So what I've been doing is just defining them all like this:

var Kahoot = require("kahoot.js-updated");
var client1 = new Kahoot;
var client2 = new Kahoot;
var client3 = new Kahoot;
var name = "example";
var id = "12345";


And then using them like this:

client1.join(id , name+"1").then(() => {client1.on("question", question => {});client1.on("questionStart", question => {question.answer(randomAnswer());})});
client2.join(id , name+"2").then(() => {client2.on("question", question => {});client2.on("questionStart", question => {question.answer(randomAnswer());})});
client3.join(id , name+"3").then(() => {client3.on("question", question => {});client3.on("questionStart", question => {question.answer(randomAnswer());})});

This works well, but I have to manually copy and paste, then replace the variable number. What is an easier way to do this without copypasting over and over?

mjm0813
  • 74
  • 6
  • 5
    [Arrays](https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array). – Wiktor Zychla Sep 16 '19 at 16:17
  • ([English version](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) of Wiktor's link) Arrays allow you to group all of your "same items" together, then you can loop through all items in the array and run the code for each, as opposed to hard-coding it. – Tyler Roper Sep 16 '19 at 16:19

1 Answers1

3

Just create an array.

var Kahoot = require('kahoot.js-updated');
var name = 'example';
var id = '12345';

var clients = new Array(3);
for (var i = 0; i < 3; i++) {
  var client = new Kahoot();
  client.join(id, name + (i + 1)).then(() => {
    client.on('question', question => {});
    client.on('questionStart', question => {
      question.answer(randomAnswer());
    });
  });
  clients[i] = client;
}

Then the array clients contains your three clients.

Ricola
  • 2,621
  • 12
  • 22
  • Hmm, the coding site im using throws on line 2: `Parsing error: Unexpected token ":"` Is this an error with the code or is the website incorrect? – mjm0813 Sep 16 '19 at 16:30
  • @mjm0813 I think the foreach might use `in` rather than `:`. Google the syntax. – Ian Kirkpatrick Sep 16 '19 at 16:31
  • @mjm0813 Yes sorry I forgot that `for ... : ...` don't exist in js. I replaced it by `Array.prototype.forEach`. [There are a lot of ways to iterate on an array](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) but `for-in` is not the most recommended and is even deprecated (@ago), use `for-of` instead. – Ricola Sep 16 '19 at 16:42
  • When I run this code it is acting like its using the same variable (`example`) 5 times, not acting like its 5 different variables – mjm0813 Sep 16 '19 at 17:01
  • 1
    There's a syntax error in the answer (`.then()({ ... })`), however the code functionally should be fine. @mjm0813, you'll have to provide your actual code, as evidently it seems something important may have been left out. – Tyler Roper Sep 16 '19 at 17:43
  • heres a [link to my project](https://glitch.com/~beryl-beanie). Its kinda a mess, the code related to this is in `randomAnswers.js` – mjm0813 Sep 16 '19 at 17:54
  • 1
    @mjm0813 Please edit your question with the appropriate code. Though, I'd suggest finding a happy medium between a 5-line snippet that doesn't reproduce the issue and providing an entire repo. Your question is required to have a [**minimal**, **reproducible** example](https://stackoverflow.com/help/minimal-reproducible-example) - unfortunately the two versions you've provided satisfy each case strictly individually. – Tyler Roper Sep 16 '19 at 17:59
  • @mjm0813 I edited my answer according to your edited question. – Ricola Sep 26 '19 at 09:46