I am very new to vue and I wanted to play around with methods a little. What I wanted to try was printing out an array of Strings and this is the method I tried to use:
JavaScript
x
10
10
1
printStringArray(objectWithArray) {
2
i = 0;
3
s = '';
4
5
while(i < objectWithArray.stringArray.length) {
6
s = objectWithArray.stringArray[i] + s,
7
};
8
return s;
9
},
10
But I get errors because of i and s. I tried a few things but it always either says I didn’t define or them or I defined them but didn’t use them. Any ideas? I looked at some posts that used working code but if I used that code to see my mistake in comparisons, I get the same erros. I feel like it’s very simple but I can’t find anything on it.
Advertisement
Answer
You can use the following method:
JavaScript
1
6
1
let array = [1, 2, 3, 4];
2
function printArrayElements(array){
3
array.forEach(element => console.log(element))
4
}
5
6
printArrayElements(array);
You can achieve that using the while
loop as well:
JavaScript
1
10
10
1
let array = [1, 2, 3, 4]
2
function printArrayElement(array){
3
let index = 0;
4
while (index < array.length){
5
console.log(array[index]);
6
index +=1;
7
}
8
}
9
10
printArrayElement(array);