📖 Scenario: You are helping a small shop owner keep track of the prices of some fruits they sell. You will create an array to store these prices and then display them.
🎯 Goal: Create an array called fruitPrices with exact prices, then print the array to see all prices.
📋 What You'll Learn
Create an array called fruitPrices with the exact values: 10, 20, 30, 40, 50
Create a variable called numberOfFruits and set it to the length of fruitPrices
Use a for loop with variables index to iterate from 0 to numberOfFruits - 1
Inside the loop, print each price from fruitPrices using the index
Print the entire fruitPrices array at the end
💡 Why This Matters
🌍 Real World
Arrays are used everywhere to store lists of items like prices, names, or measurements. This helps shops, websites, and apps keep track of many things easily.
💼 Career
Knowing how to create and use arrays is a basic skill for any programming job, especially when working with collections of data.
Progress0 / 4 steps
1
Create the array of fruit prices
Create an array called fruitPrices with these exact values: 10, 20, 30, 40, 50
DSA Typescript
Hint
Use square brackets [] to create the array and separate values with commas.
2
Create a variable for the number of fruits
Create a variable called numberOfFruits and set it to the length of the fruitPrices array
DSA Typescript
Hint
Use the .length property to get the number of items in the array.
3
Loop through the array and print each price
Use a for loop with variable index to go from 0 to numberOfFruits - 1. Inside the loop, print each price from fruitPrices using index
DSA Typescript
Hint
Use a for loop starting at 0 and ending before numberOfFruits. Use console.log to print each price.
4
Print the entire array
Print the entire fruitPrices array using console.log(fruitPrices)
DSA Typescript
Hint
Use console.log(fruitPrices) to print the whole array after the loop.
Practice
(1/5)
1. Which of the following is the correct way to declare an array of numbers in TypeScript?
easy
A. const numbers: number[] = [1, 2, 3];
B. const numbers: number = [1, 2, 3];
C. const numbers: array = (1, 2, 3);
D. const numbers = {1, 2, 3};
Solution
Step 1: Understand TypeScript array syntax
In TypeScript, arrays are declared using the type followed by square brackets, e.g., number[].
Step 2: Check each option's syntax
const numbers: number[] = [1, 2, 3]; correctly declares an array of numbers. Options A, B, and D use incorrect syntax for arrays.
Final Answer:
const numbers: number[] = [1, 2, 3]; -> Option A
Quick Check:
Array declaration with type[] = correct syntax [OK]
Hint: Use type[] to declare arrays in TypeScript [OK]
Common Mistakes:
Using type without [] for arrays
Using parentheses instead of square brackets
Using curly braces instead of square brackets
2. Which of the following is the correct syntax to initialize an empty array of strings in TypeScript?
easy
A. let names = string[];
B. let names: string = [];
C. let names: string[] = [];
D. let names: [] = string;
Solution
Step 1: Identify correct type annotation for empty string array
To declare an empty array of strings, use string[] as the type and assign an empty array [].
Step 2: Validate each option
let names: string[] = []; correctly uses let names: string[] = []. Others have syntax errors or wrong type placement.
Final Answer:
let names: string[] = []; -> Option C
Quick Check:
Empty array of strings = string[] = correct [OK]
Hint: Use type[] with empty brackets to declare empty arrays [OK]
Common Mistakes:
Omitting [] after type for arrays
Assigning type incorrectly after =
Using invalid syntax like let names = string[];
3. What will be the output of the following TypeScript code?