Coding Challenge #1
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]
문제를 해결하기 위해서는 다음과 같은 두 가지 스텝을 거쳐야 한다.
1. Understanding the problem 문제를 이해하기
2. Breaking up into sub-problems 작은 문제들로 쪼개기
// test data
const data1 = [17, 21, 23];
const data2 = [12, 5, -5, 0, 4];
// what we want to make as a result
console.log(`... ${data1[0]}ºC ... ${data1[1]}ºC ... ${data1[2]}ºC ...`);
// create a function which takes in an array 'arr'
// and logs a string like the above to the console
const printForecast = function (arr) {
let str = ''; // for문의 결과값을 연속해서 저장할 변수 선언
for (let i = 0; i < arr.length; i++) {
str += `${arr[i]}ºC in ${i + 1} days ...`;
}
console.log('... ' + str); // 콘솔에 출력
};
printForecast(data1);