1

I'm new in JavaScript and I want to transform this array ["Banana", "Orange", "Apple", "Mango"], in this one [["Banana"], ["Orange"], ["Apple"], ["Mango"]], but when I try to do it, my browser freezes. I'm using this code:

<script>
var i = 0;
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
 var fruits_aux = [];
 for (i=0; fruits.length; i++)
 fruits_aux.push([fruits[i]]);
 fruits = fruits_aux;
 document.getElementById("demo").innerHTML = fruits;
}
</script>

Be careful executing this code. Anyone can help me? Thanks

Nikhil Aggarwal
28.5k4 gold badges46 silver badges61 bronze badges
asked Jun 24, 2016 at 14:55

1 Answer 1

2

In your for loop, the condition always evaluates to true, hence, it becomes an infinite loop and the reason for your browser freeze.

for (i=0; fruits.length; i++)

should probably be

for (i=0; i < fruits.length; i++)
Nikhil Aggarwal
28.5k4 gold badges46 silver badges61 bronze badges
answered Jun 24, 2016 at 14:57
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear that - please accept it as answer if it solved the issue for you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.