Skip to content

Instantly share code, notes, and snippets.

@droganaida
Created October 18, 2016 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save droganaida/30e9dab130e6342dacb8e7a36995b29b to your computer and use it in GitHub Desktop.
Save droganaida/30e9dab130e6342dacb8e7a36995b29b to your computer and use it in GitHub Desktop.
//----------БЕЗУСЛОВНОЕ УСЛОВИЕ------------//
var layout = 'mobile';
if (layout = 'desktop'){
console.log('Будем парсить большой баннер на всю страницу и флеш-плеер');
} else {
console.log('Будем парсить скромную мобильную темплейту');
}
var layout = 'mobile';
if (layout == 'desktop'){
console.log('Будем парсить большой баннер на всю страницу и флеш-плеер');
} else {
console.log('Будем парсить скромную мобильную темплейту');
}
//------------ОБЩИЙ СЧЕТЧИК-------------//
var dataArray = [{id:'1', score: [1,2,3,4,5]}, {id:'2', score: [6,7,8,9,10]}, {id:'3', score: [11,12,13,14,15]}];
for (var i= 0; i<dataArray.length; i++){
var score = dataArray[i].score;
console.log('Элемент массива под номером ' + dataArray[i].id);
for (var i= 0; i<score.length; i++){
console.log(score[i]);
}
}
var dataArray = [{id:'1', score: [1,2,3,4,5]}, {id:'2', score: [6,7,8,9,10]}, {id:'3', score: [11,12,13,14,15]}];
for (var i= 0; i<dataArray.length; i++){
var score = dataArray[i].score;
console.log('Элемент массива под номером ' + dataArray[i].id);
for (var j= 0; j<score.length; j++){
console.log(score[j]);
}
}
//--------------------ЧУЖОЙ CALLBACK-----------------------//
var fooObject = {};
function GoToOne(callback){
function GoToTwo(parameter){
fooObject.size = 14;
callback();
}
GoToTwo(1, function(){
fooObject.color = 'red';
callback();
});
}
GoToOne(function(){
console.log(JSON.stringify(fooObject));
});
var fooObject = {};
function GoToOne(callback){
function GoToTwo(parameter, callback){
fooObject.size = 14;
callback();
}
GoToTwo(1, function(){
fooObject.color = 'red';
callback();
});
}
GoToOne(function(){
console.log(JSON.stringify(fooObject));
});
//---------------БЕСКОНЕЧНЫЙ ЦИКЛ-----------------//
var mainArticle = {id:1, title:'Статья 1'};
var articlesLimit = 4;
function randomArray(articles, callback){
var array = [];
var indexArray = [];
if (articles.length >= articlesLimit){
for (var i=0; i<=articlesLimit-1; i++){
var rand = Math.floor(Math.random() * articles.length) + 0;
while ((articles[rand].id == mainArticle.id) || (indexArray.indexOf(rand) != -1)){
rand = Math.floor(Math.random() * articles.length) + 0;
}
indexArray.push(rand);
array.push(articles[rand]);
if (i == articlesLimit-1){
callback(array);
}
}
} else {
callback(articles);
}
}
var articlesFromDB = [{id:1, title:'Статья 1'}, {id:2, title:'Статья 2'}, {id:3, title:'Статья 3'}, {id:4, title:'Статья 4'}];
randomArray(articlesFromDB, function(resArticles){
console.log('—–articles—–' + JSON.stringify(resArticles));
});
var mainArticle = {id:1, title:'Статья 1'};
var articlesLimit = 4;
function randomArray(articles, callback){
var array = [];
var indexArray = [];
if (articles.length > articlesLimit){
for (var i=0; i<=articlesLimit-1; i++){
var rand = Math.floor(Math.random() * articles.length) + 0;
while ((articles[rand].id == mainArticle.id) || (indexArray.indexOf(rand) != -1)){
rand = Math.floor(Math.random() * articles.length) + 0;
}
indexArray.push(rand);
array.push(articles[rand]);
if (i == articlesLimit-1){
callback(array);
}
}
} else {
callback(articles);
}
}
var articlesFromDB = [{id:1, title:'Статья 1'}, {id:2, title:'Статья 2'}, {id:3, title:'Статья 3'}, {id:4, title:'Статья 4'}];
randomArray(articlesFromDB, function(resArticles){
console.log('—–articles—–' + JSON.stringify(resArticles));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment