I have following java script code segments to print less than 5 numbers between 1-10.
<script type="text/javascript">
for (let i = 1; i < 10; i++) {
if (i < 5) {
document.write('The Number is' + i + '<br>');
}
}
</script>
Then I need print this using while loop. how can I do this?
Try this:
let i = 1;
while (i < 10) {
if(i < 5) {
document.write('The Number is ' + i + '<br>');
}
i++;
}
By using while loop, write a program to print the numbers from 1 to 10.
let i = 1;
while (i <= 10) { //while (i < 11) {
console.log(i);
i++;
}
Output :
1
2
3
4
5
6
7
8
9
10
Related
I am trying to implement the while and do while loop without using an actual array to print an list of number. Here is my code and the output I want.
My code
console.log("While Loop")
let j = 0;
while(j < 5){
j += 1;
console.log (j);
}
console.log("Do While Loop")
var i = 0; //set varible i = 0
do {
i += 1; //return i + 1 to i
console.log (i);
}
while (i < 5); //where i suppose to less than 5
Actual Output I want:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
There is two things here:
You need an inner loop to create the countdown of numbers.
If you are only using JavaScript, you need to form a string with your output and then use console.log() to print. This is because console.log() will append a newline.
The example below will print the first half of the triangle.
console.log("While Loop")
let j = 1;
while (j <= 5) {
let k = 1;
let str = "";
while (k <= j) {
str += k + " ";
k++;
}
j += 1;
console.log(str);
}
If you are using Node.js you can use process.stdout.write to print to the same line and then explicitly write the newline character when needed.
console.log("While Loop")
let j = 1;
while (j <= 5) {
let k = 1;
while (k <= j) {
process.stdout.write(k + " ");
k++;
}
j += 1;
process.stdout.write("\n");
}
This is more of a question on how I should approach my problem, and not what code to use. this is the general framework/simplification of my code. I can provide the actual code if needed. question is below, it is similar to the last question I asked as it is part of the same system that isn't working:
for (i = 0; i < 10; i++) {
var amt = 0;
function checktime() {
console.log(amt + " / " + i);
amt++;
if (amt <= 5) {
setTimeout(checktime, 3000);
}
}
checktime();
}
I want it to have all the set timeouts running at once, for each i. the console results in
0 / 0
0 / 1
0 / 2
...
0 / 8
0 / 9
1 / 10
2 / 10
3 / 10
4 / 10
...
13 / 10
14 / 10
I'd like it to look like this:
0/0
0/1
0/2
0/3
...
0/9
1/0
1/1
...
5/9
5/10
sorry for the long question, but how would I go about doing this?
Have a function inside checktime that runs a loop. Set up your variables, and then pass the count variable into the inner function again with your setTimout.
function checktime() {
// Set the amt variable to zero
let amt = 0;
// Set count to zero if it doesn't exist
function loop(count = 0) {
// Log the new data
console.log(`${amt}/${count}`);
// Increase the count
++count;
// If count hits 10 reset the count
// and increase the amt variable
if (count === 10) {
count = 0;
++amt;
}
// Call the loop function again and pass in the new count
// as a parameter
setTimeout(loop, 1000, count);
}
loop();
}
checktime();
There are 2 ways you can do this.
Using nested loops like this
for(i = 0; i< 10; i++){
for(j = 0; j < 5; j++) {
console.log(i + " / " + j)
setTimeout(3000)
}
}
Using recursion like this:
function setTimeout_recurse(amt, i) {
if (amt < 5) {
console.log(i + " / " + amt)
amt ++;
setTimeout(3000)
setTimeout_recurse(amt, i)
}
}
for(i = 0; i< 10; i++){
var amt = 0;
setTimeout_recurse(amt, i);
}
You'll notice that the base is now i not amt which makes more sense to be the base.
I'm trying to display numbers in the following format can you please tell me what is wrong?
Can not use div. As i'm printing this in the console.
I for n = 5:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
I have tried the following code:
n = 10
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= i; j++) {
console.log("0" + j + " ");
}
console.log("<br />");
}
n = 10
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= i; j++) {
console.log("0" + j + " ");
}
console.log("<br />");
}
But it displays something like this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
Here is the code for your Task. It takes numbers from 1 to declared. Also puts some space at the beginning of each row. You need to declare a string at the beginning of each row and then add all elements to it. When all loops are done, you can print it in console with console.log.
var n = 10 // Declaring number of rows
for (var i = 1; i <= n; i++) {
var row = ''; //declaring text variable for current row
for (var x = n - i; x >= 1; x--) {
row += ' '; //adding spaces in begining of the row
}
for (var j = 1; j <= i; j++) {
row += ' ' + j; // numbers which increase up to middle
}
for (var k = i - 1; k >= 1; k--) {
row += ' ' + k; // adding rest of the numbers to the row
}
console.log(row); //displaying whole row
}
for the algorithm point of view you have the forward loop but you missed the downward loop
adding it more the indentation from your first version of the question :
var n = 10
for (var i = 1; i <= n; i++) {
for (var j = 0; j < n - i; j++) {
document.write(" ");
}
for (var j = 1; j <= i; j++) {
document.write(" " + j + " ");
}
for (var j = i - 1; j >= 1; j--) {
document.write(" " + j + " ");
}
document.write("<br />");
}
P.S. It is the very first time I write code in javascript ;-)
I need to display a number range between 1 and 30 in the console, but in a specific way. While I've figured out how to get the range using a for loop, I have not figured out how to display the numbers in the console like in the image shown below where each * represents a number 1-30. I need numbers 1-30 to be displayed 7 rows across, and 5 rows down, without using HTML tables.
example
My code, to display the number range, is as follows:
for (var i = 1; i <= 5; i++)
{
var output = " ";
for (var j = 0; j <= 7; j += 1)
{
output += "*" + "\t";
}
console.log(output);
}
So far I have tried adding a third nested for loop, but there will be 5 iterations of 1 - 30 displayed 7 times. I have also tried using an array of 1-30, a while loop, an if statement, and have tried adding or multiplying variables with similar results.
I can't help but feel like I am approaching this the wrong way. I am considering using an array and having the inner for loop display each index of the array, but I am not sure if JavaScript has the ability to move to the next index after printing the previous index (1 then 2 then 3, etc) in the way in which I need it to as shown in the image.
This code works
var output="";var count = 0;
for (var i = 1; i <= 5; i++)
{
//output = " ";
for (var j = 0; j < 7; j += 1)
{
output +=(count+1)+"\t";count ++;
}output+="\n";
//document.write(output);
}
console.log(output);
Output:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
Problem in Question:
Question States -Number range between 1 and 30 in the console but specified order of
matrix is 7*5 which is equal to 35 not 30 which was wrongly mentioned
in the questiion.
Screenshot
As indicated by #torazaburo the OP is looking to populate numbers as opposed to [ * ] . Though SaiKiranUppu had a satisfactory answer and should be awarded the upvote, I wanted to offer another solution:
JS:
function matrix(r, c) {
var n = '';
var x = 1;
for(var i = 1; i <= r; i++) {
for(var j = 0; j < c; j++) {
n += x + '\t';
x++;
}
n += '\n';
}
console.log(n);
};
matrix(5, 7);
You can just do this. I do it only for your..
var lastJ = 1;
for (var i = 1; i <= 6; i++) {
var output = " ";
var j = lastJ ;
var totalLoop = j+4;
for (j = lastJ; j <= totalLoop; j++) {
output += j + "\t";
}
console.log(output);
lastJ = j;
}
output is like this
console output in image
If you want calendar like output use this
var lastJ = 1;
for (var i = 1; i <= 5; i++) {
var output = " ";
var j = lastJ ;
var totalLoop = j+6;
for (j = lastJ;j <= totalLoop; j++) {
if(j<32){
output += j + "\t";
}else{
output += 0 + "\t";
}
}
console.log(output);
lastJ = j;
}
output is like this
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 0 0 0 0
First of all, Welcome to SO!
A newline element after every outer loop should do the trick. Something like this.
for (var i = 1; i <= 5; i++)
{
var output = "";
for (var j = 1; j <= 7; j += 1)
{
output += "*" + "\t";
}
output += "\n";
console.log(output);
}
Edit: Realised OP wanted numbers and not *s. A simple modification to the code makes this unbelievably easy.
var output = "";
for (var i = 1; i <= 5; i++)
{
for (var j = 1; j <= 7; j += 1)
{
output += i*j + "\t";
}
output += "\n";
}
console.log(output);
}
Output - Chrome Developer Tools Screenshot
I have an assignment for school that I seem to be stuck on.
Write a script that prints the prime numbers between 1 and 999 in a table that consists >of 10 columns. Use document.write() statements to create the table elements and a >counter variable to create the table so that it consists of 10 columns. The counter >variable should start with an initial value of 0 and be incremented by one each time your >code identifies a prime number and prints it in a table cell. Once the counter variable >reaches a value of 10 (meaning that 10 cells have been added to the current row), print >to start a new row and reset the variable to 0.
I cannot figure out where my error or errors are with the table row and counter. Also, I might have placed the document.write() methods in the wrong places. Currently, the table only displays vertically.
Here is what I have:
<script>
function primeNumbers(num) {
document.write('<table>');
if (num < 2)
return false;
for (var i = 2; i < num; i++) {
if (num % i == 0)
return false;
}
return true;
}
document.write('<tr>');
var counter = 0;
for (var i = 0; i < 999; i++) {
if (primeNumbers(i)) {
if (counter % 10 == 0) {
document.write('</tr><tr>');
counter = 0;
}
counter++;
document.write('<td>' + i + '</td>');
}
}
document.write('</table>');
</script>
Please, push me in the right direction and don't give me the answer. I want to see if I can figure this out.
Thanks!
There were some errors in your code:
The <table> opener was inside the function, so actually it was opening 1000 times.
The counter is reset, so you don't need to use modulus (%), just compare to 10.
A </tr> was missing in the end
Your function primeNumbers is misnamed, the best would be call isPrime
Here's the code:
function isPrime(num) {
if (num < 2) return false;
for (var i = 2; i < num; i++) {
if (num % i == 0) return false;
}
return true;
}
document.write("<table><tr>");
var counter = 0;
for (var i = 0; i < 999; i++) {
if (isPrime(i)) {
if (counter == 10) {
document.write('</tr><tr>');
counter = 0;
}
counter++;
document.write('<td>' + i + '</td>');
}
}
document.write('</tr></table>');
Here's the fiddle working (I can't use document.write here, but you get the idea) http://jsfiddle.net/bortao/D2bzk/
Try this: http://jsfiddle.net/ezn7f/
<script>
function primeNumbers(num) {
if (num < 2)
return false;
for (var i = 2; i < num; i++) {
if (num % i == 0)
return false;
}
return true;
}
document.write('<table><tr>');
var counter = 0;
for (var i = 0; i < 999; i++) {
if (primeNumbers(i)) {
if (counter % 10 == 0) {
document.write('</tr><tr>');
counter = 0;
}
counter++;
document.write('<td>' + i + '</td>');
}
}
document.write('</table>');
</script>
I wonder if this is what you are looking for though. I didn't change much in your code.