Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Hey I am trying to print out #, by increasing the amount of # for each line. like this:
#
##
###
####
#####
Here is the for loop that I have tried to work this out with:
var printout = "";
for(var i=0;i<5;i++) {
printout+= "#" + " <br>";
You can add one more variable for incrementing #
var printout = "", a = '';
for (var i = 0; i < 5; i++) {
printout += (a += '#') + " <br>";
}
document.body.innerHTML += printout;
If you're just trying to print them:
for (var i=0; i < 5; i++) {
console.log("#".repeat(i+1))
}
Check this out, output will be in HTML.
var printout = "";
for(var i=0;i<5;i++) {
for(var j=0; j<=i; j++)
{
printout += "#";
}
printout += " <br>";
}
var pvar = document.getElementById("check");
pvar.innerHTML = printout;
<p id="check">
</p>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I've got trouble with arrays. I'm trying to iterate elements for each position in the array using javascript.
so, I need to do that on the first position, be a value [1]. On the second position, [2, 3, 4]. on the third position, [5,6,7,8,9] and successively two by two.
My attempt was did a for loop:
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= 5; j++) {
console.log(`${i} ${j}`)
}
}
But while looping, the indices (i) repeat with (j).
What to do?
Try this code.
var counting = 1;
for (let i = 1; i <= 5; i = i + 2) {
var str = "";
for (let j = 1; j <= i; j++) {
str += `${counting} `;
counting++;
}
console.log(str);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I know this is very simple question, but I don't know why it only giving same output.
I'm trying to randomize 4 numbers from 1 to 100
I'm very new to javascript
this is my code:
function myFunction() {
var text = "",
i;
var x = Math.floor((Math.random() * 100) + 1);
for (i = 1; i <= 4; i++) {
text += " number " + x;
}
document.getElementById("rand").innerHTML = text;
}
<button onclick="myFunction()">Generate</button>
<p id="rand"></p>
You are calling your randomize part of code outside the for loop. It should be recalculated on every step of the loop. Something like this
function myFunction() {
var text = "",
i;
for (i = 1; i <= 4; i++) {
var x = Math.floor((Math.random() * 100) + 1);
text += " number " + x;
}
document.getElementById("rand").innerHTML = text;
}
<button onclick="myFunction()">Generate</button>
<p id="rand"></p>
function myFunction() {
var text = "";
var i = 0;
randNos = [];
while(i<4) {
x = rand();
if(randNos.indexOf(x) === -1) {
text += " number " + x ;
randNos.push(x);
i++
}
}
document.getElementById("rand").innerHTML = text;
}
function rand() {
return Math.floor((Math.random() * 100) + 1);
}
You were trying to print the same random no 4 times. You just need to move your random no line inside the loop
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I want to add one space in between each outcome in this for loop,
for (var i = -52; i <= 1066; i++) {
document.write(i)
}
You can add to do it
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}
Or just add to space in the write
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}
this should help you out.
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}
or
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}
Browsers are designed to ignore whitespace such as spaces, typing in multiple ' ' (spaces) will be reduced to a single space. If you're looking to add multiple spaces in Javascript the easiest way is to use  . Such tags are called entities and numerous characters can be printed using entities. More can be found here
for(var i = 0;i < n;i++)
{
document.write(" ")
}
for (var i = 52; i <= 1066; i++) {
document.write(i+'\t') }
Try this:
let result = [];
for (let i = -52; i <= 1066; i++) {
result.push(i);
}
document.write(result.join(" "));
Suggestion: avoid using document.write at best, especially in a big for loop.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm making a JavaScript program where if a user inputs a sting such as
"i love my country"
the output will produce:
country
my
love
i
I have tried using split function but not getting the required output.
<script language="javascript">
var n, x, i, j;
rev = "";
n = window.prompt("enter any string");
x = n.split(" ");
for (i = 0; i < x.length; i++) {
for (j = x[i].length() - 1; j >= 0; j--) {
document.writeln(x[i].charAt(j));
document.write(rev);
}
}
</script>
Try using Array.prototype.reverse() , Array.prototype.join()
var str = "I LOVE MY COUNTRY";
console.log(str.split(/\s/).reverse().join(" "));
Remove () at j = x[i].length() ; .length is not a function ; adding document.write("<br>") below inner for loop to include <br> tag in output after each iteration of i to place line break in html following a word entered at prompt . E.g., try input of "I love my country" at prompt() at stacksnippets
var n, x, i, j;
rev = "";
n = window.prompt("enter any string");
x = n.split(" ");
for (i = 0; i < x.length; i++) {
for (j = x[i].length - 1; j >= 0; j--) {
document.writeln(x[i].charAt(j));
}
document.write("<br>")
}
try this code
var str = "SAMPLE STRING";
alert( str.split( "\\s+" ).reverse.join ( " " ) );
alert( str.split( " " ).reverse.join ( " " ) );
Since you want to print in a new line
var str = "SAMPLE STRING";
alert( str.split( " " ).reverse.join ( "\n" ) );
var str = "SAMPLE STRING";
var arr = str.split(" ");
arr.reverse();
str = arr.join(" ");
a = "SAMPLE STRING"
a.split(" ").reverse().forEach(function(element) {
document.writeln(element);
document.write('<br />')
})
OUTPUT:
STRING
SAMPLE
JSFiddle
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to implement this javascript code in jquery, please!
var html = '';
for (i = 0; i < 2; i++) {
for (var j = 0; j < 7; j++) {
var num = Math.floor(Math.random()*39)+1;
html += num + ' ';
}
html += '<br>';
}
document.getElementById('rows').innerHTML = html;
There is nothing wrong with the code you posted and there is not much difference between the straight javascript version and a version written in jQuery. The only different is the last line, where you select the rows object. In jQuery you get the object using $('#rows').
Here you go:
var html = '';
for (i = 0; i < 2; i++) {
for (var j = 0; j < 7; j++) {
var num = Math.floor(Math.random()*39)+1;
html += num + ' ';
}
html += '<br>';
}
$('#rows').html(html);