FIZZBUZZ v2.0
var FizzBuzzPlus = {
isFizzBuzzie: function(value)
{
if (value%5===0&&value%3===0)
{
return false;
}
else if (value%5===0||value%3===0)
{
return true;
}
else
{
return false;
}
},
getFizzBuzzSum: function(value){
var total=0;
for (i=0;i<value;i++)
{
if(this.isFizzBuzzie(i))
{
total +=i;
}
}
return total;
},
getFizzBuzzCount: function(value){
var count=0;
for (i=0;i<value;i++)
{
if(this.isFizzBuzzie(i))
{
count++;
}
}
return count;
},
getFizzBuzzAverage: function(value){
var average = this.getFizzBuzzSum(value)/this.getFizzBuzzCount(value);
return average;
}
};
console.log(FizzBuzzPlus.getFizzBuzzAverage(100));
_____________________________________________________________________________________
Instanceof
Проверка есть ли один объект образцом другого. Тоесть используется ли команда .prototype
console.log(myElectricCar instanceof Car);
_____________________________________________________________________________________
We call String a JavaScript built-in object.
var str1 = new String("Hello!");
- BUILT-IN MATH METHODS
- Math.pow(num1, num2) takes num1 and takes it to the num2-th power. It returns the result of the exponentiation.
- Math.max(num1, num2) takes two (or more!) numbers and returns the one with the largest value.
_____________________________________________________________________________________
REMOVING ITEMS FROM ARRAY(.splice)
The splice method takes 2 arguments: the index position to start at, and the number of items to remove.
So if you want to remove the first item from an array, you would type:
myArray.splice(0,1)
_____________________________________________________________________________________
COPYING ARRAY AND ITEMS(.slice)
var b = a.slice(); или часть массива var b = a.slice(1,3);
_____________________________________________________________________________________
DEEP LOOPING IN ARRAYS
//Your three dimensional array from the last exercise probably
//looked something like this:
var hands = [];
hands[0] = [ [3,"H"], ["A","S"], [1,"D"], ["J","H"], ["Q","D"] ];
hands[1] = [ [9,"C"], [6,"C"], ["K","H"], [3,"C"], ["K","H"] ];
//Loop over every dimension in the array, logging out the suit and rank
//of each card in both hands
//1. loop over each hand
for (i=0;i<hands.length;i++) {
//2. loop over each card array in each hand
for (j=0;j<hands[i].length;j++) {
//3. loop over each rank/suit array for each card in each hand
for (k=0;k<hands[i][j].length;k++) {
//4. log the value of the rank/suit array item
console.log(hands[i][j][k]);
}
}
}
_____________________________________________________________________________________