最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript loop to return string x times - Stack Overflow

programmeradmin1浏览0评论

I'm doing Udacity javascript studies now and there is one quiz that bothers me. I know how to do it easily in ruby, but this one is killing me.

I need to call function and return "ha" num times and add "!" at the end with the loop.

I tried this but it didn't help. Should be very simple.

function laugh(num) {
  for (var x = 0; num; x ++) {
      return 'ha';
  }
}

console.log(laugh(3));

I'm doing Udacity javascript studies now and there is one quiz that bothers me. I know how to do it easily in ruby, but this one is killing me.

I need to call function and return "ha" num times and add "!" at the end with the loop.

I tried this but it didn't help. Should be very simple.

function laugh(num) {
  for (var x = 0; num; x ++) {
      return 'ha';
  }
}

console.log(laugh(3));
Share Improve this question asked Jul 17, 2017 at 17:04 Олег ЯкунинОлег Якунин 1402 silver badges12 bronze badges 4
  • 1 A function returns once. – davidxxx Commented Jul 17, 2017 at 17:06
  • 3 i doubt the question is worded like that, you probably dont want to return ha 3 times, you probably want to return a string that has 3 ha's in it. like "ha ha ha!". there should be one return. – John Boker Commented Jul 17, 2017 at 17:06
  • return returns immediately. IOW, the first pass through the loop returns, and the next passes never happen. Clearly that isn't what you intend. – Ken White Commented Jul 17, 2017 at 17:07
  • Var str =""; str+="ha"; return str + "!"; – Yogen Darji Commented Jul 17, 2017 at 17:09
Add a ment  | 

5 Answers 5

Reset to default 5

Actually you don't even need the loop.

const laugh = num => 'ha'.repeat(num) + '!';

console.log(laugh(3));
console.log(laugh(5));

Returning in a loop will return the whole function. To make this work you could concatenate the string in the loop and then return the concatenated output. You also formatted your loop incorrectly, you need to tell the loop to stop when x is less than num. Try:

function laugh(num) {
  var laughString = '';
  for (var x = 0; x < num; x++) {
    laughString += 'ha';
  }
  return laughString + '!';
}

console.log(laugh(3));

You can only return once from a function. Try building up the string you need in the loop, then return the value after the loop is done.

(Note: You can have multiple return statements in a function, but as soon as you hit one of them the function pletes its execution.)

function laugh(num) {
  var outString = '';
  for (var x = 0; x<num; x ++) {
    outString += ' ha';
  }
  return outString.substring(1) + '!';
}

console.log(laugh(3));

function laugh(input) {
  var answer = '';
  for (var i = 0; i < input; i++) {
    answer += 'ha';
  }
  return (answer + '!');
}

First, set a variable to an empty string, (it will be your answer) Then, write a for loop that loops according to the input (you did that) then inside the for loop you add 'ha' to your answer string finally outside the for loop (after it has ran all the loops) return the answer string plus !

发布评论

评论列表(0)

  1. 暂无评论