...And the good! - Oops, try again. It looks like you printed out the wrong number of items - Solved!



for (i=1;i<21;i++){
    if(i%3===0 && i%5===0){
        else if (i%3===0){
        else if(i%5===0){

Where am I mistaking?


    else {

Also, neither for nor if-else if-else statements end with semi-colon.


Thank you! Didn't know that...




It doesn't look like much, but the above is an expression. Any object 'standing alone in the field' as it were, is an expression. It denotes a value or a reference object..

A value is a singular object, such as a number, a string, a boolean, or null. That's if we are strictly speaking. In the looser sense, expressions and values are interchangeable, and quite synonymous. But in the strict sense, an object reference is not a value, per se. It references an object that is a data structure or variable.

Eg. 1

var a = 5;
var b = a;
a += a;
console.log(a);    // 10
console.log(b);    // 5

Eg. 2

var a = [5];
var b = a;
a[0] += 5;
console.log(a);    // [10]
console.log(b);    // [10]

Study these examples closely. In eg. 1, a and b are values. In eg. 2, a and b are reference objects. I'll leave you to study up more on this, and get on to the real topic at hand. When to use a semi-colon, or more properly, when not to.

Never after a {} unless it is an expression.

var a = {};
var f = function () {};

The above are assignment statements. The right hand side of each statement is an expression. More exactly, they are anonymous expressions. in that they only exist in the present context and stay in memory as long as the reference to them exists. a above has same scope on both sides of the assignment. f is in parent scope, while its function reference has its own local scope.

We can change both a and f so there is no guarantee of permanance for the 'values' they reference. The only thing we cannot change is the name of the reference. Those objects are ready only.

The following are not expressions.

function foo() {}
for () {}
if () else if () {} else {}
while () {}
switch () {}

Topping in the list is the declared function. The rest are control flow declarations.

This line is only partially declarative, and does require a semi-colon:

do {} while ();

If the code you are writing does not fall into the group mentioned above, it's pretty safe to assume it gets a semi-colon.

var p = prompt('...');