-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayToStrings.ts
More file actions
32 lines (28 loc) · 1.02 KB
/
Copy patharrayToStrings.ts
File metadata and controls
32 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Given an array of forecasted maximum temperatures, the thermometer displays a
string with the given temperatures. Example: [17, 21, 23] will print "... 17ºC in 1
days ... 21ºC in 2 days ... 23ºC in 3 days ..."
Your tasks:
1. Create a function 'printForecast' which takes in an array 'arr' and logs a
string like the above to the console. Try it with both test datasets.
2. Use the problem-solving framework: Understand the problem and break it up
into sub-problems!
Test data:
§ Data 1: [17, 21, 23]
§ Data 2: [12, 5, -5, 0, 4]
GOOD LUCK 😀
*/
// declear an empty string
// loop through the array
// add each item in the array to the empty string using (+=)
// increase the number of days by using (i + 1) and add to the string
// return string
const printForecast = (array: number[]) => {
let output = "";
for (let i = 0; i < array.length; i++) {
output += `${array[i]}ºC in ${i + 1} days ... `;
}
return "... " + output;
};
console.log(printForecast([17, 21, 23]));
console.log(printForecast([12, 5, -5, 0, 4]));