0
0
Typescriptprogramming~15 mins

Rest parameters with types in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Rest parameters with types
📖 Scenario: You are building a simple calculator that can add any number of numbers together.Users will give you many numbers, and you want to add them all up.
🎯 Goal: Create a function that uses rest parameters with types to accept any number of numbers and returns their sum.
📋 What You'll Learn
Create a function called sumNumbers that uses rest parameters with type number[].
Inside the function, add all the numbers together.
Call the function with multiple numbers.
Print the result.
💡 Why This Matters
🌍 Real World
Functions with rest parameters are useful when you don't know how many inputs you will get, like adding many numbers or combining many strings.
💼 Career
Understanding rest parameters and types is important for writing flexible and type-safe functions in TypeScript, a skill valuable for many programming jobs.
Progress0 / 4 steps
1
Create the function with rest parameters
Write a function called sumNumbers that takes rest parameters named numbers with type number[].
Typescript
Need a hint?

Use ...numbers: number[] to accept many numbers as an array.

2
Add the numbers inside the function
Inside the sumNumbers function, create a variable called total set to 0. Then use a for loop with variable num to add each number in numbers to total.
Typescript
Need a hint?

Use a for...of loop to add each number to total.

3
Call the function with multiple numbers
Call the sumNumbers function with the numbers 5, 10, and 15, and store the result in a variable called result.
Typescript
Need a hint?

Call the function with the numbers separated by commas inside the parentheses.

4
Print the result
Write a console.log statement to print the value of the variable result.
Typescript
Need a hint?

Use console.log(result) to show the sum.