How do you loop a string inside an alert box?
-
I just want to be able to do it because I can't think how it could be achieved after trying various combinations. I want to send an alert to myself in my browser like this: javascript:var text = "Hello"; alert(for(var i = 0; i <= 101; i++) { print name; }) This doesn't work, but what I want to do is loop the string 'text' so "Hello" appears 100 times in the alert box. Thanks!
-
Answer:
You can do like this: var myString='Hello'; for ( i = 1; i < 100; i++ ){ myString=myString+ '\n' +'Hello'; } alert(myString); http://jsfiddle.net/aw3gh/
Raghav Khunger at Quora Visit the source
Other answers
First of all you are doing it wrong. 'alert' function takes sting as a parameter. So the for loop which you are trying to invoke inside 'alert' will not work. Second the 'print' function you are using is actually used to print the page if used correctly. Source: https://developer.mozilla.org/en-US/docs/DOM/window.alert, http://stackoverflow.com/questions/1247040/how-does-the-javascript-print-function-work-can-i-create-a-document-using-javas, http://www.htmlgoodies.com/beyond/javascript/article.php/3471121/Print-a-Web-Page-Using-JavaScript.htm So how to do it? Fairly simple loop on variable 'text' outside of alert and take the value in some other variable and finally alert this other variable. Below is the snippet. var text = 'Hello'; var text_to_repeat = ''; for(i=0; i<100; i++){ text_to_repeat += text; } alert(text_to_repeat);
Neeraj Khandelwal
i wish to add a few more terms here in the output. suppose of we want to generate a random number and the alert box should show the random number generated and the number of stars equal to that number. eg. if the random number is 5, then the box should say " 5. *****" var x = Math.floor(( Math.random() * 8) + 1) var j = '*' for( var i=0; i<=x; i++) { j = j + '*' } alert(x + j) This is what i was trying, but didn't work. What changes are to made?
Daipayan Roy
Related Q & A:
- How to replace a string inside of a Word Document?Best solution by Stack Overflow
- How do you hash a string to SHA512 in Swift?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How do I split a String and keep the delimiters?Best solution by Stack Overflow
- How do you put a string on an acoustic guitar?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.