Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Dynamic variable in an other variable

brabra551

Coder
Code:
var var1 = 12;
var var2 = [var1,"fruit"];

var var1 = 4;

console.log(var1,var2);

output : 4 [12,"fruit"]

how do I turn it to : 4 [4,"fruit"]
 
Hey @brabra551

I'm guessing you don't want to just change it in the code by changing var var1 = 12; to var var1 = 4, so could you please explain a bit more what you're trying to do
 
Here is a great idea of what the real code looks like, sorry if it's confusing ... I'm a noobie

Code:
let dict = { pos: [1,1],[2,2]  ,pos2: [3,3],[4,4]};

let pawn = class {
  constructor {
    this.int = 0; // index for pos1
    this.anotherint = 0; // index for pos2
    this.position = [0,0]; // screen position (in pixel in real code)
    this.type_int = ["pos1",this.int];

    go_forward() {
      this.position = dict[0][type_int[0]]type_int[1][0]],dict[0][type_int[0]][type_int[1][1]];
      drawPawn(this.position);
  }
  }
}

obj.type_int = ["pos2",obj.anotherint];
obj.go_forward(); // drawPawn will draw in [3,3]
..
obj.anotherint++; // new value = 1

obj.go_forward(); //drawPawn still draws in [3,3]
..
console.log(this.position); // return [3,3]

//expected output : [4,4] obj.anotherint doesn't update in obj.type_int, it stays 0
 
Hmmm... even for a non-noobie this code is hard to swallow ! Especially this line

this.position = dict[0][type_int[0]]type_int[1][0]],dict[0][type_int[0]][type_int[1][1]];

is a masterpiece of obfuscation which I won't even begin to try and understand - but I have to admit I avoid two-dimensional arrays like the plague 🙄

You say this is what the "real" code looks like. Have you tried debugging it ? There is already a syntax error on the first line. And on line 4, a class cannot have a field named constructor. The horrible line also has errors, I can see right away the brackets are unbalanced. Did you write this code yourself ? Then try something less complicated.

Going back to the code from your first post, the value of var2 was already evaluated on line 2, using the then current value of var1. How would you expect it to change when you then re-assign var1 ? Note that you actually re-declare it, you have var var1 twice. Not sure if that is part of the issue, but don't do this kind of thing. You just can't be sure you are looking at the same memory address.

I experimented some with the eval() function (about which many people say "don't use it") but I could not get it to work like you want. I guess what you need is some kind of lazy evaluation, but when you read up on that (e.g. here https://www.codementor.io/@agustinchiappeberrini/lazy-evaluation-and-javascript-a5m7g8gs3) it does get pretty abstract and complicated. Not recommended for a beginning programmer. So maybe you'll need to re-think your design.

So.... I haven't got a solution for you but I hope my post helps some anyway.
 
First of all thank you for your elaborated answer.

I said that it was what the code looks like, it’s not the real code which is even more complex ^^ I guess that I make things complicated when it’s not necessary, maybe I’ll call a pro programmer when I’m done to check for optimizations.

My real code works well, no errors. I was just trying to give you a vision on the code’s structure so you could figure out why I would need such a thing. Seems like it’s not clear enough, sorry for that.

I also read on eval() which seems to comport security issues and turned out to not be working.

I found an other way of doing what I wanted to do anyway.

I can send you the code if you want to have a look.(it’s a ludo game)

Have a great day !
 
Yes, sometimes when you get stuck on something it is better to just try another approach. If it all works for you now, that is fine !
When you say 'no errors', do you mean no errors in the console log ? That would be great. I am a big fan of the console log, it sometimes reveals things you did not even know were wrong. By all means have a look at it if you haven't done so already.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom