My goal is to construct a pyramid of stars. The first row will have one star, the second row will have two stars with equal spacing, and this pattern will continue for the third and fourth rows as well


    [insert 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);

---I am a beginner and I would like the output to be in this format. Thank you very much! [1]: https://i.sstatic.net/AJvaZ.png

Answer №1

Slight modification:

let num = 4;
let output = "";
for(let a = 1; a <= num; a++) {
    for(let b = 1; b <= num - a; b++) {
        output += " ";
    }
    output += "*";
    for(let c = 0; c + 1 < a; c++) {
        output += " *";
    }
    output +="\n";
}
console.log(output);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AJAX parameters for sending xmlhttp requests

I'm currently working on an AJAX function that dynamically changes the color of a specific button. However, I have only been able to achieve this in a static manner by manually inputting the values sent to the corresponding php script. My goal is to b ...

Convert a JSON string on the server side to a formatted format using jQuery on the client side

This is the fiddle: http://jsfiddle.net/zzk3rgrm/3/ Here is the JSON string retrieved from the server: JS var myjsonstring = [{"Key":"item1","Value":"problem statement 1"},{"Key":"item1","Value":"problem statement 2 bigstatement bigstat ...

Identify when an ajax request is completed in order to trigger a subsequent ajax request

Within my project, I am faced with a challenge involving select option groups that dynamically load ajax data based on previous selections. The issue arises when I attempt to replicate this functionality for another select option group. Here is the scenar ...

Incorporate pictures from the popup onto the main page

I have developed a basic PHP image editor script where users can select images from galleries and merge them together. However, I am facing an issue: The galleries open in a popup while the editor area is on the parent page. I am looking for a JavaScript ...

Is there a way to transfer JSON data from a JSP page to a Spring REST controller efficiently?

Attempting to send data from a jsp page containing datetime, zoneid, checked checkboxes with values, and unchecked checkboxes with null values. Despite sending all this as json to my spring rest controller, I noticed in debug mode that the controller only ...

Why does this switch case statement fail to effectively replace the elements of the text in order to unravel this JavaScript problem?

Question I need help converting special characters to HTML entities in a string: &, <, >, " (double quote), and ' (apostrophe). My Code function convertHTML(str) { let tempArr = ['a']; let newArr = []; let regex ...

Issue with Cascading Dropdowns in jQuery

I am currently experiencing issues with a piece of code on my website (also accessible via this link). The code consists of 2 select fields that should display different data based on the selection made. However, I have identified 2 bugs within the code. ...

Is there a way to pre-format a phone number field using jQuery?

Looking to create a phone number text field with a pre-formatted 10-digit structure, like this: |( ) - | allowing users to input numbers and have them fill in automatically: |(804) 479-1832| I came across a helpful script for formatting input d ...

The assigned javascript/CSS style is not being applied to every button

In an attempt to customize a list, I have written the following code: <script> for (n = 0; n < 3; n++) { var node = document.createElement("li"); var btn = document.createElement("button"); $("button").css({ 'background-c ...

Unable to obtain the width of images on Chrome browser

I've been developing a basic slider that moves images from right to left. It's functioning properly on Firefox, but I'm encountering an issue with Chrome when trying to obtain the width of the images. $(window).load(function() { // Loa ...

Deleting a ZIP file after sending the response in Express: A step-by-step guide

Is there any alternative solution to deleting the zip folder after sending a response in my code? Below is the code for a GET request: exports.Download = async (req, res) => { try { var zp = new admz(); zp.addLocalFolder(`./downl ...

What is the best way to verify and handle an undefined array in order to display an error message?

Currently working on a ternary statement to display the certification of a movie (e.g. PG, R...). If the array length is zero or undefined, I'm aiming to output an error message stating "No Certification Available For This Movie". While I have succes ...

Angular ngClass and ngIf directives failing to update upon alterations

In my current Angular project, I am working on a functionality where I need to dynamically change a class based on a variable without having to refresh the page. I have experimented with *ngIf/else and [ngClass] directives, which do work, but unfortunatel ...

"Karma init" fails to initialize the configuration file

https://i.sstatic.net/m7mWB.png After installing karma-cli, karma, karma-jasmine, and karma-chrome-launcher, I made sure to set all the required path variables. Even when running "npm list karma," the proper version is returned. However, despite these ste ...

Dynamically transform array values into an object

I'm interested in developing an algorithm that can replicate values for a specific object schema, as shown in the sample data below: let layer1 = {name: 'x', values: [{_color: '#996666', time: 0, tween: 'quadEaseIn& ...

Could you display the picture prior to the function commencing?

This is the image that needs to be loaded before the loop begins. <div id="attendenceGridDivLoader" style="display:none"> <img src="<?php echo base_url() . 'images/loader.gif'; ?>" /> </div> <select onchange=checkAll ...

Learn the steps to resolve pagination issues in React. When the first page loads, ensure all data is displayed properly. Click

Here is an excerpt from my code snippet: const Inventory = () => { const [products, setProducts] = useState([]); const [pageCount,setPageCount] = useState(0); //console.log(pageCount); const [page,setPage] = useState(0); const navigate = useNa ...

What is the best way to determine in component.html whether the column type is equal to 1 to show the label text "Active,"

Having trouble checking the value of an object named ReportControl. If the column type is 1, display the label "active"; otherwise, display the label "not active" on reportcomponent.html. The data for the ReportControl object is as follows: {"reportId": ...

Ui Bootstrap (angularjs) sidebar is not functioning properly

I am encountering an issue with a sidenav that I am in the process of creating for one of my projects. The goal is to develop a side menu that shifts the content to the right when opened and returns it to the left when closed, essentially functioning as a ...

Converting an unordered list into a <select> dropdown with jquery - a step-by-step guide

Can you help me transform an unordered list that looks like this? <ul class="selectdropdown"> <li><a href="one.html" target="_blank">one</a></li> <li><a href="two.html" target="_blank">two</a></ ...