How can I print the output as
17 in 1 days
21 in 2 days
23 in 3 days
and not
17 in 0 days
21 in 1 days
23 in 2 days
JavaScript
x
8
1
const arr = [17, 21, 23];
2
3
const printForecase = function() {
4
for (let i = 0; i < arr.length; i++) {
5
console.log(`${arr[i]} in ${i} days`);
6
} }
7
printForecase();
8
Advertisement
Answer
Since the index starts from 0, you just need to add one (1) when you print it in your console.log()
(${i+1}
)
JavaScript
1
8
1
const arr = [17, 21, 23];
2
3
const printForecase = function() {
4
for (let i = 0; i < arr.length; i++) {
5
console.log(`${arr[i]} in ${i+1} days`);
6
} }
7
printForecase();
8