Reputation: 205
My program should take the text from the first paragraph, duplicate it 100 times and put that into paragraph two. But when I do this function:
<html>
<body>
<p id="first">First</p>
<p id="second">Second</p>
<script>
var i;
var word = document.getElementsByTagName('p')[0].innerHTML;
document.writeln(word); //Did this to check if it copied "first" correctly.
for (i=0; i<100; i++){
word+=word;
document.write(i) //Did this to see how man time it loops
}
document.getElementById("second").innerHTML = word;
</script>
</body>
</html>
It returns this in Safari (Loops only 27 times):
First
Second
First 0123456789101112131415161718192021222324252627
And Returns this in Chrome (Loops only 24 times):
First
Second
First 0123456789101112131415161718192021222324
Thanks!
Upvotes: 0
Views: 114
Reputation: 3340
You mistake in that line . you add word
instead of you have to add 'i'.
So it should be like this:
word+=i;
Upvotes: 0
Reputation: 751
What your program is trying to do now to make the second paragraph contain 2^100 times "First", since you duplicate the duplicates, after the first loop you have 2*"First", then 4*"First" etc. This obviously gives memory problems. If you instead wanted to make the second paragraph contain only 100 times "First" you could just use:
<script>
document.getElementById("second").innerHTML =
document.getElementById("first").innerHTML.repeat(100)
</script>
Upvotes: 1