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 trial
behar
10,800 PointsOptimization question
Hey guys! Im trying to create line drawing algorithm game in which two random points will be chosen at a grid that 10 x 10. Im storing the randomly chosen points in the variables first_point and second_point. However i need to convert the first_point and second_point variables into x1, y1 and x2, y2 cordinates (currently they are just id's ranging from 0 - 99) I have the following code:
if (first_point in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) {
x1 = 10
console.log(x1)
} else if (first_point in [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) {
x1 = 9
console.log(x1)
} else if (first_point in [20, 21, 22, 23, 24, 25 ,26, 27, 28, 29]) {
x1 = 8
console.log(x1)
} else if (first_point in [30, 31, 32, 33, 34, 35, 36, 37 ,38, 39]) {
x1 = 7
console.log(x1)
} else if (first_point in [40, 41, 42, 43, 44, 45, 46, 47 ,48, 49]) {
x1 = 6
console.log(x1)
} else if (first_point in [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]) {
x1 = 5
console.log(x1)
} else if (first_point in [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]) {
x1 = 4
console.log(x1)
} else if (first_point in [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]) {
x1 = 3
console.log(x1)
} else if (first_point in [80, 81, 82, 83, 84, 85, 86, 87, 88, 89]) {
x1 = 2
console.log(x1)
} else if (first_point in [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]) {
x1 = 1
console.log(x1)
}
So basicly if the id for the first_point is 92, the x1 cordinate is 1. However this code dosent seem to work. Only if its at the top of the table. That is from 0 - 9, the code works perfectly but the else if statements seem not to work, it dosent print to the console, and there are no error messages. If someone could explain to me why, i would appreciate it alot! Also if some genius could figure out a smarter way to mimic the code above in a simpler way id be gratefull! Thank you so much!
1 Answer
Steven Parker
243,199 PointsThe membership operator ("in") might not do what you are expecting. It determines if a specified property (on the left) is in the specified object (on the right). Since your objects are all simple arrays with 10 elements, each one will contain properties of "0" to "9". The actual values contained in the array are not important, only the existing properties (index numbers).
However, it looks like what you intended that entire "if...else if" chain to do can be accomplished easily with a math formula:
x1 = 10 - Math.floor(first_point / 10);
behar
10,800 Pointsbehar
10,800 PointsHey steven! Thank you so much for the response. I see why my code dosent work. And i knew that there was a better way to do it. And ive already taken into use the formula you provided. However it actually dosent calculate x1 and x2, it calculates y1 and y2. It makes sense tho because say im on the id 92: 92 / 10 = 9.2, Math.floor(9.2) = 9. 10 - 9 = 1. Which is indeed what the y value should be. But the x value should actually be 3. However im too dumb to figure out a formula like the above for the x value. If you could provide me with that aswell i would be very gratefull!
UPDATE: I figured out this following code, for x1 and x2:
Thank you for the y1 and y2 still tho steven!
Steven Parker
243,199 PointsSteven Parker
243,199 PointsThe formula I gave before was based on what the original code appeared to be intended to do. But good deal on finding formulas that meet your actual intentions!