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

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++)
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

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.