Inverted Right-angle triangle of Asterisk
Problem:- write a programme that takes an integer input I and prints an inverted right-angle triangle pattern of asterisks with I rows.
Divide the problem into small Steps: -
first, run the loop based on the input of the integer
run loop inside the loop for each asterisks rows
Applying the steps:-
first, run the loop based on the input of the integer
-> Let's assume the input of the integer is 5.
const value = 5
apply for loop and initiate the variable I = 0 and the set condition is less than the value and the changer is set as an increment. and inside the loop initiate the variable named pattern and assign the value as an empty string.
for (let i = 0; i < value; i++){ let pattern = '' }
run loop inside the loop for each asterisks rows
-> apply for loop inside the loop. initiate the variable j = 0 and the condition is set to less than value minus I and the changer is an increment.
inside this loop using the addition assignment operator assigns asterisks to the pattern.
console the variable pattern out of the inner loop and at the last of the outer loop.
for (let j = 0; j < value - i: j++) { pattern += '*' } console.log(pattern)
so basically, the outer loop is executed several times that assign in the value variable. for each loop cycle the inside loop is run as the value of the variable minus the outer loop I. That's why each new cycle in the inside loop is decreased by one and asterisks are printed by decreasing one each time.
const value = 5;
for (let i = 0; i < value; i++) {
let pattern = "";
for (let j = 0; j < value - i; j++) {
pattern += "*";
}
console.log(pattern);
}