🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
A left arrow star pattern consists of stars (*) arranged in a way that resembles a left-pointing arrow. The stars start from one and increase to the middle row, then decrease back to one in the lower half, while being aligned to the right. This is a good exercise for practicing loops and formatting output in JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts the number of rows for the left arrow pattern.
- Prints a left arrow-shaped pattern using stars (
*).
Example:
- Input:
rows = 5 - Output:
* ** *** **** ***** **** *** ** *
Solution Steps
- Input the Number of Rows: The user specifies the number of rows for the arrow (upper half of the arrow).
- Use Nested Loops: The outer loop handles the rows, and the inner loops handle printing the stars and spaces to align the stars to the right.
- Display the Left Arrow Pattern: Print stars in increasing order for the upper part and decreasing order for the lower part, with leading spaces to align them to the right.
JavaScript Program
// Step 1: Input the number of rows for the left arrow pattern
let rows = parseInt(prompt("Enter the number of rows: "));
// Step 2: Print the upper part of the left arrow
for (let i = 1; i <= rows; i++) {
let output = '';
// Print spaces for alignment
for (let j = 1; j <= rows - i; j++) {
output += ' ';
}
// Print stars for the current row
for (let k = 1; k <= i; k++) {
output += '*';
}
// Print the output for the current row
console.log(output);
}
// Step 3: Print the lower part of the left arrow
for (let i = rows - 1; i >= 1; i--) {
let output = '';
// Print spaces for alignment
for (let j = 1; j <= rows - i; j++) {
output += ' ';
}
// Print stars for the current row
for (let k = 1; k <= i; k++) {
output += '*';
}
// Print the output for the current row
console.log(output);
}
Explanation
Step 1: Input the Number of Rows
- The program starts by asking the user to input the number of rows for the left arrow pattern. This input is converted to an integer using
parseInt().
Step 2: Print the Upper Part of the Left Arrow
- The first loop handles printing the upper part of the arrow:
- The inner loop first prints spaces to align the stars to the right.
- Then, stars are printed in increasing order based on the row number.
Step 3: Print the Lower Part of the Left Arrow
- The second loop handles printing the lower part of the arrow:
- Similar to the upper part, the inner loop prints spaces first, followed by stars in decreasing order for each row.
Step 4: Output the Rows
- After constructing each row with spaces and stars, it is printed using
console.log().
Output Example
For rows = 5, the output will be:
*
**
***
****
*****
****
***
**
*
For rows = 4, the output will be:
*
**
***
****
***
**
*
Conclusion
This JavaScript program prints a left arrow star pattern using nested loops. The stars are aligned to the right by printing spaces first, and the number of stars increases and then decreases to form the left arrow shape. This exercise is useful for practicing loop control, alignment, and formatting output in JavaScript.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment