123123
<!DOCTYPE html><html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<script>
var arr=[-2,-1,0,1,2,3];
// var n=parseInt(prompt());
// for(var i=0;i<n;i++) arr=parseInt(prompt());//自定义输入
function fil(arr,fn){
var newarr=[];
for(var i=0;i<arr.length;i++)
{
if(fn(arr)) newarr.push(arr);
}
return newarr;
}
function fn(a)
{
if(a>0) return true;
else return false;
}
document.write(fil(arr,fn));
</script>
</body>
</html>
-------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编程题答案1</title>
</head>
<body>
<style>
span {
display: inline-block;
width: 80px;
height: 80px;
border-radius: 50%;
border: 1px solid white;
opacity: 0.1;
}
.light {
opacity: 1;
}
#red {
background-color: red;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#time {
margin-left: 30px;
font-size: 70px;
}
</style>
编程题答案1
<p>
<span id="red" class="light"></span>
<span id="yellow"></span>
<span id="green"></span>
<b id="time">10</b>
</p>
<script>
var current = 'red';
var time = 10;
function changeLight(from, to, timeout) {
if (time > 0) {
return;
}
current = to;
time = timeout;
document.getElementById(from).removeAttribute('class', 'light');
document.getElementById(to).setAttribute('class', 'light');
}
setInterval(function() {
time--;
if (current === 'red') {
changeLight('red', 'yellow', 3);
} else if (current === 'yellow') {
changeLight('yellow', 'green', 8);
} else if (current === 'green') {
changeLight('green', 'red', 10);
}
document.getElementById('time').innerText = time;
}, 1000);
</script>
</body>
</html>
-----------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>实现计时器</h1>
<p>
<span id = "hour">00</span>
<span>:</span>
<span id = "minute">00</span>
<span >:</span>
<span id = "second">00</span>
</p>
<button id = "start"> 开始</button>
<button id = "pause"> 暂停</button>
<button id = "stop"> 停止</button>
<script>
var hour = 0 ;
var minute = 0 ;
var second = 0 ;
var intervalId;
function getEleById(id){
return document.getElementById(id) ;
}
function addZero(num){
return num >= 10 ? num : '0' + num ;
}
function setTimeOnDOM() {
getEleById('hour').innerText = addZero(hour) ;
getEleById('minute').innerText = addZero(minute) ;
getEleById('second').innerText = addZero(second) ;
}
getEleById('start').onclick = function() {
if(intervalId){
return;
}
intervalId = setInterval(function() {
second++ ;
if(second >= 60){
second = 0 ;
minute++ ;
}
if(minute >= 60 ){
minute = 0 ;
hour++ ;
}
if(hour >= 24){
clearInterval(intervalId)
}
setTimeOnDOM();
} , 1000)
}
getEleById('pause').onclick = function(){
clearInterval(intervalId);
intervalId = null ;
}
getEleById('stop').onclick = function(){
clearInterval(intervalId) ;
intervalId = null ;
hour = minute = second = 0 ;
setTimeOnDOM() ;
}
</script>
</body>
</html> 你好!你提供的第一个代码是用来实现交通灯控制的,但是代码中有一行是空的:var arr=;,这导致代码不能运行。如果你想要实现一个简单的交通灯控制系统,可以参考如下代码:
var current = 'red';
var time = 10;
function changeLight(from, to, timeout) {
if (time > 0) {
return;
}
current = to;
time = timeout;
document.getElementById(from).removeAttribute('class', 'light');
document.getElementById(to).setAttribute('class', 'light');
}
setInterval(function() {
time--;
if (current === 'red') {
changeLight('red', 'yellow', 3);
} else if (current === 'yellow') {
changeLight('yellow', 'green', 8);
} else if (current === 'green') {
changeLight('green', 'red', 10);
}
document.getElementById('time').innerText = time;
}, 1000);
上述代码会在页面上显示一个交通灯,通过定时器来控制交通灯的颜色和倒计时时间。
第二个代码是一个简单的计时器的实现,点击"开始"按钮后,计时器会开始计时;点击"暂停"按钮,计时器会暂停计时;点击"停止"按钮,计时器会停止计时并重置为0。你可以参考以下代码实现:
<span id="hour">00</span>
<span>:</span>
<span id="minute">00</span>
<span>:</span>
<span id="second">00</span>
<button id="start">开始</button>
<button id="pause">暂停</button>
<button id="stop">停止</button>
<script>
var hour = 0;
var minute = 0;
var second = 0;
var intervalId;
function getEleById(id) {
return document.getElementById(id);
}
function addZero(num) {
return num >= 10 ? num : '0' + num;
}
function setTimeOnDOM() {
getEleById('hour').innerText = addZero(hour);
getEleById('minute').innerText = addZero(minute);
getEleById('second').innerText = addZero(second);
}
getEleById('start').onclick = function() {
if (intervalId) {
return;
}
intervalId = setInterval(function() {
second++;
if (second >= 60) {
second = 0;
minute++;
}
if (minute >= 60) {
minute = 0;
hour++;
}
if (hour >= 24) {
clearInterval(intervalId);
}
setTimeOnDOM();
}, 1000);
}
getEleById('pause').onclick = function() {
clearInterval(intervalId);
intervalId = null;
}
getEleById('stop').onclick = function() {
clearInterval(intervalId);
intervalId = null;
hour = minute = second = 0;
setTimeOnDOM();
}
</script>
以上代码会在页面上显示一个初始为"00:00:00"的计时器,并提供开始、暂停和停止按钮来控制计时器的运行。希望能对你有所帮助!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]