var par = new Array(2);
par.push("Apple");
par.push("Orange");
var nar = new Array("2");
nar.push("red ");
nar.push("dark orange");
var ok = document.getElementById('one').value;
function abc() {
for (var i = 0; i < par.length; i++) {
if (par[i] == ok) {
break;
}
}
document.write(par[i] + " " + "color is " + " " + nar[i]);
}
<!doctype html>
<html>
<head>
</head>
<body>
<label>Enter the name</label>
<input type="text" id="one">
<input type="submit" onclick="abc()" id="on">
</body>
</html>
// In the above code snippets I am trying to display the color of apple and orange by defining the required details in an array but while running the code I am getting output as "undefined color is undefined".I am unable to figure out the exact problem in my code.
//在上面的代码片段中,我试图通过在数组中定义所需的细节来显示苹果和橙色的颜色但是在运行代码时我得到输出为“未定义的颜色未定义”。我无法弄清楚我的代码中的确切问题。
4 个解决方案
#1
1
In Ist Statement you have used var par =new Array(2); This means it is creating 2 empty slots and then 2 values so creating array of length 4 . And in 2nd one you are using var par =new Array("2"); means ist value s string and then 2 value so creating array of length 3. So you are getting wrong result. Use new Array() this. Also get the value of ok inside abc function
在Ist语句中,您使用了var par = new Array(2);这意味着它创建了2个空插槽,然后创建了2个值,因此创建了长度为4的数组。在第二个你使用var par = new Array(“2”);表示ist值s字符串然后是2值,因此创建长度为3的数组。所以你得到错误的结果。使用新的Array()。还可以在abc函数中获取ok的值
var par =new Array();
par.push("Apple");
par.push("Orange");
var nar= new Array();
nar.push("red ");
nar.push("dark orange");
function abc(){
var ok= document.getElementById('one').value;
for (var i=0;i<par.length;i++){
if(par[i]==ok){
break;
}
}
document.write(par[i]+" "+"color is "+" "+nar[i]);
}
<!doctype html>
<html>
<head>
</head>
<body>
<label>Enter the name</label>
<input type="text" id="one">
<input type="submit" onclick="abc()" id="on">
</body>
</html>
#2
0
in your code after the loop ends i,j
will be 2 and bar[2]
or nar[2]
will be undefined (accessing element of array with not existing index)
在循环结束后你的代码中,j将是2并且bar [2]或nar [2]将是未定义的(访问没有现有索引的数组元素)
you need to move the write command to loop
你需要将write命令移动到循环
for (var i = 0; i < par.length; i++) {
if (par[i] == ok) {
break;
}
document.write(par[i] + " " + "color is " + " " + nar[i]);
}
#3
0
The problem is you define ok
outside the abc()
function. At the time you define it (as the pages loads), the input is empty. When you click on your submit button, ok
is still the value of the input as it loaded. The following should work :
问题是你在abc()函数之外定义了ok。在您定义它时(当页面加载时),输入为空。当您单击提交按钮时,ok仍然是加载时输入的值。以下应该有效:
function abc(){
for (var i=0;i<par.length;i++){
var ok= document.getElementById('one').value;
if(par[i]==ok){
break;
}
}
document.write(par[i]+" "+"color is "+" "+nar[i]);
}
#4
0
<html>
<head>
</head>
<body>
<label>Enter the name</label>
<input type="text" id="one">
<input type="submit" onclick="abc()" id="on">
<script>
var par =new Array("Apple","Orange");
var nar= new Array("red ","dark orange");
var ok= document.getElementById('one').value;
function abc(){
for (var i=0;i<par.length;i++){
if(par[i]==ok){
break;
}
document.write(par[i]+" "+"color is "+" "+nar[i]);
}
}
</script>
</body>
</html>
#1
1
In Ist Statement you have used var par =new Array(2); This means it is creating 2 empty slots and then 2 values so creating array of length 4 . And in 2nd one you are using var par =new Array("2"); means ist value s string and then 2 value so creating array of length 3. So you are getting wrong result. Use new Array() this. Also get the value of ok inside abc function
在Ist语句中,您使用了var par = new Array(2);这意味着它创建了2个空插槽,然后创建了2个值,因此创建了长度为4的数组。在第二个你使用var par = new Array(“2”);表示ist值s字符串然后是2值,因此创建长度为3的数组。所以你得到错误的结果。使用新的Array()。还可以在abc函数中获取ok的值
var par =new Array();
par.push("Apple");
par.push("Orange");
var nar= new Array();
nar.push("red ");
nar.push("dark orange");
function abc(){
var ok= document.getElementById('one').value;
for (var i=0;i<par.length;i++){
if(par[i]==ok){
break;
}
}
document.write(par[i]+" "+"color is "+" "+nar[i]);
}
<!doctype html>
<html>
<head>
</head>
<body>
<label>Enter the name</label>
<input type="text" id="one">
<input type="submit" onclick="abc()" id="on">
</body>
</html>
#2
0
in your code after the loop ends i,j
will be 2 and bar[2]
or nar[2]
will be undefined (accessing element of array with not existing index)
在循环结束后你的代码中,j将是2并且bar [2]或nar [2]将是未定义的(访问没有现有索引的数组元素)
you need to move the write command to loop
你需要将write命令移动到循环
for (var i = 0; i < par.length; i++) {
if (par[i] == ok) {
break;
}
document.write(par[i] + " " + "color is " + " " + nar[i]);
}
#3
0
The problem is you define ok
outside the abc()
function. At the time you define it (as the pages loads), the input is empty. When you click on your submit button, ok
is still the value of the input as it loaded. The following should work :
问题是你在abc()函数之外定义了ok。在您定义它时(当页面加载时),输入为空。当您单击提交按钮时,ok仍然是加载时输入的值。以下应该有效:
function abc(){
for (var i=0;i<par.length;i++){
var ok= document.getElementById('one').value;
if(par[i]==ok){
break;
}
}
document.write(par[i]+" "+"color is "+" "+nar[i]);
}
#4
0
<html>
<head>
</head>
<body>
<label>Enter the name</label>
<input type="text" id="one">
<input type="submit" onclick="abc()" id="on">
<script>
var par =new Array("Apple","Orange");
var nar= new Array("red ","dark orange");
var ok= document.getElementById('one').value;
function abc(){
for (var i=0;i<par.length;i++){
if(par[i]==ok){
break;
}
document.write(par[i]+" "+"color is "+" "+nar[i]);
}
}
</script>
</body>
</html>