38 - Nested for loops, Price Matrix (JS lessons)
๐ Task: Price Matrix
The user enters a number from 1 to 10.
After clicking the button, the following result should be displayed:
$1
( $11 $21 $31 $41 $51 )
$2
( $12 $22 $32 $42 $52 )
$3
( $13 $23 $33 $43 $53 )
- Use nested
for loops
- Use
if to check the range
- If the number is less than 1 or greater than 10 โ display an error message
๐ How it works:
- Get the number from the input
- Check that it is within the range from 1 to 10
- The outer loop creates the rows (
$1, $2, $3...)
- The inner loop creates the values inside the parentheses
- Append each new value with
+=
๐งช Example:
Input: 3
$1
( $11 $21 $31 $41 $51 )
$2
( $12 $22 $32 $42 $52 )
$3
( $13 $23 $33 $43 $53 )
๐ HTML structure of the result:
- Each row must be placed inside a
div class="price-row"
<div>
$2
( $12 $22 $32 $42 $52 )
</div>
๐ Important:
- The outer loop is responsible for the rows
- The inner loop is responsible for the values inside the row
- The range check must be done with
if