I am trying to write a function which takes an array and returns a new array with the number of characters in each string from the original array, but it is not working.
Only the number of characters of the first string is returned. I am almost sure that this is because the parameter suggests that there is only parameter, rather than an array of parameters, but I am not sure how this should be represented as this is how the original problem was set up and presented to me. I hope someone knows what I mean (!). I am still pretty new to this, so sorry if it seems like it should be obvious…
Could someone offer help?
function sortByLength(array) {
let i;
let tempArray = [];
for (i = 0; i < array.length; i++) {
tempArray.push(array[i]).length;
}
return tempArray;
}
function sortByLength(array) {
let i;
let tempArray = [];
for (i = 0; i < array.length; i++) {
tempArray.push(array[i].length);
}
return tempArray;
}
console.log(sortByLength("dfdww", "scvx"));
yes, sorry. I am trying to solve a problem that was presented to me in this way, so I assumed (wrongly) that the parameter format was correct. Just need to figure out how to set an array as the parameter in a function then…I will try to find out