将整数显示为有序列表,使用for循环将背景颜色更改为浅蓝色



我写了这段代码,但如何将整数显示为有序列表,使用for循环将背景色更改为浅蓝色?如果你能帮忙,请帮忙,并提前表示感谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 6.14: welcome5.html -->
<!-- Using equality and relational operators. -->
<html>
<head>
<title>Compare Numbers</title>
<script type="text/javascript">
var firstNumber, // first string entered by the user
secondNumber, // second string entered by the user
thirdNumber, // third string entered by the user
sum, // add the integers
average, // add the integers, divide by 3
product; // multiply the integers
// enter first number from user as a string
firstNumber = window.prompt("Enter first integer");
// enter second number from user as a string
secondNumber = window.prompt("Enter second integer");
// enter third number from user as a string
thirdNumber = window.prompt("Enter third integer");
// convert numbers from strings to integers
firstNumber = parseInt(firstNumber);
secondNumber = parseInt(secondNumber);
thirdNumber = parseInt(thirdNumber);
findLargNum(firstNumber,secondNumber,thirdNumber);
function findLargNum(firstNumber,secondNumber,thirdNumber){
//determine result
if (firstNumber > secondNumber) {
if(firstNumber > thirdNumber) window.alert(firstNumber + " is largest");
}
else if(secondNumber > thirdNumber) window.alert(secondNumber + " is largest");
else window.alert(thirdNumber + " is largest");
var sum =  firstNumber + secondNumber + thirdNumber;
window.alert("the sum is " + sum); 
var average = (firstNumber + secondNumber + thirdNumber) / 3;
window.alert("the average is " + average);var product = firstNumber * secondNumber * thirdNumber;window.alert("the product is" + product);
}
</script>
</head>
<body>
<h1 style = "text-align:center">Compare the Integers!</h1>
</body>
</html> ```

如果这不是您想要的解决方案,请原谅,但是,假设您有一个值数组,您可以使用以下代码段创建一个有序列表并将其添加到文档主体:

document.addEventListener('DOMContentLoaded', () => {
var values = [15, 72, 50, 45, 12];
var list = document.createElement('ol');
values.forEach(value => {
var item = document.createElement('li');
item.innerText = value;
list.append(item);
});
list.style.backgroundColor = 'lightblue';
document.body.append(list);
});

在您的情况下,您可以在代码中添加以下内容:

var list = document.createElement('ol');
[firstNumber, secondNumber, thirdNumber].forEach(value => {
var item = document.createElement('li');
item.innerText = value;
list.append(item);
});
list.style.backgroundColor = 'lightblue';
document.body.append(list);

如果你特别需要一个for循环,这应该可以工作:

var list = document.createElement('ol');
for(var value of [firstNumber, secondNumber, thirdNumber]) {
var item = document.createElement('li');
item.innerText = value;
list.append(item);
}
list.style.backgroundColor = 'lightblue';
document.body.append(list);

希望这个解决方案对您有用!

相关内容

  • 没有找到相关文章

最新更新