Home > Back-end >  a func called count and this func accepts 2 parameters and it'll return a string.If count(0,5).
a func called count and this func accepts 2 parameters and it'll return a string.If count(0,5).

Time:03-07

function count(param1, param2) {
  let value = [];
  for (let i = param1; i <= param2; i  ) {
    value.push(i);
  }
  return value;
}
let countNumber = count(0, 10);
console.log(countNumber);

I want to show it as a string like 0,1,2,3,4,5. But I can't solve this problem. I can only solve this as an array. My output is [0,1,2,3,4,5].

CodePudding user response:

try

console.log(countNumber.toString());

ref : https://www.w3schools.com/jsref/jsref_tostring_array.asp

CodePudding user response:

You can try this:

function count (start, end) {
    console.log(Array.from({length: end-start 1}, (v, k) => k start).join(","));
}

CodePudding user response:

function count(param1, param2) {
  let value = [];
  for (let i = param1; i <= param2; i  ) {
    value.push(i);
  }
  return value;
}
let countNumber = count(0, 10);
console.log(countNumber.toString());

CodePudding user response:

Just join the array elements back into a string, and return that from your function.

function count(param1, param2) {
  const value = [];
  for (let i = param1; i <= param2; i  ) {
    value.push(i);
  }
  return value.join(',');
}

const countNumber = count(0, 5);

console.log(countNumber);

  • Related