var array = ['a', 'b', 'c']
function arrayTransform(array) {
if (array.lengths === 0) {
console.log("Empty")
} else {
console.log(array.join());
}
}
arrayTransform(array);
the outcome should be 1.a, 2.b, 3.c I am getting abc
var array = ['a', 'b', 'c']
function arrayTransform(array) {
if (array.lengths === 0) {
console.log("Empty")
} else {
console.log(array.join());
}
}
arrayTransform(array);
the outcome should be 1.a, 2.b, 3.c I am getting abc
Seems you wnat to add the index with the element. In that case use map function which will return an array and then use join to create the final string
var array = ['a', 'b', 'c']
function arrayTransform(array) {
if (array.lengths === 0) {
console.log("Empty")
} else {
return array.map((item, index) => {
return `${index+1}.${item}` // template literals
}).join()
}
}
console.log(arrayTransform(array));
You could map the incremented index with the value and join it to the wanted style.
var array = ['a', 'b', 'c']
function arrayTransform(array) {
if (array.lengths === 0) {
console.log("Empty")
} else {
console.log(array.map((v, i) => [i + 1, v].join('.')).join());
}
}
arrayTransform(array);