0

My Current Project

I'm trying to make a terminal with jsQuery that has a log retrieval function.

My Problem

I don't know how to

  1. Make the function use an argument instead of writing each command, one by one.

Here's my code:

RETRIEVE 1: function() {
    this.echo('Log one.');
  },
RETRIEVE 2: function() {
   this.echo('Log two.);
  }
AgentLoneStar007
  • 117
  • 3
  • 12
  • You need to explain more clearly what you want to do, this make no sense and you have wrong JavaScript syntax, you can't have spaces in property names. – jcubic Aug 08 '20 at 05:35
  • Yeah, I figured that out. Basically want I want is I create a variable somewhere, like const log1 = "BEKFAST!!", and then when you type RETRIEVE 1 it logs BEKFAST!!, and so on. – AgentLoneStar007 Aug 13 '20 at 12:14

1 Answers1

0

As I understand what you need is object, key values pair:

I used object instead of array, because they don't need to be in order and some number may be missing.

var log = {
  1: 'Log one.',
  2: 'Log two.'
};

$('body').terminal({
    RETRIEVE: function(logNumber) {
       this.echo(log[logNumber]);
    }
});
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • I knew how to handle the array, but I couldn't figure out how to connect it to the this.echo prototype. – AgentLoneStar007 Aug 18 '20 at 12:11
  • @AgentLoneStar007 it may be tempted to use `'RETRIEVE 1': function() {}` - note the quotes - but this will not work because jQuery Terminal parse the commands and command arguments land as function arguments. For something like this you will need to use object and `function(command) { this.echo(object[command]) })` but it will probably not work as you want because you will have literal command, and if you type any extra space it will not work. Function should be used if you parse the command yourself. – jcubic Aug 19 '20 at 17:31