不确定确切要问什么。但是,我的代码如下。我想做的是使用listPlanets();在if语句中返回用户键入到提示中的数组项。
任何帮助都会很好。
var planets = [
{planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
{planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
{planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
{planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
{planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
{planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
{planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
{planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
];
var listPlanets = function () {
for (var i = 0; i < planets.length; i++) {
document.write(planets[i].planet + ' is planet #' + planets[i].position +
' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>' );
}
}
// listPlanets();
var num = window.prompt("Please enter a number between 1 and 8");
if (1 <= num && num <= 8) {
listPlanets();
} else {
alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive.");
window.location.reload();
}
答案 0 :(得分:2)
@dgeare的答案之所以有效,是因为数组索引与位置属性值匹配。您还可以通过在if
函数中添加一个简单的listPlanets
语句来完成此操作,以检查该函数的参数和position属性是否匹配。
var planets = [
{planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
{planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
{planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
{planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
{planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
{planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
{planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
{planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
];
var listPlanets = function (userInput) {
for (var i = 0; i < planets.length; i++) {
if (planets[i].position == userInput) {
document.write(planets[i].planet + ' is planet #' + planets[i].position +
' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>' );
}
}
}
// listPlanets();
var num = window.prompt("Please enter a number between 1 and 8");
if (1 <= num && num <= 8) {
listPlanets(num);
} else {
alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive.");
window.location.reload();
}
答案 1 :(得分:1)
我认为您的意思是将输入的值作为参数传递给listPlanets函数?
var planets = [
{planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
{planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
{planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
{planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
{planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
{planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
{planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
{planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
];
var listPlanets = function (i) { //i is a parameter that will be passed into the function at the time it is called
i -= 1;//the user entered a value between 1 and 8, but our array indexes are from 0 - 7. decrease input by one
document.write(planets[i].planet + ' is planet #' + planets[i].position +
' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>' );
}
// listPlanets();
var num = window.prompt("Please enter a number between 1 and 8");
if (1 <= num && num <= 8) {
listPlanets(num); //pass num into listPlanets function as argument
} else {
alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive.");
window.location.reload();
}
在上面的代码中,我删除了循环,因为我们已经知道用户的选择。无需执行遍历数组中每个项目的工作,因为我们可以直接访问该项目。由于数组在JavaScript中是基于0的,因此我们需要通过将用户输入减少1来解决这个问题。
正如一些评论所指出的那样,您可能更希望将数据反映到DOM而不是document.write,这会覆盖现有页面,通常被认为是不好的做法。如果您要在DOM中执行此操作,则它看起来可能像这样:
var planets = [
{planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
{planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
{planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
{planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
{planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
{planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
{planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
{planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
];
var listPlanets = function (i) {
i -= 1;
document.getElementById('planet_name').innerHTML = planets[i].planet;
document.getElementById('planet_info').innerHTML = planets[i].planet + ' is planet #' + planets[i].position +
' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>';
}
// listPlanets();
document.getElementById('info_button').addEventListener('click', function(evt){
var num = document.getElementById('planet_input').value;
if (1 <= num && num <= 8) {
listPlanets(num);
} else {
alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive.");
}
});
Pick a planet between 1 and 8 <input id='planet_input' type='number' /><button id='info_button'>View Info</button>
<h1 id='planet_name'></h1>
<h3 id='planet_info'></h3>
答案 2 :(得分:0)
您应该将在提示中输入的索引作为函数参数传递,并使用它来获取数组中的给定项:
var planets = [...];
var listPlanets = function (num) {
var planet = planets[num];
var thingToWrite = planet.planet + ' is planet #' + planet.position + ' from the Sun. Time to complete its orbit is ' + planet.orbit_time ' + earth year(s). It has ' + planet.nat_satellites + ' natural satellite(s).<br>'
document.write(thingToWrite);
}
var num = window.prompt("Please enter a number between 1 and 8");
if (1 <= num && num <= 8) {
listPlanets(num);
} else {
alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive.");
window.location.reload();
}
最好的问候, 伊沃