Question: ```js
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]);

}
```

```html
<!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.

Answer: 
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

```js
 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]);
        }
```

```html
        <!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> 
```
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 :

```
        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]);
        }

```