一个JS的问题,绕不过去了。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Dummy

一个JS的问题,绕不过去了。

  •  
  •   Dummy Oct 21, 2012 4821 views
    This topic created in 4938 days ago, the information mentioned may be changed or developed.
    刚学JS编程, 哥哥给出了道JS的题,有个思考绕不过去,求指点:

    有四张卡片,每张都有正反面。
    程序开始执行,五秒后第一张卡片翻面,过五秒再翻回来,再过10秒下一张卡片翻面,也是五秒之后再翻回来...

    依次类推到第四次卡片翻回来之后,再翻第一张卡片。。

    怎么用卡片翻面的脚本应该很简单就不求解了,只想请教一下这个逻辑怎么写?

    请V2EX的同学能够帮助一下我。
    11 replies    1970-01-01 08:00:00 +08:00
    hyq
        1
    hyq  
       Oct 21, 2012
    http://gist.github.com/3925580
    不知道这个是不是你想要的
    54sword
        2
    54sword  
       Oct 21, 2012
    //true 正面, false 反面
    var card = ['false', 'false', 'false', 'false'];
    var index = 0;
    function startTurnCard() {
    if (index == card.length) index = 0;
    var time = 5000 * index;
    setTimeout(function() {
    card[index] = true;
    console.log((index + 1) + "卡片被翻到正面");
    setTimeout(function() {
    card[index] = false;
    console.log((index + 1) + "卡片被翻回反面");
    index++;
    startTurnCard();
    }, 5000);
    }, 5000 + time);
    }
    startTurnCard();

    也尝试写了一下,看看是否正确。
    zhangxiao
        3
    zhangxiao  
       Oct 21, 2012
    // 第一个函数,负责翻一张卡片,参数是卡片
    f1(card_id) {
    //翻卡片
    }

    // 第二个函数,负责每隔5s调用第一个函数,参数是卡片和调用次数(这里是2)
    f2(card_id, times) {
    for i in [0..times]
    f1(card_id)
    sleep(5)
    }

    // 第三个函数,负责每隔10s调用第二个函数,参数是卡片们
    f3(card_ids) {
    while true
    for i in [0..card_ids.length]
    f2(card_ids[i])
    }

    都是伪代码,逻辑应该能表示清楚了
    zhangxiao
        4
    zhangxiao  
       Oct 21, 2012
    呃...上面的f3里忘记sleep(10)了
    Dummy
        5
    Dummy  
    OP
       Oct 21, 2012
    @hyq 结果不对。

    5.002 "卡片0正面"
    10.004 "卡片0反面"
    20.007 "卡片1正面"
    25.009 "卡片1反面"
    40.011 "卡片2正面" //这儿好像不对,应该是 35才对。。
    hyq
        6
    hyq  
       Oct 21, 2012
    @Dummy 那个时间的序列是 5 5 10 5 10 5 10 5...?
    Dummy
        7
    Dummy  
    OP
       Oct 21, 2012
    @hyq 以下是你的程序算出的结果:

    5.001 "卡片0正面" //正确
    10.003 "卡片0反面" //正确
    20.005 "卡片1正面" //正确
    25.006 "卡片1反面" //正确
    40.007 "卡片2正面" //错误,应该是35
    45.008 "卡片2反面" //上一个不正确,这一个自然就错了,应该是 40
    65.009 "卡片3正面" //应该是 50,原因同上。
    hyq
        8
    hyq  
       Oct 21, 2012
    @Dummy 那就是我理解有误,你把order+1全部替换成2就行了
    Dummy
        9
    Dummy  
    OP
       Oct 21, 2012
    @hyq 也不对。
    hyq
        10
    hyq  
       Oct 21, 2012
    5 "卡片0正面"
    10.001 "卡片0反面"
    20.001 "卡片1正面"
    25.002 "卡片1反面"
    35.002 "卡片2正面"
    40.002 "卡片2反面"
    50.002 "卡片3正面"
    55.002 "卡片3反面"
    这个结果,不知道你是指哪里有问题
    tshwangq
        11
    tshwangq  
       Oct 21, 2012
    思路,牌+空档共有3个状态,4张牌12个状态也就是12位
    var cards = 0x6db,seq = 0,bits = 12;

    window.setInterval(function () {
    seq++;
    if ((cards & (0x1 << (seq-1) % bits)) > 0) {
    console.debug(seq * 5 + ":" + ~~(seq / 3)%4 + "/" + ((seq % 2) ^ ( ~~(seq / 3)%2)));
    } else {
    console.debug(seq * 5 + ":");
    }
    }, 500);
    About     Help     Advertise     Blog     API     FAQ     Solana     1079 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 18:40 PVG 02:40 LAX 11:40 JFK 14:40
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86