日期:2014-05-16 浏览次数:20354 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <script type="text/javascript"> function customFormatter() { var s = prompt('请输入'); if (!/[01]{24}/.test(s)) { alert('输入的数据不符合要求,请重新输入'); customFormatter(); } else { var result = [], lastIndex = -2; for (var i = 0; i < s.length; i ++) { if (s[i] == '1') { if (i != lastIndex + 1) { result.push([i + 1]); lastIndex = i; } else { if (result[result.length - 1].length < 2) result[result.length - 1].push(i + 1); else result[result.length - 1][1] = i + 1; lastIndex = i; } } } } var msg = ''; if (result.length < 1) msg = '所有周都木有课'; else { var finalResult = []; for (var i = 0; i < result.length; i ++) result[i].length < 2 ? finalResult.push(result[i]) : finalResult.push(result[i].join('-')); msg = finalResult.join(',') + '周上课'; } alert(msg); } customFormatter(); </script> </body> </html>