Answer to Question #26590 in AJAX | JavaScript | HTML | PHP for Anshu

Question #26590
I am a beginner and i want to know how to execute the below pattern in through javascript,
*
**
***
****
*****
and
*****
***
*
*
***
*****
1
Expert's answer
2013-03-19T10:35:27-0400
You can generate required patterns in such way:





-Pattern





*


**


***


****





JavaScript:





document.write('Pattern
#1</br>');


var
count=10;//maximum number of * symbol


var
pattern='';


for
(var i=1;i<=count;i++) {


document.write((pattern+='*')+'</br>');//change
pattern and write it to document


}





-Pattern





*****


***


*


*


***


*****





Javascript:





document.write('Pattern
#2</br>');


var
pattern='*****';


var
addIt='';


var
addItSymb=' ';//symbol to add





/*


*****


***


*


*/


while
((2*addIt.length)<pattern.length) {


document.write(addIt+pattern.substring(0,pattern.length-2*addIt.length)+addIt+'</br>');


addIt+=addItSymb;


}


/*


*


***


*****


*/


while
(addIt.length!=0) {


addIt=addIt.substring(0,addIt.length-1);


document.write(addIt+pattern.substring(0,pattern.length-2*addIt.length)+addIt+'</br>');


}





To view results you can use such HTML document





<html>


<head>


<script
type="text/javascript">


<!--
INSERT JAVASCRIPT HERE -->


</script>


</head>


<body>


</body>


</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS