i have this but it can not resolve the problem:
for (let i = 1; i <= 5; i++) {
let line = '';
for (let j = 1; j <= 5 - i; j++) {
line += ' ';
}
for (let k = i; k <= 2 * i - 1; k++) {
line += `${i}` + ' ';
}
for (let l = 2 * i - 2; l >= i; l--) {
line += `${l}` + ' ';
}
console.log(line);
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is the output I want:
1
2 2
3 4 3
4 5 5 4
5 6 7 6 5
It forms an equilateral triangle.
linenumber = 5
for (let i = 1; i <= linenumber; i++) {
let line = ''
for (let space = 1; space <= linenumber-i; space++){
line += ' ';
}
for (let j = 1; j <= i; j++) {
let numberadd = Math.abs(j-1);
let numberadd2 = Math.abs(i-j);
if (numberadd > numberadd2){
numberadd = numberadd2
}
line += i+numberadd + ' ';
}
console.log(line)
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is a very simple make a pyramid with an extra sauce. For every cell, you need to its value by closest distance relative to the 'walls'.
Related
I want to create a source that can input odd numbers into the user and print out diamond-shaped * accordingly
example)
enter an odd number : 5
*
***
*****
***
*
If (example), input = 5, you just must learn, how to count:
(spaces)(stars)
2 1
1 3
0 5
1 3
2 1
let input = 7; // Number( prompt("Enter any odd integer") );
let space = " ";
/*****/
let breakPoint = Math.floor(input / 2);
let str = "", iter = 0, i = 0;
while( iter < input ) {
str += (
space.repeat( (breakPoint - i) ) +
"*".repeat( 2 * i + 1 ) +
"<br>"
);
(iter < breakPoint) ? (i++) : (i--);
iter++;
}
document.body.innerHTML = str;
body { font-family: 'Lucida Console'; } /* Monospaced font required */
One loop might be confusing. I came to it after trying two separate loops:
let input = 7;
let space = " ";
/***/
let breakPoint = Math.ceil(input / 2);
let str = "";
for( let i = 0; i < breakPoint - 1; i++ ) {
str += (
space.repeat( (breakPoint - i) ) +
"*".repeat( 2 * i + 1 ) +
"\n"
);
}
for( let i = breakPoint - 1; i >= 0; i-- ) {
str += (
space.repeat( (breakPoint - i) ) +
"*".repeat( 2 * i + 1 ) +
"\n"
);
}
console.log( str );
Or, KISS (Keep It Simple Stupid):
let input = 7;
let space = " ";
/***/
let breakPoint = Math.floor(input / 2); // 3
let str = "";
let spaces = breakPoint; // (3)
let stars = 1;
for( let i = 0; i < breakPoint; i++ ) {
str += (
space.repeat(spaces) +
"*".repeat(stars) +
"\n"
);
spaces -= 1;
stars += 2;
}
for( let i = breakPoint; i >= 0; i-- ) {
str += (
space.repeat(spaces) +
"*".repeat(stars) +
"\n"
);
spaces += 1;
stars -= 2;
}
console.log( str );
"use strict";
function diamondPattern() {
let string = "";
for (let i = 1; i <= 5; i++) {
for (let j = 5; j > i; j--) {
string += " ";
}
for (let k = 0; k < i * 2 - 1; k++) {
string += "*";
}
string += "\n";
}
for (let i = 1; i <= 4; i++) {
for (let j = 0; j < i; j++) {
string += " ";
}
for (let k = (5 - i) * 2 - 1; k > 0; k--) {
string += "*";
}
string += "\n";
}
return string;
}
console.log(diamondPattern());
I'm trying to solve task with the usage of nested loops. My code below
var n = 5;
var lineOfStars = '';
for (var i = 1; i <= 10; i++) {
for (var j = 1; j <= n; j++) {
if (j <= i) { // 1 <= 5 - 1 - 1 (3)
lineOfStars += ' * ';
} else {
lineOfStars += ' ' + j + ' ';
}
}
lineOfStars += '\n';
}
console.log(lineOfStars);
The result that im looking for is (i want to do it only with nested loops):
* 2 3 4 5
* * 3 4 5
* * * 4 5
* * * * 5
* * * * *
* * * * *
* * * * 5
* * * 4 5
* * 3 4 5
* 2 3 4 5
The code that ive shown does only half of the job. I need help. Thanks in advance
This is closer to what you are wanting, still working on it.
var n = 5;
var lineOfStars = '';
let i, j
for (i = 1; i <= 5; i++) {
for (j = 1; j <= n; j++) {
if (j <= i) {
lineOfStars += ' * ';
} else {
lineOfStars += ' ' + j + ' ';
}
}
lineOfStars += '\n';
}
console.log(lineOfStars)
for (j = 5; j <= 5; j--) {
if (j == 0) break;
for (i = 5; i <= n; i--) {
if (0 == 0) break;
if (i <= j) {
lineOfStars += ' ' + i + ' ';
} else {
lineOfStars += ' * ';
}
}
lineOfStars += '\n';
}
let rev = lineOfStars.split('\n').reverse()
for (let z = 1; z <= rev.length - 1; z++) {
if (rev[z].length != 0) {
console.log(rev[z])
lineOfStars += rev[z]
lineOfStars += '\n';
}
}
console.log(lineOfStars)
I have this loop problem, I really don't understand why my code doesn't work, I've even draw this on paper and for my logic it looks good but it's not working, please help me.
function drawTree(h) {
for(var i=0; i<=h; i++){
var star = '';
for(var k=0; k<=1; k++){
star += " ";
};
for(var j=0; j<=i; j++) {
star += "*";
};
};
console.log(star);
};
drawTree(5);
See comments in the code for changes.
function drawTree(h) {
for(var i=0; i<=h; i++){
var star = '';
//Changed to start high then decrease
for(var k = 1; k <= h - i; k++){
//shortened to one space
star += " ";
};
for(var j=0; j<=i; j++) {
//Added space so there is an odd number
//of symbols and the star above fits
//the space
star += " *";
};
//Moved into the loop
console.log(star);
};
};
drawTree(5);
Note that the code can be substantially shortened using String.prototype.repeat:
function drawTree(h) {
for (var i = 0; i <= h; i++){
console.log(" ".repeat(h - i) + " *".repeat(i + 1));
};
};
drawTree(5);
Also note that your example produces a base line with six stars for a call of drawTree(5). I am unsure whether that is intended. The code above reproduces said behavior, editing it to show a line less should not be too hard.
function drawTree(h) {
for (var i = 0; i < h; i++) {
var star = '';
var space = (h - i);
if (i == 0) {
star += ' '.repeat(space + 1) + '\n';
}
star += ' '.repeat(space + 1);
var zero = 2 * i + 1;
star += '*'.repeat(zero);
console.log(star);
}
}
drawTree(5);
You’re re-setting it each line, but printing it only at the end.
Move console.log(star); to the end of the first loop.
Just for fun:
const tree = ({height: h = 5, stumpHeight: sh = 2, branchChar: bc = '*', emptyChar: ec = ' ', stumpChar: sc = '#'} = {}) => [
... Array .from ({length: h}, (_, n) => ec .repeat (h - n - 1) + bc.repeat (2 * n + 1) + ec .repeat (h - n - 1)),
... Array .from ({length: sh}, () => ec .repeat (h - 1) + sc + ec .repeat (h - 1)),
] .join ('\n')
console .log (tree ())
console .log (tree ({height: 6, emptyChar: '_'}))
console .log (tree ({height: 12, stumpHeight: 3, stumpChar: '#'}))
console .log (tree ({branchChar: '#'}))
.as-console-wrapper {max-height: 100% !important; top: 0}
Because, you know, that's how all those JS 101 instructors expect it to be coded.
function drawTree(n) {
let tag = "#";
for (let i = 0, j = "#"; i < n; i++) {
console.log(tag);
tag = tag + j;
}
}
drawTree(10);
You have to place the console.log statement inside first for loop. That should give a half tree.
If you want correct tree, then pre-pend spaces before star.
Calculate the center of the number of rows and add spaces before it.
Hope this not an assignment problem.
function drawTree(h) {
for(var i=0; i<=h; i++){
var star = '';
for(var k=0; k<=1; k++){
star += " ";
};
for(var j=0; j<=i; j++) {
star += "*";
};
console.log(star);
};
};
drawTree(5);
How could I create Pyramide of Stars that increase every row by 2 like that:
*
* * *
* * * * *
* * * * * * *
My currently code:
for (var x = 0; x < 5; x++) {
for (var y = 0; y <= x; y = y + 1) {
document.write(" * ");
}
document.write("<br>");
}
It's possible just to increment in your loop by 2.
for(var i = 1; i < 20; i += 2) {
console.log( Array(i).fill('*').join(' ') );
}
Otherwise just multiply inside your loop
for(var i = 0; i < 10; i++) {
console.log( Array(i*2 + 1).fill('*').join(' ') );
}
You may also need to polyfill Array.fill depending on your target.
Other answers recreate the entire row each time. This solution just extends the row each time to have another star.
function pyramid(n) {
let result = '', str = '', add = '*';
for (var i = 0; i < n; i++) {
str += add;
add = ' *';
if (!(i % 2)) result += str + '\n';
}
return result;
}
console.log(pyramid(5));
You can do like this.
function generate() {
var totalNumberofRows = 5;
var output="";
for (var i = 1; i <= totalNumberofRows; i++) {
for (var j = 1; j <= i; j++) {
if(j==1)
output+="*";
else
output+=" "+ "*" + " "+ "*";
}
console.log(output);
output="";
}
}
generate()
Hope so this is also beneficial for you....
$(document).ready(function () {
var NumberofRows = 5,arr;
for (var i = 1; i <= NumberofRows; i++) {
pyramid = [];
for (var j = 1; j <= i; j++) {
pyramid.push('*');
}
console.log(pyramid.join(" ") + "\n");
}
});
``
Here is my javascript code
for (var i = 1; i <= _MAXPAGECOUNT - 2; i++) {
e = document.getElementsByName("q" + i + "[]");
for (var j = 0; j <= e.length - 1; j++) {
if (e[j].checked) {
result = result + "," + i + ":" + e[j].value;
// break;
}
}}
The problem is this, it shows result like this 1:2,1:3,1:4,2:3,2:4,2:5
here in code i means question number and j means answer number, but I want to result as like this 1:2,3,4 ; 2:3,4,5
Try this
for (var i = 1; i <= _MAXPAGECOUNT - 2; i++) {
result = result+i+":";
e = document.getElementsByName("q" + i + "[]");
for (var j = 0; j <= e.length - 1; j++) {
if (e[j].checked) {
result = result + e[j].value;
// break;
}
}
if(i<_MAXPAGECOUNT - 2)
{
result = result+" ; ";
}
}