I have a simple question although i cannot manage to resolve this problem. Hope you can help. I need to make triangle using for loop and from this 4 exercises I don't know what to do with the third one. I haven't used Javascript before, so any help would be appreciated.
# # # # #
# # # #
# # # <----- here is triangle i need to make. Just in case
# #
#
var i;
var j;
for (i = 0; i <= 5; i++ )
{
document.write("</br>");
for ( j = 0; j < 6-i; j++ )
{
document.write( "  " );
}
for ( j = 6-i; j <= 5; j++ )
{
document.write( "*" );
}
}
This is code I wrote for D in photo.
And I'm sorry i did not add it at first.
for (let line = "*"; line.length < 8; line += "*")
console.log(line);
this question came in this book: http://eloquentjavascript.net
I don't know why there are so bad answers on google for this one.
function leftTriangle(rows){
let result = '';
for(let i=rows;i>0;i--){
if(i===rows) {
result += '*'.repeat(i) + '\n';
}else{
let empty = rows-i
result+= ' '.repeat(empty) + '*'.repeat(i)+ '\n'
}
}
return result;
}
console.log(leftTriangle(5))
I'm sure there are better solutions (simply left-padding with spaces comes to mind), but here's the quick and dirty one I created from your own solution.
for (var i = 0; i < 5; i++) {
for (var j = 0; j < i; j++) {
document.write(" ");
}
for (var j = 5; j > i; j--) {
document.write("#");
if (j > i + 1) document.write(" ");
}
document.write('<br/>')
}
https://js.do/code/diamondsinthesky
Something like this?
var rows = 5;
for (var i = rows; i--;) {
var columns = 0;
while (columns <= i) {
document.write('#');
columns++
}
document.write('<br />\n');
}
Thank you for your help. I did it. It was too obvious but somehow I couldn't find it. Thank you one more time. Here is how i did it.
for (i = 5; i > 0; i--) {
document.write("</br>");
for (j = 0; j < 6 - i; j++) {
document.write("  ");
}
for (j = 6 - i; j <= 5; j++) {
document.write("*");
}
}
var rows = 5;
for (var i = rows; i--;) {
var columns = 0;
while (columns <= i) {
document.write('#');
columns++
}
document.write('<br />\n');
}
You can also do this if you are looking for something different.
This code is for a triangle of 7 lines.
let size = 8;
let y = "#";
for (let x = 0; x < size; x++)
{
console.log(y);
y += "#";
}
// Second method
for (let i = 1; i < size;i++)
{
let me ="#".repeat(`${i}`)
console.log(me);
}
var size = 5;
for (var i = 0; i < size; i++) {
for (var j = 0; j <= i; j++) {
document.write("*");
}
document.write("<br />\n");
}
Related
I'm trying to find a shorter way to print a right-triangle and an isosceles triangle without using that many nested loops. Please help, here is my code so far, it works I just have trouble finding a shorter way to write it:
//right-triangle
let triangleStr = "";
let num = 5;
for (let i = 1; i <= num; i++) {
for (let j = 0; j < num - i; j++) {
triangleStr += " ";
}
for (let k = 0; k < i; k++) {
triangleStr += "*";
}
triangleStr += "\n";
}
console.log(triangleStr);
//isosceles-triangle
triangleString = "";
for (let i = 1; i <= num; i++) {
for (let j = 1; j <= num - i; j++) {
triangleString += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
triangleString += "*";
}
triangleString += "\n";
}
console.log(triangleString);
If you want to use only one loop, you can use repeat() method in ECMAScript6 or higher.
For Right triangle:
const line = 5;
let rightTriangle = "";
for (let l = 1; l <= line ; l++) {
const indent = line-l;
rightTriangle += `${" ".repeat(indent)}${"*".repeat(l)}${"\n"}`;
}
console.log(rightTriangle);
For Isosceles triangle:
const line = 5;
let isoscelesTriangle = "";
for (let l = 1; l <= line ; l++) {
const indent = line - l;
isoscelesTriangle += `${" ".repeat(indent)}${"*".repeat(2*l-1)}${"\n"}`;
}
console.log(isoscelesTriangle);
This question already has answers here:
Print an output in one line using console.log()
(13 answers)
Closed last year.
Instead of 1 index on each row, I want horizontal not vertical returns.
let n = [6];
function staircase() {
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n - i; j++) {
console.log("0");
}
for (let k = 1; k <= i; k++) {
console.log('#');
}
}
}
staircase();
But I need it to render like this. (This works using Java; it's the same logic, but renders differently.)
#
##
###
####
#####
######
console.log prints one line each therefore i put 2 variables to keep the empty string and hash string and after the loops logged the concatenation of the strings
let n = 6
function staircase() {
for (let i = 1; i <= n; i++) {
let emptyString = '';
let hashString = '';
for (let j = 1; j <= n - i; j++) {
emptyString += " ";
}
for (let k = 1; k <= i; k++) {
hashString += '#'
}
console.log(emptyString + hashString)
}
}
staircase();
const n = [6]
for (let i = 1; i <= n; i++) {
//
// this array stores the results;
//
const array = [];
//
for (let j = 1; j <= n - i; j++) {
array.push("0");
}
for (let k = 1; k <= i; k++) {
array.push('#');
}
// adding `toString`, does the magic
console.log(array.toString())
}
var s = '';
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 10; j++) {
s += '*'
}
s += '\n';
console.log(s)
}
maybe you just know what makes the asterisks on this loop so much?
even though I only want to repeat it for 5 lines, but why are there so many results?
[1]: https://i.stack.imgur.com/7zzOu.png
Your code is producing the following result:
Because not resetting the var s inside the outter iteration.
Instead, you may do:
for (let i = 0; i < 5; i++) {
let s = '';
for (let j = 0; j < 10; j++) {
s += '*';
}
s += '\n';
console.log(s);
}
which will produce the expected result.
I have a problem with my exercise. I have to draw something like this:
https://screenshots.firefox.com/3qaHB7dcr3n610hi/jsbin.com
And this is my code
var empty = "";
for(var i=0; i < 5; i++) {
empty += "*";
console.log(empty)
}
but with this code I can only make this:
*
**
***
****
*****
I have no idea how to reverse this loop to start it from top from 5 stars, i tried something like this:
var empty = "";
for (i = 5; i <= 0;i--) {
empty+="*";
console.log(empty);
}
But doesn't work. Anybody know how to do this? I will be grateful :)
Your approach builds the first part.
The second part can be accomplished using the function slice in descending direction.
var empty = "";
var i = 0;
// Build the first part
for (; i < 5; i++) {
empty += "*";
console.log(empty)
}
// Here i = 5, so this is the initialization for the following loop.
// Loop in descending direction using the function slice.
for (; i > 0; i--) {
console.log(empty.slice(0, i))
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Your condition was wrong. Check this.
for(let i = 1; i <= 5; i++) {
console.log('\"' + '*'.repeat(i) + '\"');
}
for(let i = 5; i > 0; i--) {
console.log('\"' + '*'.repeat(i) + '\"');
}
Instead of a for/loop you could use a while loop to change the direction:
let stars = 0;
let count = 0;
while (count < 9) {
if (count < 5) stars++;
if (count >= 5) stars--;
const line = Array(stars).fill('*').join('');
console.log(line);
count++;
}
Just in case you need a nested for loop. (As some exercises sometimes do)
var k = 0;
for(var i = 1; i < 12; i++){
var stx = "";
for(var j = k; j < i; j++){
stx += "*";
}
if(i > 5) k += 2
if(i == 6) continue
console.log(stx);
}
I want to make a reverse triangle using 'for' loop, which should look like this:
*****
****
***
**
*
This what I got:
*****
****
***
**
*
And this is my code:
function rightTriangle(n) {
for (var i = 0; i <= n; i++) {
for (var j = n - 1; j >= i; j--) {
document.write('*');
}
document.write('<br>');
}
}
rightTriangle(5);
Please help me out with this task, I would be so appreciated!
Add the below code to leave the intendation
for(var k=0; k<i; k++){
document.write(" ");
}
function rightTriangle (n) {
for (var i = 0; i <= n; i++) {
for(var k=0; k<i; k++){
document.write(" ");
}
for (var j = n-1; j >= i; j--) {
document.write('*');
}
document.write('<br>');
}
}
rightTriangle(5);
html{
font-family:monospace;
}
function rightTriangle(n) {
for (var i = 0; i <= n; i++) {
for (var j = 0; j <= n; j++) {
if(j>=i) document.write('*');
else document.write('  ');
}
document.write('<br>');
}
}
rightTriangle(5);
Algo
Create n*n matrix with 2 values, " " and "*"
In html " " has no effect as extra spaces are trimmed. You will have to use , unicode value for single space.
Now loop and add necessary parts to string.
Since you need reverse triangle, spaces will come first. You can loop in ascending fashion with value of i denoting spaces and n-i denoting "*"
function rightTriangle(n) {
var html = "";
for (var i = 0; i < n; i++) {
for(var j = 0; j< i; j++){
html += " ";
}
for(var k = 0; k< n-i; k++){
html += "*"
}
html +="<br/>"
}
document.querySelector('.content').innerHTML = html
}
rightTriangle(5);
.content {
font-family: monospace;
}
<div class="content"></div>
string.repeat
function rightTriangle(n) {
var html = "";
for (var i = 0; i < n; i++) {
html += " ".repeat(i) + "*".repeat(n - i) + "</br>"
}
document.querySelector('.content').innerHTML = html
}
rightTriangle(5);
.content {
font-family: monospace;
}
<div class="content"></div>
Also note that using document.write and < br/>" are considered bad practice.
Here is the most simple solution:
const prompt = require("prompt-sync")();
let n = parseInt(prompt("Enter the number of rows: "));
let string = "";
function printPyramid(row) {
for (let i = 0; i < n; i++) {
string += " ".repeat(i) + "*".repeat(n - i);
string += "\n";
}
}
printPyramid(n);
process.stdout.write(string);