0

I am trying to do this evolution thing with different characters and it is having an issue with the "if (perTable. + ("per" + per + 1) + .allele == "black")" part of the code. It throws unexpected identifier error whereas I thought everything was in the correct syntax. I'm new to code and would appreciate any easy fix for this.

I tried spreading out the plus signs by adding some spaces in between perTable, ("per" + per + 1) bit, and also before the .allele but that didn't seem to help either.

Code:

var per = 0;
var perTable = {
 per1: {
    allele: "green",
    number: "1",
    living:"1",
  },
  per2: {
    allele: "black",
    number: "2",
    living:"1",
  }
};
// console.log(perTable.per2.number)

 for (var i = 0; i < Object.keys(perTable).length; i++){
  //  console.log("per"+(per + 1))
  if (perTable. + ("per" + per + 1) + .allele == "black"){
    console.log("okay");
  } else {
    console.log("yikes");
  }

   per++;
 };

I want it to log "okay" once and "yikes" once in console but instead I get unexpected identifier at line 19 and letter 16. If you could help that would be great!

Timothy Macharia
  • 2,641
  • 1
  • 20
  • 27
milkkman
  • 7
  • 4

4 Answers4

1

This is not a correct javascript syntax. But there's a way to get sub-elements dynamically.

Instead of accessing it with dots ., you will want to access it with [], like this :

if (perTable["per" + (per + 1)].allele == "black"){

This notation will allow you to access the sub-element dynamically.

Note that I added parenthesis around "per +1", because since you are adding string and integers, Javascript will concat the stirng representation of per and 1, meaning you would have per01 and per11 instead of per1 and per2

Dimitri Mockelyn
  • 1,535
  • 2
  • 11
  • 17
1

You can't access dynamic properties using dot notation, use bracket notation.

Now your datastructure is flawed by itself. Either you want to access it by index, then use an array, or you need key-value pairs, then don't try to compose keys in a loop. Instead use either Object.keys, Object.values or Object.entries to work with them:

 for(const obj of Object.values(perTable)) {
   if(obj.allele) { /*...*/ } else { /*...*/ }
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

I believe Line 18 is causing the error:

perTable. + ("per" + per + 1) + .allele

There are two ways to access properties in JavaScript object: Dot notation and bracket notation

var obj = {a: 1, b: 2}
// Dot notation
console.log(obj.a) // 1
// Bracket notation
console.log(obj['a']) // 1

In this case, bracket notation would work better

perTable["per" + (per + 1)].allele

Here's the full snippet!

var per = 0;
var perTable = {
  per1: {
    allele: "green",
    number: "1",
    living:"1",
  },
  per2: {
    allele: "black",
    number: "2",
    living:"1",
  }
};
// console.log(perTable.per2.number)

for (var i = 0; i < Object.keys(perTable).length; i++){
  //  console.log("per"+(per + 1))
  if (perTable["per" + (per + 1)].allele == "black"){
    console.log("okay");
  } else {
    console.log("yikes");
  }

  per++;
};
Atsushi
  • 341
  • 3
  • 11
-2

Most likely the browser is complaining about this: perTable. + ("per"... because the . here makes no sense. It looks like you are attempting to build an object key from the value of a variable. When you do this, you can no longer use dot notation. Instead you need to use bracket notation:

perTable["per" + per + 1]

Note that in situations like this it is often helpful to break a long expression into smaller assignments statements. For example (using your original attempt):

key = "per" + per + 1;
allele = perTable. + key + .allele
if (allele == "black") {
...

This helps show where the problem is because the error will point at the line allele =. Also, it makes the code easier to read when you come back to look at it later.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268