-1

how do I do

 #define repeat(iterations)                                                     \
 for (int iterator = 0; iterator < iterations; iterator++)

but in JavaScript?

or any other way to make it so that I can just type

repeat(x){
any thing I want to repeat x amount of times
}

(I know you can just do a for loop, I just like being able to type repeat to make it look nicer)

Dan
  • 858
  • 7
  • 18
xevinf
  • 13
  • 2

2 Answers2

2

There are a few ways how you could accomplish this.

Using the c preprocessor

You can use the c preprocessor on other file types, so you could create a compile setup for your javascript code.

Example:

foo.js:

#include "macros.js"

function foo() {
  repeat(10) {
    console.log(i);
  }
}

macros.js:

#pragma once

#define repeat(x) for(let i = 0; i < x; i++)

and then you can compile it with cpp:

cpp -nostdinc -CC -P -undef -I. foo.js -o foo.compiled.js

This would result in a foo.compiled.js file that looks like this:

function foo() {
  for(let i = 0; i < 10; i++) {
    console.log(i);
  }
}

You can easily set this up for an entire project with a Makefile or similar, like you would for a c++ project.

using babel

Babel is often used in javascript projects to be able to use newer javascript features without having to think about compatiblity. (it compiles the code down and inserts polyfills as needed)

With a few plugins you can achieve something similar to what you want.

For example babel-plugin-macros that allows you to write macros that will be inlined in the calling function.

There's however one big downside to this - your code must be valid javascript, so your repeat example would not work as-is.


Why this is a bad idea

While i can see the benefit for having to type less with those kinds of macros, you should also think about the maintainability of your code.

Think about it from the perspective of someone who has never seen your code and then gets presented with c++ code like this:

numero foo fonction
debut
  si i > 0 alors
    sortie(i)
    revenir(i)
  sinon
    inverser(i)
    sortie(i)
    revenir(i)
  finsi
fin

Sure this is valid c++ code - but can you tell what it does without knowing each one of those macros?

Maybe with the help of the macro definitions?

#define debut {
#define fin }
#define si if(
#define alors ){
#define sinon }else{
#define finsi }
#define numero int
#define fonction (int i)
#define sortie(v) std::cout << v << std::endl;
#define revenir(v) return v;
#define inverser(v) v = -v;

As you can see this quickly gets into a deep rabbit hole - you basically have to lookup each macro definition, memorize what they replace with and piece the actual code together in your mind to actually be able to understand what it does.

In comparison to that horrible example try to read it in plain c++:

int foo(int value) {
  if(value > 0) {
    std::cout << value << std::endl;
    return value;
  } else {
    value = -value;
    std::cout << value << std::endl;
    return value;
  }
}

while that might be a bit more to type, everybody will be able to quickly understand what that function does, since its just standard c++ that everyone should be familiar with.

A few more helpful links
Turtlefight
  • 9,420
  • 2
  • 23
  • 40
0

I think what you're looking for is a while loop:

while (condition) {
  // code block to be executed
}

If you want to repeat however many times based on a variable, you could do something like this:

var x = 0;
// While x is less than 10
while (x < 10) {
  // code block to be executed
  x++;
}
Koto
  • 430
  • 4
  • 19
  • Thanks for the response! i probably should have clarified, I know you can do that with a while loop, i was just wondering if i can implement a repeat command because that is the syntax i use when doing C++, and it would be nice to be able to do that for java script as well. – xevinf Nov 05 '21 at 05:18
  • @xevinf I'm not sure I understand. From what you're describing it sounds like a ```while``` loop is exactly what you're looking for. But you just want to be able to type ```repeat``` rather than ```while```? As far as I know, there is no ```repeat``` loop in JavaScript. – Koto Nov 05 '21 at 19:27
  • i was wondering if you can make your own command in JavaScript, like in C++ where you can make your own preprocessor macros. in C++ if you do ` #define repeat(iterations) \ for (int iterator = 0; iterator < iterations; iterator++) ` you can now type repeat(3) { whatever you want to repeat } and it repeats the code 3 times – xevinf Nov 05 '21 at 20:42
  • @xevinf Making your own command should be a last resort when coding. If the language already has the type of commands you're looking for, there should be no reason to "create" your own command. Give me an example of something you can't do in JavaScript that would warrant the "creation" of your own command. The example you've already given is already available via the ```while``` loop. – Koto Nov 05 '21 at 21:09
  • I know it is kind of stupid to make your own commands when there is a perfectly working while loop you can use. This is just personal preference at this point, and i like being able to customize code to work for me. in C++ i use many different custom commands, and was just wondering if there was any way to do it in JavaScript. – xevinf Nov 06 '21 at 04:44