I’m trying to print out the result from a loop onto the console but it keeps printing on a new line instead of printing everything on the same line.
This is the output I’m expecting:
...17°C in 1 days ... 21°C in 2 days ... 21°C in 3 days
But this is the output I keep getting:
... 17°C in 1 days
... 21°C in 2 days
... 23°C in 3 days
I’ve tried most of the answers I found but they are not working.
const arr =[17, 21, 23];
function printForcast(arr) {
for (let i = 0; i < arr.length; i++) {
let days = i;
if(i <= days){
days +=1;
}
console.log(`... ${arr[i]}°C in ${days} days`);
}
return arr;
}
printForcast(arr);
<!DOCTYPE html>
3
Answers
Just collect the log items in an array and print at once in the end:
Since
console.log()
will always output a newline, you’ll need to append (+=
) your output to a string, and then log that after the loop:I think this simple snippet would work
Hope this helps 🙂