so the code bellow would write ten numbers in the console. How could I add some text behind, lets say, number five? So it would write numbers like usual, but the number five would have also some text to it.
for (let x = 1; x < 11; x++) {
console.log(x);
}
One more idea is to use switch statement (Could be more readable for a lot of cases "do something on 1" and "2" and "5" and so on).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
<script>
for (let i = 1; i < 11; i++) {
switch (i) {
case 1:
console.log("hello: " + i);
break;
case 5:
console.log("hello: " + i);
break;
default:
console.log(i);
}
}
</script>
defaultOptional A default clause; if provided, this clause is executed if the value of expression doesn't match any of the case clauses.
** out of topic: it is more common to use i inside for loop (i == index).
Just use a ternary condition that checks if x is 5.
for (let x = 1; x < 11; x++) {
console.log(x + (x == 5 ? ' text' : ''));
}
If you want the text before the number you can just move the expression like:
console.log((x == 5 ? 'text ' : '') + x)
for(let x = 0; x < 11; x++) {
if(x === 5) {
console.log('number 5 and additional text');
} else {
console.log(x);
}
}
if I understood your questioncorrectly, this should work