为什么返回关键字不给我带来理想的结果?

发布于 2025-01-22 06:07:57 字数 404 浏览 3 评论 0原文

我很想克服这个挑战。我被困了将近2周。这是代码:

function multiply(top,bottom){
  var num = top;
  var multiple = 2;
  while (num <= bottom){
    num *= multiple;
    console.log(num);
  }
  return num;
}

console.log(multiply(5, 100));// expected outcome is 80 

我很想从Console.log返回80的最后一个结果,但是每次使用返回关键字时,它都不会带来所需的结果。

There is this challenge that I'd love to overcome. I've been stuck for almost 2 weeks. Here is the code:

function multiply(top,bottom){
  var num = top;
  var multiple = 2;
  while (num <= bottom){
    num *= multiple;
    console.log(num);
  }
  return num;
}

console.log(multiply(5, 100));// expected outcome is 80 

I'd love to return the last result from the console.log which is 80 but each time I use the return keyword, it doesn't bring the desired result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2025-01-29 06:07:57

您有另一个可跟踪下一个倍数的变量,因此我们可以在将其分配给num之前对其进行检查。

function multiply(top,bottom) {
 var num = top;
 var multiple = 2;
 var next = num * multiple;
 while(next < bottom) {
  num = next;
  next = num * multiple;
 }
 return num;
}

console.log(multiply(5, 100));

You have another variable which keeps track of the next multiple, so that we can check it before we assign it to num.

function multiply(top,bottom) {
 var num = top;
 var multiple = 2;
 var next = num * multiple;
 while(next < bottom) {
  num = next;
  next = num * multiple;
 }
 return num;
}

console.log(multiply(5, 100));

无可置疑 2025-01-29 06:07:57

这里发生的事情是,您正在乘以一个数字并这样做,直到达到欲望结果为止。但是,由于每个步骤都没有添加1个,因此可能会超过结果。
您需要的是具有单独的变量来存储上一个值:

function multiply(top,bottom){
  var num = top;
  var multiple = 2;
  while (top < bottom)
  {
    num = top; //get prevous value
    top *= multiple;
    console.log(num);
  }
  return num;
}

console.log(multiply(5, 100));// expected outcome is 80

或少1步是在内进行条件的计算:

function multiply(top,bottom){
  var num = top;
  var multiple = 2;
  while ((top *= multiple) < bottom)
  {
    num = top; //get prevous value
    console.log(num);
  }
  return num;
}

console.log(multiply(5, 100));// expected outcome is 80

What's happening here is that you are multiplying a number and doing so until it reaches the desire result. But since it's not adding 1 with each step it can exceed the result.
What you need is have separate variable to store the previous value:

function multiply(top,bottom){
  var num = top;
  var multiple = 2;
  while (top < bottom)
  {
    num = top; //get prevous value
    top *= multiple;
    console.log(num);
  }
  return num;
}

console.log(multiply(5, 100));// expected outcome is 80

Or having 1 less step is to do the calculation inside the while condition:

function multiply(top,bottom){
  var num = top;
  var multiple = 2;
  while ((top *= multiple) < bottom)
  {
    num = top; //get prevous value
    console.log(num);
  }
  return num;
}

console.log(multiply(5, 100));// expected outcome is 80

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文