Last active
October 18, 2016 08:59
-
-
Save droganaida/c907fd372e3446dd9deaee065d8008cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------ПРИМЕР 1--------------------// | |
var starCount = 0; | |
var potatoBoom = function(callback){ | |
starCount = starCount + 1; | |
callback(); | |
}; | |
setTimeout(function(){ | |
potatoBoom(function(){console.log("На счету у картофельного снайпера " | |
+ starCount + " звезд");}) | |
}, 1000 | |
); | |
//----------------ПРИМЕР 2--------------------// | |
var neighbors = ['Дядя Ваня', 'Мария Петровна', 'Серега']; | |
var gunsCount = 3; | |
function getRandomIntFromTo(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
var potatoBoom = function(callback){ | |
var neighbor = neighbors[getRandomIntFromTo(0, 2)]; | |
var timeout = getRandomIntFromTo(20, 200); | |
setTimeout(function(){ | |
callback(neighbor); | |
}, timeout); | |
}; | |
for (var i=0; i<gunsCount; i++){ | |
potatoBoom(function(neighbor){ | |
console.log("Сосед по имени " + neighbor); | |
}) | |
} | |
//----------------ПРИМЕР 3--------------------// | |
//Сортировка при помощи массива индексов | |
var neighbors = ['Дядя Ваня', 'Мария Петровна', 'Серега']; | |
var potatoOne = {id:1,weight: 50, color: 'yellow', sort: 'адретта'}; | |
var potatoTwo = {id:2, weight: 62, color: 'red', sort: 'скороспелка'}; | |
var potatoTree = {id:3, weight: 74, color: 'white', sort: 'синеглазка'}; | |
var potatoes = [potatoOne, potatoTwo, potatoTree]; | |
function getRandomIntFromTo(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
var potatoBoom = function(potatoid, callback) { | |
var neighbor = neighbors[getRandomIntFromTo(0, 2)]; | |
var timeout = getRandomIntFromTo(20, 200); | |
console.log("Картошка под номером " + potatoid + " прямо сейчас летит к соседу " + neighbor); | |
setTimeout(function(){ | |
callback(neighbor, potatoid); | |
}, timeout); | |
}; | |
var trophies = []; | |
var indexArray = []; | |
for (var i=0; i<potatoes.length; i++){ | |
var potatoIndex = potatoes.indexOf(potatoes[i]); | |
indexArray.push(potatoes[i].id); | |
potatoBoom(potatoes[i].id,function(neighbor, potatoid){ | |
var trophy = {}; | |
trophy.neighbor = neighbor; | |
var potatoIndex = indexArray.indexOf(potatoid); | |
trophy.potato = potatoes[potatoIndex]; | |
trophies[potatoIndex] = trophy; | |
console.log("Журнал трофеев: " + JSON.stringify(trophies)); | |
}) | |
} | |
//Выборка без сортировки | |
var neighbors = ['Дядя Ваня', 'Мария Петровна', 'Серега']; | |
var potatoOne = {id:1, weight: 50, color: 'yellow', sort: 'адретта'}; | |
var potatoTwo = {id:2, weight: 62, color: 'red', sort: 'скороспелка'}; | |
var potatoTree = {id:3, weight: 74, color: 'white', sort: 'синеглазка'}; | |
var potatoes = [potatoOne, potatoTwo, potatoTree]; | |
function getRandomIntFromTo(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
var potatoBoom = function(potato, callback){ | |
var neighbor = neighbors[getRandomIntFromTo(0, 2)]; | |
var timeout = getRandomIntFromTo(20, 200); | |
potato.neighbor = neighbor; | |
setTimeout(function(){ | |
callback(potato); | |
}, timeout); | |
}; | |
var trophies = []; | |
for (var i=0; i<potatoes.length; i++){ | |
var potatoIndex = potatoes.indexOf(potatoes[i]); | |
potatoBoom(potatoes[i],function(potato){ | |
trophies.push(potato); | |
console.log("Журнал трофеев: " + JSON.stringify(trophies)); | |
}) | |
} | |
//----------------ПРИМЕР 4--------------------// | |
var fillTrophies = function(callback){ | |
var neighbors = ['Дядя Ваня', 'Мария Петровна', 'Серега']; | |
var potatoOne = {weight: 50, color: 'yellow', sort: 'адретта'}; | |
var potatoTwo = {weight: 62, color: 'red', sort: 'скороспелка'}; | |
var potatoTree = {weight: 74, color: 'white', sort: 'синеглазка'}; | |
var potatoes = [potatoOne, potatoTwo, potatoTree]; | |
function getRandomIntFromTo(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
var potatoBoom = function(potato, callback){ | |
var neighbor = neighbors[getRandomIntFromTo(0, 2)]; | |
var timeout = getRandomIntFromTo(20, 200); | |
potato.neighbor = neighbor; | |
setTimeout(function(){ | |
callback(potato); | |
}, timeout); | |
}; | |
var trophies = []; | |
var counter = 0; | |
for (var i=0; i<potatoes.length; i++){ | |
var potatoIndex = potatoes.indexOf(potatoes[i]); | |
potatoBoom(potatoes[i],function(potato){ | |
trophies.push(potato); | |
counter ++; | |
if (counter == potatoes.length){ | |
callback(trophies); | |
} | |
}) | |
} | |
}; | |
fillTrophies(function(trophies){ | |
console.log("Журнал трофеев: " + JSON.stringify(trophies)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment