I am trying to display the pyramid in the console part using only javascript except document.write("")
*
* *
* * *
* * * *
* * * * *
I don't want in the above format.
I want in the below format
*
* *
* * *
* * * *
* * * * *
My code is
function getPyramid(param) {
for (var i = 1; i <= param; i++) {
var output = "";
for (var j = 1; j <= i; j++) {
output += j + " ";
}
console.log(output);
output = "";
}
}
<button onclick="getPyramid('10')">
function renderPyramid(n) {
for (var i = 0; i < n; i++) {
var str = '';
for (var j = 1; j < n-i; j++) {
str = str + ' ';
}
for (var k = 1; k <= (2*i+1); k++) {
str = str + '*';
}
console.log(str);
}
}
renderPyramid(5)
my solution
function pyramid(n) {
// generate base of pyramid, aka longest possible string
let limit = n+n-1;
let hashesToPrint = 1; // number of hashes to print
for (let i=0; i<n; i++) {
// get length of spaces we need on each side
let difference = (limit - hashesToPrint) / 2;
// generate spaces string
let spaces = ' '.repeat(difference);
// create pounds string
let pounds = '#'.repeat(hashesToPrint);
// append spaces on either side of our pound string
let newString = spaces + pounds + spaces
console.log(newString)
// increment our counter by two
hashesToPrint += 2
}
}
pyramid(3)
Resolved :I got logic for this question
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<button onclick="getPyramid('10')">
Get Pyramid</button>
<script>
function getPyramid(param)
{
for(var i=0;i<param;i++) {
var output="";
for(var j=0;j<param-i;j++) {
output+=" ";
// console.log(" ");
}
for(var k=0;k<=i;k++) {
// console.log("* ");
output += "* ";
}
// output += "";
console.log(output);
}
}
</script>
</body>
</html>
<script type="text/javascript">
var i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
document.write("\t*");
}
document.write("<br>");
}
for (i = 5; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
document.write("\t*");
}
//document.write("\t");
document.write("<br>");
}
for (i = 5; i >= 0; i--)
{
for (j = 0; j < i; j++)
{
document.write("*");
}
for (k = i; k <= 5; k++)
{
document.write("*");
}
document.write("*");
}
</script>
Related
[enter image description here][1]
List item
let x = 4;
let text = "";
for(let i = 1; i <= x; i++) {
for(let j = 1; j <= x - i; j++) {
text += " ";
}
for(let k = 0; k < 2 * i - 1; k++) {
text += "*";
}
text +="\n";
}
console.log(text);
---Output I want is like this. I'm new to this. Thank you in advance
[1]: https://i.stack.imgur.com/AJvaZ.png
Small change:
let x = 4;
let text = "";
for(let i = 1; i <= x; i++) {
for(let j = 1; j <= x - i; j++) {
text += " ";
}
text += "*";
for(let k = 0; k + 1 < i; k++) {
text += " *";
}
text +="\n";
}
console.log(text);
I'm trying to print a pyramid that has odd numbers for edges. For example output should be:
.......5
...3,4,3
1,2,3,2,1
(dots are here just to show formating)
I managed to write:
function p(n){
let string = "";
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n - i; j++) {
string += " " ;
}
for (let k = 1; k <= 2*i-1; k++) {
string += n-k +1;
}
string += "\n";
}
console.log(string);
}
p(5);
But I'm not sure how to continue to get wanted result
You can try it :
function p(n){
let string = ""
for (let i = 0; i <= n/2; i++) {
let k = n - 2 * i
for (let j =0; j < Math.floor(n/2)*2 + 1; j++) {
if(j < Math.floor(n/2) - i) {
string += " ";
}
if( j >= Math.floor(n/2) - i && j <= Math.floor(n/2) + i) {
string += k;
j < Math.floor(n/2) ? k++ : k--
}
if(j > Math.floor(n/2) + i ) {
string += " ";
}
}
string += "\n";
}
console.log(string);
}
p(7);
I want to print the following star pattern using JavaScript:
*****
***
*
***
*****
And I am trying this code, where I am getting off a triangle:
for (i = 3; i >= 1; i--) {
/* Printing spaces */
for (j = 0; j <= 3 - i; j++) {
document.write(" ");
}
/* Printing stars */
k = 0;
while (k != (2 * i - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
}
try this code
after i==1 create new pattern inside it
for (i = 3; i >= 1; i--) {
/* Printing spaces */
for (j = 0; j <= 3 - i; j++) {
document.write(" ");
}
/* Printing stars */
k = 0;
while (k != (2 * i - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
if (i == 1) {
for (t = 2; t <= 3; t++) {
for (j = 0; j <= 3 - t; j++) {
document.write(" ");
}
/* Printing stars */
k = 0;
while (k != (2 * t - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
}
}
}
Try this code:-
In this we use outer loop and inner loop.
var i, j;
//outer loop
for(i=1; i <= 5; i++)
{
//inner loop
for(j=1; j<=i; j++)
{
document.write('*');
}
document.write('<br/>');
}
Iteration:-
i j(Repeat)
1 1
2 2
3 3
4 4
5 5
const n = 5
for (var i = 0; i < n; i++) {
var star = '';
for (var j = 0; j < n - i; j++) {
star += ' ';
}
for (var k = 0; k < i; k++) {
star += '* ';
}
console.log(star);
}
for (var i = 0; i < n; i++) {
var star = '';
for (var b = 0; b < i; b++) {
star += ' ';
}
for (var a = 0; a < n - i; a++) {
star += '* ';
}
console.log(star);
}
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);
So I am trying to model Gram-Schmidt for any size N×N matrix, and I have officially hit a roadblock I can't get past. I know it's a matter of looping this correctly, but I can't figure out what the problem is. Remember I do not want to just pass in a 3×3 matrix, but any size N×N.
The course notes QR Decomposition with Gram-Schmidt explains exactly what I want to do. Very simple calculation by the way. In the course notes ||u|| means that it is the sum of the square of the elements, so sqrt(x12 + x22 + x32 + .... + xn2).
The multiplication symbol is actually the dot product.
The code I wrote so far is listed below. What is wrong with it?
function qrProjection(arr) {
var qProjected = [];
var tempArray = [];
var aTemp = arr;
var uTemp = new Array(arr.length);
var uSquareSqrt = new Array(arr.length);
var eTemp = [];
var sum = 0;
var sumOfSquares = 0;
var breakCondition = 0;
var secondBreakCondition = 0;
var iterationCounter = 0;
//Build uTemp Array
for (i = 0; i < arr.length; i++) {
uTemp[i] = new Array(arr[i].length);
}
for (i = 0; i < arr.length; i++) {
eTemp[i] = new Array(arr[i].length);
}
uTemp[0] = aTemp[0];
for (j = 0; j <= arr.length; j++) {
for (l = 0; l < arr[j].length; l++) {
if (breakCondition == 1) break;
sumOfSquares = Math.pow(uTemp[j][l], 2) + sumOfSquares;
}
if (breakCondition == 0) {
uSquareSqrt[j] = Math.sqrt(sumOfSquares);
sumOfSquares = 0;
}
for (i = 0; i < arr[j].length; i++) {
if (breakCondition == 1) break;
eTemp[j][i] = (1 / (uSquareSqrt[j])) * (uTemp[j][i]);
}
breakCondition = 1;
if (iterationCounter == 0) {
for (m = 0; m < arr[j].length; m++) {
matrixDotProduct = aTemp[j + 1][m] * eTemp[j][m] + matrixDotProduct;
}
}
else {
for (m = 0; m < arr[j].length; m++) {
for (s = 0; s <= iterationCounter; s++) {
matrixDotProduct = aTemp[j + 1][s] * eTemp[m][s] + matrixDotProduct;
}
for (t = 0; t < arr[j].length; t++) {
uTemp[j + 1][t] = aTemp[j + 1][t] - eTemp[j][t] * matrixDotProduct;
}
}
}
if (iterationCounter == 0) {
for (m = 0; m < arr[j].length; m++) {
uTemp[j + 1][m] = aTemp[j + 1][m] - eTemp[j][m] * matrixDotProduct;
}
}
matrixDotProduct = 0;
for (l = 0; l < arr[j].length; l++) {
sumOfSquares = Math.pow(uTemp[j + 1][l], 2) + sumOfSquares;
}
uSquareSqrt[j + 1] = Math.sqrt(sumOfSquares);
sumOfSquares = 0;
for (i = 0; i < arr[j].length; i++) {
eTemp[j + 1][i] = (1 / (uSquareSqrt[j + 1])) * (uTemp[j + 1][i]);
}
iterationCounter++;
}
qProjected = eTemp;
return qProjected;
}
I must apologize that instead of tweaking your code, I wrote my own from scratch:
/* Main function of interest */
// Each entry of a matrix object represents a column
function gramSchmidt(matrixA, n) {
var totalVectors = matrixA.length;
for (var i = 0; i < totalVectors; i++) {
var tempVector = matrixA[i];
for (var j = 0; j < i; j++) {
var dotProd = dot(matrixA[i], matrixA[j], n);
var toSubtract = multiply(dotProd, matrixA[j], n);
tempVector = subtract(tempVector, toSubtract, n);
}
var nrm = norm(tempVector, n);
matrixA[i] = multiply(1 / nrm, tempVector, n);
}
}
/*
* Example usage:
* var myMatrix = [[1,0,0],[2,3,0],[5,4,7]];
* gramSchmidt(myMatrix, 3);
* ==> myMatrix now equals [[1,0,0],[0,1,0],[0,0,1]]
* 3 here equals the number of dimensions per vector
*/
/* Simple vector arithmetic */
function subtract(vectorX, vectorY, n) {
var result = new Array(n);
for (var i = 0; i < n; i++)
result[i] = vectorX[i] - vectorY[i];
return result;
}
function multiply(scalarC, vectorX, n) {
var result = new Array(n);
for (var i = 0; i < n; i++)
result[i] = scalarC * vectorX[i];
return result;
}
function dot(vectorX, vectorY, n) {
var sum = 0;
for (var i = 0; i < n; i++)
sum += vectorX[i] * vectorY[i];
return sum;
}
function norm(vectorX, n) {
return Math.sqrt(dot(vectorX, vectorX, n));
}
Note that the algorithm above computes the Gram-Schmidt orthogonalization, which is the matrix [e1 | e2 | ... | en], not the QR factorization!