Aidan Bevan
Aidan Bevan

Reputation: 1

for loops regarding unknown errors

I have an assignment for school that I need to complete, these are the criteria, Count every number 0 - 35 Count every number 0 - 50, start from 30 and stop at 50 Count by 5's 0 - 50 Count down from 10 to 0 Count down from 100 - 0 by 10's Count every odd number from 1-30 all doing that with for loops so here is what i have so far, and for some reason it is not working

 <html>
<body>
<script>
for (int i = 0; i < 36; i++){ 
document.write(i);

}



</script>
</body>
</html>

my question is what am I doing wrong? it comes up with an unexpected identifier but thats all it says.

Upvotes: 0

Views: 41

Answers (3)

Scott Marcus
Scott Marcus

Reputation: 65825

You can't declare a data type (int) in JavaScript. JavaScript is a loosely-typed language. There are only strings, numbers, booleans as primitive data types and the type you get is dependent on how you implicitly (or explicitly) use them.

Here, the variable i is initialized to 0, which is a valid number. When the JavaScript runtime sees you attempting to add to it, it allows that because it has implicitly known that i should be categorized as a number:

for (var i = 0; i < 36; i++){ 
  document.write(i);
}

// And, just for fun...
var a = 5;
var b = "5";
console.log("a's type is: " +  typeof a);
console.log("b's type is: " +  typeof b);

// But, you can coerce a value into a different type:
console.log("parseInt(b) results in: " + typeof parseInt(b));
console.log('a + "" results in: ' + typeof (a + ""));

Here's some good reading on the subject.

Upvotes: 3

C. Smith
C. Smith

Reputation: 411

int is no good in Javascript. Everything is defined as var. Try:

for (var i = 0; i < 36; i++){ 
    document.write(i);
}

Upvotes: 2

Janaka Dissanayake
Janaka Dissanayake

Reputation: 547

there is no type called 'int' in javascript use 'var'

for (var i = 0; i < 36; i++){ 
 document.write(i);

}

Upvotes: 2

Related Questions