Javascript 可以从函数中选择数组中的特定位置吗?
我不知道到底该问什么。但是,我的代码如下。我想做的是使用listPlanets();在if语句中,返回用户在提示符中键入的数组项 任何帮助都很好Javascript 可以从函数中选择数组中的特定位置吗?,javascript,arrays,function,Javascript,Arrays,Function,我不知道到底该问什么。但是,我的代码如下。我想做的是使用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'},
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();
}
var=[
{行星:'Mercury',位置:'1',轨道时间:'0.24',卫星:'0'},
{行星:“金星”,位置:“2”,轨道时间:“0.62”,卫星:“0”},
{行星:'地球',位置:'3',轨道时间:'1',卫星:'1'},
{行星:'火星',位置:'4',轨道时间:'1.88',卫星:'2'},
{行星:'Jupiter',位置:'5',轨道时间:'11.86',卫星:'67'},
{行星:'土星',位置:'6',轨道时间:'29.46',卫星:'62'},
{行星:'天王星',位置:'7',轨道时间:'84.32',卫星:'27'},
{行星:'Neptune',位置:'8',轨道时间:'164.79',卫星:'14'}
];
var=函数(){
对于(变量i=0;i<1.length;i++){
document.write(行星[i]。行星+'是行星#'+行星[i]。位置+
来自太阳。完成其轨道的时间为“+行星[i]。轨道时间为“+地球年”。它有“+行星[i]。自然卫星+”。
;
}
}
//列表行星();
var num=window.prompt(“请输入一个介于1和8之间的数字”);
如果(1我认为您的意思是将输入的值作为参数传递到listPlanets函数中
var=[
{行星:'Mercury',位置:'1',轨道时间:'0.24',卫星:'0'},
{行星:“金星”,位置:“2”,轨道时间:“0.62”,卫星:“0”},
{行星:'地球',位置:'3',轨道时间:'1',卫星:'1'},
{行星:'火星',位置:'4',轨道时间:'1.88',卫星:'2'},
{行星:'Jupiter',位置:'5',轨道时间:'11.86',卫星:'67'},
{行星:'土星',位置:'6',轨道时间:'29.46',卫星:'62'},
{行星:'天王星',位置:'7',轨道时间:'84.32',卫星:'27'},
{行星:'Neptune',位置:'8',轨道时间:'164.79',卫星:'14'}
];
var listplanes=function(i){//i是一个参数,在调用该函数时将传递给该函数
i-=1;//用户输入了一个介于1和8之间的值,但是我们的数组索引是从0到7。将输入减少1
document.write(行星[i]。行星+'是行星#'+行星[i]。位置+
来自太阳。完成其轨道的时间为“+行星[i]。轨道时间为“+地球年”。它有“+行星[i]。自然卫星+”。
;
}
//列表行星();
var num=window.prompt(“请输入一个介于1和8之间的数字”);
如果(1我认为您的意思是将输入的值作为参数传递到listPlanets函数中
var=[
{行星:'Mercury',位置:'1',轨道时间:'0.24',卫星:'0'},
{行星:“金星”,位置:“2”,轨道时间:“0.62”,卫星:“0”},
{行星:'地球',位置:'3',轨道时间:'1',卫星:'1'},
{行星:'火星',位置:'4',轨道时间:'1.88',卫星:'2'},
{行星:'Jupiter',位置:'5',轨道时间:'11.86',卫星:'67'},
{行星:'土星',位置:'6',轨道时间:'29.46',卫星:'62'},
{行星:'天王星',位置:'7',轨道时间:'84.32',卫星:'27'},
{行星:'Neptune',位置:'8',轨道时间:'164.79',卫星:'14'}
];
var listplanes=function(i){//i是一个参数,在调用该函数时将传递给该函数
i-=1;//用户输入了一个介于1和8之间的值,但是我们的数组索引是从0到7。将输入减少1
document.write(行星[i]。行星+'是行星#'+行星[i]。位置+
来自太阳。完成其轨道的时间为“+行星[i]。轨道时间为“+地球年”。它有“+行星[i]。自然卫星+”。
;
}
//列表行星();
var num=window.prompt(“请输入一个介于1和8之间的数字”);
if(1@dgeare的答案之所以有效,是因为数组索引与位置属性值匹配。您还可以通过在listplanes
函数中添加一个简单的if
语句来检查函数参数与位置属性是否匹配
var=[
{行星:'Mercury',位置:'1',轨道时间:'0.24',卫星:'0'},
{行星:“金星”,位置:“2”,轨道时间:“0.62”,卫星:“0”},
{行星:'地球',位置:'3',轨道时间:'1',卫星:'1'},
{行星:'火星',位置:'4',轨道时间:'1.88',卫星:'2'},
{行星:'Jupiter',位置:'5',轨道时间:'11.86',卫星:'67'},
{行星:'土星',位置:'6',轨道时间:'29.46',卫星:'62'},
{行星:'天王星',位置:'7',轨道时间:'84.32',卫星:'27'},
{行星:'Neptune',位置:'8',轨道时间:'164.79',卫星:'14'}
];
var listplanes=函数(用户输入){
对于(变量i=0;i<1.length;i++){
if(行星[i]。位置==用户输入){
document.write(行星[i]。行星+'是行星#'+行星[i]。位置+
来自太阳。完成其轨道的时间为“+行星[i]。轨道时间为“+地球年”。它有“+行星[i]。自然卫星+”。
;
}
}
}
//列表行星();
var num=window.prompt(“请输入一个介于1和8之间的数字”);
if(1@dgeare的答案之所以有效,是因为数组索引与位置属性值匹配。您还可以通过在listplanes
函数中添加一个简单的if
语句来检查函数参数与位置属性是否匹配
var=[
{行星:'Mercury',位置:'1',轨道时间:'0.24',卫星:'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();
}