1

This bit of code doesn't seem to work.

for(var b = 0; b < wallPoints.length-1; b++)  
{  
    wallPoints[b].xPos = wallPoints[b]-1;  
}  

function Point(x,y)  
{  
    this.xPos = x;  
    this.yPos = y;  
}

wallPoints is an array of Points

The code doesn't return any errors, it just makes all my code stop executing. This is my first time using JavaScript, so this is probably a very stupid mistake.

5
  • 1
    check this line wallPoints[b].xPos = wallPoints[b]-1; Commented Nov 3, 2010 at 17:14
  • Show the code for wallPoints please. Commented Nov 3, 2010 at 17:14
  • 'wallPoints = new Array(); wallPoints[0] = new Point(0,10); wallPoints[1] = new Point(600,10); wallPoints[2] = new Point(650,10);' Commented Nov 3, 2010 at 17:20
  • A current best-practice way to initialize that array: var wallPoints = [new Point(0,10), new Point(600,10), new Point(650,10)]; Commented Nov 3, 2010 at 17:29
  • Did you ever get this resolved successfully? Do you still need help with this? Commented Dec 14, 2010 at 4:07

2 Answers 2

3

What are you trying to do -- shift each point by one in the x axis? You need to reference the property on the right hand side of the assignment as well.

for(var b = 0; b < wallPoints.length; b++)   
{   
    wallPoints[b].xPos = wallPoints[b].xPos - 1;   
}

or do you want to propagate the x axis from one point to another

for(var b = 1; b < wallPoints.length; b++)   
{   
    wallPoints[b].xPos = wallPoints[b-1].xPos;   
}

In the latter case, you'll need to figure out what to do with the first point. Note the change in the termination condition (and start condition in the second case).

EDIT: Here's my test code:

<html>
<head>
<title>Point</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    var wallPoints = new Array();
    wallPoints[0] = new Point(0,10);
    wallPoints[1] = new Point(600,10);
    wallPoints[2] = new Point(650,10);

    var content = $('#content');

    content.append('<h2>Before</h2>');

    for(var b = 0; b < wallPoints.length; b++)
    {
        content.append('<p> x = ' + wallPoints[b].xPos + ', y = ' + wallPoints[b].yPos + '</p>' );
        wallPoints[b].xPos = wallPoints[b].xPos-1;
    }
    content.append('<h2>After</h2>');

    for(var b = 0; b < wallPoints.length; b++)
    {
        content.append('<p> x = ' + wallPoints[b].xPos + ', y = ' + wallPoints[b].yPos + '</p>' );
    }

    function Point(x,y)
    {
        this.xPos = x;
        this.yPos = y;
    }
});
</script>
</head>
<body>
<div id="content">
</div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, that is what I originally had in my code but I messed up when typing it in here. The code still does not work.
@Daniel: It's best to post real code that you've run as written and experienced the problem. Code you've retyped might not exhibit the same problem. It also might have other problems, as this answer shows.
@daniel -- I've tested the code (with a few additions so that I could see that it's working) above and it seems to be ok. Does firefox/firebug show you anything in the console? If you don't have firefox/firebug, you really ought to get them. It will be a tremendous help in debugging javascript problems.
@tvanfosson ~ I think you've invested more time in his problem than he has.
@drachenstern -- cut and paste, cut and paste. It helps to have a template test document already set up.
0

You're assigning an X variable equal to a point. Not an option:

for(var b = 0; b < wallPoints.length-1; b++)  
{  
    wallPoints[b].xPos = wallPoints[b]-1;  
}  

instead try this:

for(var b = 0; b < wallPoints.length-1; b++)  
{  
    wallPoints[b].xPos = wallPoints[b].xPos - 1;
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.