開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
Head First JavaScript ―頭とからだで覚えるJavaScriptの基本( Michael Morrison (著), 豊福 剛 (翻訳)、オライリージャパン)の6章(関数)、getSeatStatus()のマグネット(p.267)をDartで考えてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
getSeatStatus()のマグネット(p.267)
コード
sample.dart
import 'dart:html';
void main() {
  [input_selected_seat, input_seat_num].forEach((InputElement input){
    input.onKeyUp.listen((KeyboardEvent event){
      try{
        selected_seat = int.parse(input_selected_seat.value);
        seat_num = int.parse(input_seat_num.value);
        pre.text = getSeatStatus(seat_num);
      } catch (e) {
        pre.text = '$e';
      }
    });
  });
}
InputElement input_selected_seat = querySelector('#selected_seat');
InputElement input_seat_num = querySelector('#seat_num');
PreElement pre = querySelector('#pre0');
int selected_seat;
int seat_num;
List<List<bool>> seats = [[false, true, false, true, true, true, false, true, false],
                          [false, true, false, false, true, false, true, true, true],
                          [true, true, true, true, true, true, false, true, false],
                          [true, true, true, false, true, false, false, true, false]];
String getSeatStatus(int seat_num){
  if (selected_seat != -1 &&
      (seat_num == selected_seat || seat_num == selected_seat + 1 ||
      seat_num == selected_seat + 2)) {
    return 'あなたの席';
  }
  int l = seats[0].length;
  if (seats[seat_num ~/ l][seat_num % l]){
    return '空席';
  }
  return '満席';
}
  
   
0 コメント:
コメントを投稿