Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLuke Ubelhor
2,030 PointsWorking on calculations for a webform
I can't get my calculations to work on a form. I am not getting any errors running the script. Can someone give me some direction?
for (var i = 0; i < SubTotal.value.length; i++) { if (JordanPoints[i].value > 0 || BirdPoints[i].value > 0 || JohnsonPoints[i].value > 0 || OnealPoints[i].value > 0 || CurryPoints[i].value > 0) { SubTotal[i].value = JordanPoints[i].value + BirdPoints[i].value + JohnsonPoints[i].value + OnealPoints[i].value + CurryPoints[i].value ; } else { SubTotal[i].value = 0; } }
3 Answers
LaToya Legemah
12,600 PointsGood day, What kind of calculation are you trying to do with the code? I don't see "i" being incremented. Are there filling the arrays somewhere else? The way this reads to me is:
For i = 0 If JordanPoints[0] > 0 || BirdPoints[0].value > 0 || JohnsonPoints[0].value > 0 || OnealPoints[0].value > 0 || CurryPoints[0].value > 0 ( 0>0 OR 0>0 OR 0>0 OR 0>0 OR 0>0 )
subtotal (0) = JordanPoints[0].value + BirdPoints[0].value + JohnsonPoints[0].value + OnealPoints[0].value + CurryPoints[0].value ; ( 0 = 0+0+0+0+0)
Else SubTotal[i].value = 0; ( 0 = 0)
If there are no values within the Jordan, birdpoints, etc., this will fail since zero can't be greater than zero. The next subtotal doesn't seem to have anything to do with the For above it. I think the syntax is wrong, I don't think this is the right way to mix an if, else and for loop.
http://www.w3schools.com/js/js_if_else.asp http://www.w3schools.com/js/js_loop_for.asp
miikis
44,957 PointsHey Luke,
As LaToya Legemah and Jennifer Nordell point out, you got your for-loop syntax all messed up; check out this page on W3Schools to learn about how it all works.
As to your code, it looks like you're trying to loop through something like this:
var pointsPerGame = {
jordan: 30.12,
bird: 24.29,
johnson: 20.10,
oneal: 23.69,
curry: 22.22
};
If so, the easiest way would be to use a for-in loop:
for (var player in pointsPerGame) {
// This returns something like:
// "jordan scored an average of 30.12 points per game!"
console.log(player + ' scored an average of ' + pointsPerGame[player] + ' per game!');
}
Jennifer Nordell
Treehouse TeacherI feel like your for loop is built incorrectly. You start at i = 0 and start running the loop. But then you never increment i. And in the middle you have an if statement which should actually be a statement to find out how many times your loop should be run. For example:
for( i = 0; i < 10; i++);
It's looking for an evaluation to a number. Your middle code with the if part evaluates to a boolean.
Sorry for some reason I can't get the markdown to work properly.
LaToya Legemah
12,600 PointsJennifer Nordell I been having issues with the markdown too. I found out I was using the wrong tag. I was using the quotes by the enter key instead of the quote by the #1 key. Hope this helps :)
Luke Ubelhor
2,030 PointsLuke Ubelhor
2,030 PointsI am trying to create a simple addition form. I will see if I can get this to work.
thank you