Number 3... And the good - so close!


#1

Hi, can anyone help... the following code logs the right answer to the console except it logs the word fizz buzz before it goes on to 1, 2, fizz etc. It seems to be using the index value of the array, rather than the actual value, as the counting goes from 0 (fizz buzz) to 19. Any help much appreciated:

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

for (var i in numbers) {
    if ((i % 3 === 0) && (i % 5 === 0)) {
        console.log("FizzBuzz");
    }
    else if (i % 3 === 0) {
        console.log("Fizz");
    }
    else if (i % 5 === 0) {
        console.log("Buzz");
    }
    else {
        console.log(i);
    }
}

#2

Use the generic for loop instead

for (var i  = 1;i<=20;i++) {
    if ((i % 3 === 0) && (i % 5 === 0)) {
        console.log("FizzBuzz");
    }
    else if (i % 3 === 0) {
        console.log("Fizz");
    }
    else if (i % 5 === 0) {
        console.log("Buzz");
    }
    else {
        console.log(i);
    }
}

#3

Try this code!..

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

for (var i in numbers) { <----- // use this-----> for (var i =1; i< 20; i ++){
if ((i % 3 === 0) && (i % 5 === 0)) { <----- // debug this!
console.log("FizzBuzz");
}
else if (i % 3 === 0) {
console.log("Fizz");
}
else if (i % 5 === 0) {
console.log("Buzz");
}
else {
console.log(i);
}
}


#4

@spacecamel100,

The Datatypes of Javascript are

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

Above you have an overview of all data-types.

===== Datatype Array =========
What you must understand,
is that if you ( as an example )
create a array
for the Javascript-Interpreter
you have created an object of the prototype array.
and if you do a google search
javascript array site:developer.mozilla.org
and choose
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
on the left hand side you will see
what information you can gather about the array

PropertiesArray.prototype

Array.prototype.length

Methods ( pre-defined functions for Array operations )

Array.isArray()
Array.prototype.concat()
Array.prototype.every()
Array.prototype.filter()
Array.prototype.forEach()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toString()
Array.prototype.unshift()

========= looping over an array ===

The most common way to loop over all elements of an Array
is using the generic FOR-loop
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

for ([initialization]; [condition]; [final-expression]) {
      statement
}

You would
start at index zero,
continue as long as the index is =less than= the array.length,
and at each iteration increment / decrement the index-pointer

var myArray =["a",2,{anObjectKey: "anObjectValue"}];
for ( var index = 0, len = myArray.length; index < len ; index= index + 1 ) {
     // get access to the =element-Value= using the [bracket-notation]
     console.log( index + "==> "+myArray[index] );
}

== Do NOT use FOR-IN loop on an Array ==

You could look at an Array as it being an Object like

 myArray = {
      0: "a",
      1: 2,
      2: { anObjectKey: "anObjectValue"}
 }

and with the FOR-IN loop

for (var key in myArray) {
     //getting access to the =associate property-Value= 
      //using the =key= in a [bracket-notation]
      console.log( key + "==>"+myArray[key] );
}

but do a google search
== discussions / opinions ==
javascript why is it a bad idea to use for-in on an Array site:stackoverflow.com
for instance


[quote]
An array is an object,
and array elements are simply properties with the numeric index converted to string.
For example, arr[123] refers to a property "123" in the array object arr.

The for ... in construct
works on all objects, not just arrays,
and that is cause for confusion.

When somebody for ... in an array,
most often the programmer intends to iterate just all the items, even most likely in order.

The semantics is so similar to array iteration in other programming languages that it is very easy to get confused.
In JavaScript, this construct does not iterate array elements in order.
It iterates all

  • the array's properties
  • (including inherited prototype functions, any properties added to it, any other non-element properties added to it etc.),
  • and not in order at all.
  • It will even find the property "length".

This is most likely not what the programmer means, if he/she comes from another programming language.

That's why you shouldn't do it.
You shouldn't use a language construct
that looks like it does obvious things, but actually does things that are completely different.
It creates bugs that are very obscure and very difficult to find.
[end quote Stephen Chung]