I know that for an array join()
can be used to produce what I am trying to accomplish here, but I am working with a string. What method would work with a string?
I want my output to look like “3 then 4 then 5 then 6 then 7”, etc.
I’ve come close to getting what I am looking for but my current code adds an extra “then” at the end, which is not what I want:
JavaScript
x
10
10
1
let appendString = '';
2
let then = ' then ';
3
4
function countUp(start) {
5
for(var i = 0; i < 10; i++){
6
appendString += (start++) + then;
7
}
8
console.log(appendString);
9
}
10
I do not want solutions, I just would appreciate being pointed in the right direction.
Advertisement
Answer
what about this?
JavaScript
1
13
13
1
let appendString = '';
2
let then = ' then ';
3
function countUp(start) {
4
for(var i = 0; i < 10; i++){
5
appendString += (start++)
6
if(i<9){
7
appendString+=then
8
}
9
10
}
11
console.log(appendString);
12
}
13
or
JavaScript
1
10
10
1
let appendArray = [];
2
let then = ' then ';
3
function countUp(start) {
4
for(var i = 0; i < 10; i++){
5
appendArray.push(start++);
6
}
7
8
console.log(appendArray.join(then));
9
}
10