0

I am a bit stuck here because I can't figure out why this does not work. When i try to run the code then it says "ReferenceError: item is not defined" - but I thought that by writing "let item" I am defining it. Any idea what the problem is here ?

  • i know the code looks a bit more complicated than necessary but i need to do it like that.
class Graph {
  constructor() {
    this.nodes = new Set();
    this.edges = new Set();
  }

  add(m,n){
   let e = new Edge (m,n)
   var allEdges = new Set();
   for (let item of this.edges){ 
     allEdges.add(item.startpoint.nodeName,item.endpoint.nodeName)
   };
   if (allEdges.has(e.startpoint.nodeName,e.endpoint.nodeName)){ 
     console.log("first OCL Constraint")
   } else if(allEdges.has(e.endpoint.nodeName,e.startpoint.nodeName){    
     console.log("second OCL Constraint")}
   else { 
     this.edges.add(item.startpoint.nodeName,item.endpoint.nodeName)};
   }
};

class Edge {
  constructor(startpoint,endpoint) {
    this.startpoint = startpoint;
    this.endpoint = endpoint;
  }
};

class Node {
  constructor(nodeName) {
    this.nodeName = nodeName
  }
};

node1 = new Node (1);
node2 = new Node (2);
node3 = new Node (3);
node4 = new Node (4);
node5 = new Node (5);
myTestGraph = new Graph();

myTestGraph.add(node1,node2);
myTestGraph.add(node2,node1);
myTestGraph.add(node3,node4);

Thanks in advance :)

sam
  • 1,767
  • 12
  • 15
  • 2
    `let item` makes it exist inside the for loop, however you're then accessing `item` in the `else` clause, outside the for loop. There's other problems with your code, for instance `Set.has` only accepts a single argument (the rest is ignored) –  Oct 29 '21 at 18:32
  • I see that makes perfect sense :) i tried to fix this: } add(m,n){ let e = new Edge (m,n) var allEdges = new Set(); for (let item of this.edges){allEdges.add(item.startpoint.nodeName,item.endpoint.nodeName)}; if (allEdges.has(e.startpoint.nodeName,e.endpoint.nodeName)){console.log("first OCL Constraint")} else if(allEdges.has(e.endpoint.nodeName,e.startpoint.nodeName)){console.log("second OCL Constraint")} else {this.edges.add(e.startpoint.nodeName,e.endpoint.nodeName)}; } }; **but now it says : can't access property "nodeName", item.startpoint is undefined*** –  Oct 29 '21 at 18:40
  • Please don't post question updates as comments. Here's fixed code, properly indented: https://jsfiddle.net/ak7wbn59/ –  Oct 29 '21 at 18:43
  • sure sorry about that - thanks for the code ! the issues is i am only allowed to have the add function unfortunately :( –  Oct 29 '21 at 18:57
  • Wow! How many spaces you waste! There are so may spaces arond `=`. How you can do that? (it was sarcasm if you don't mind) – A1exandr Belan Oct 29 '21 at 18:59
  • Here's a much improved version without the edgeExists method: https://jsfiddle.net/8v39x6cy/ –  Oct 29 '21 at 19:35
  • Much better Thank you very much for your time and effort :) however what is also needed is that i keep the reference to the nodeName :/ Btw you posted previously that my code has issues especially the Set. has function. Is there a working alternative to that ? :) –  Oct 29 '21 at 20:13
  • I'm keeping all references. The edge set has the edges, and for each edge you can do `edge.startpoint.nodeName` to get the name. As for the Set.has function: when you create a new Edge instance, no Set will "have" it by definition (because it's a new Edge instance that was just created in the computer's RAM and cannot possibly be already part of a set in a JS reference sense). If you're talking about a way to check if a set of edges contains an edge that connects m and n, that is exactly what the edgeExists functions of my previous fiddle does. –  Oct 29 '21 at 20:17
  • Ah alright, thanks for that so if I create an instance of Graph - lets call it test graph and then i perform the add method, that you just posted. Afterward, i would like to get the names of the nodes by looping through the edges-set of the test graph. Would that be possible then? **awesome it looks like it works** –  Oct 29 '21 at 20:33

1 Answers1

0

The problem seems to be in the last "else", where you reference the "item" out of its scope (you declare let item inside the for block) I think the correct code for the add() method is:

add(m,n){
   let item;
   let e = new Edge(m,n);
   var allEdges = new Set();
   for (item of this.edges){allEdges.add(item.startpoint.nodeName,item.endpoint.nodeName)};
   if (allEdges.has(e.startpoint.nodeName,e.endpoint.nodeName)){console.log("first OCL Constraint")}
   else if(allEdges.has(e.endpoint.nodeName,e.startpoint.nodeName)){console.log("second OCL Constraint")}
   else {this.edges.add(item.startpoint.nodeName,item.endpoint.nodeName)};
}
  • unfortunately that does not work it returns " can't access property "startpoint", item is undefined" –  Oct 29 '21 at 18:58