Reputation: 3161
Can anyone explain why the increment i++ in the while loop of this coffescript is placed outside of the while loop when converted to javascript ?
if eventtype is 'test'
i = 0
while i < platforms.length
$.ajax
url: 'myurl/?id=567&platform='+platforms[i]
.done (response) ->
if platforms[i] is 'tv'
$scope.lolVdatatv = JSON.parse(response)
alert response
if platforms[i] is 'phone'
$scope.lolVdataphone = JSON.parse(response)
alert response
if platforms[i] is 'internet'
$scope.lolVdatainternet = JSON.parse(response)
alert response
i++
Here is the converted JavaScript:
var i;
if (eventtype === 'test') {
i = 0;
while (i < platforms.length) {
$.ajax({
url: 'myurl/?id=567&platform='+platforms[i]
}).done(function(response) {
if (platforms[i] === 'tv') {
$scope.lolVdatatv = JSON.parse(response);
alert(response);
}
if (platforms[i] === 'phone') {
$scope.lolVdataphone = JSON.parse(response);
alert(response);
}
if (platforms[i] === 'internet') {
$scope.lolVdatainternet = JSON.parse(response);
return alert(response);
}
});
}
i++;
}
This is causing the while loop not to exit.
Upvotes: 0
Views: 791
Reputation: 2313
Coffeescript is, like python, indentation sensitive. You need to have the i++
statement indented further to be inside the while
loop:
i = 0
while i < platforms.length
//do things
i++
//still inside.
//this is outside of the loop
Upvotes: 1