Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Associative arrays

Читайте также:
  1. Creating and using arrays
  2. Creating and using arrays Создание и использование массивов
  3. List of variables, arrays and fields

//This is an associative array of the results of the student roll call.

//If the value of the key is false, then the student was absent.

var students = {"robert":false,"joe":true,"sharon":true};

 

//Loop over the students associative array. If the student was present

//output their name to the console

 

for (var key in students){

if (students[key]===true){

console.log(key);

}

}

 

_____________________________________________________________________________________

INDEXED ARRAY OF ASSOCIATIVE ARRAYS

var hand = [{"suit":"clubs","rank":8}, {"suit":"spades","rank":"A"}, {"suit":"hearts","rank":2}, {"suit":"hearts","rank":"K"}, {"suit":"clubs","rank":9}];

_____________________________________________________________________________________

Объединение 2 массивов

var deck = [];

var suits = ['clubs','diamonds','hearts','spades'];

var ranks = [2,3,4,5,6,7,8,9,10,'J','Q','K','A'];

for (var i=0;i<suits.length;i++) {

for (var j=0;j<ranks.length;j++) {

var card = {'rank': ranks[j], 'suit':suits[i]};

deck.push(card);

}

}

_____________________________________________________________________________________

ПОКЕР

//here's our players array from exercise 1

var players = [];

players[0] = {'name': 'Robert', hand: []};

players[1] = {'name': 'Joe', hand: []};

 

//here's our code to create the deck

var suits = ['clubs','diamonds','hearts','spades'];

var ranks = [2,3,4,5,6,7,8,9,10,'J','Q','K','A'];

var deck = [];

for (var i=0;i<suits.length;i++) {

for (var j=0;j<ranks.length;j++) {

var card = {'rank': ranks[j], 'suit':suits[i]};

deck.push(card);

}

}

 

//This will shuffle the deck. Nothing for you to do here. Just here to

//make the final output a little more realistic

deck.sort(function() {return 0.5 - Math.random()});

 

//Deal 5 cards to each player. Make sure you deal them out

//one player at a time, just like in a real poker game.

 

 

var c = 0;

for (var i=0; i<5; i++) {

for (var p=0;p<players.length;p++) {

var card = deck[c];

players[p].hand.push(card);

c++;

}

}

_____________________________________________________________________________________


Дата добавления: 2015-08-17; просмотров: 44 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Instanceof| УГАДАЙ ЧИСЛО С ПОДСКАЗКАМИ

mybiblioteka.su - 2015-2024 год. (0.005 сек.)