简体   繁体   English

Javascript递归-好像变量作用域使事情搞砸了

[英]Javascript recursion — seems like variable scope is messing things up

I'm trying to improve my JS by creating a numbers-to-words function in javascript. 我正在尝试通过在javascript中创建数字转单词功能来改善JS。 Ie, it takes 1234 and spits out "one thousand two hundred thirty four". 即,它需要1234并吐出“ 1,234”。 Coming from a Ruby background and wanting to write something recursive that could handle huge numbers, this is what I wrote: 我来自Ruby,想写一些可以处理大量数字的递归,这就是我写的:

var smallNums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'tweleve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
var suffixes = {"trillion": 1000000000000, "billion": 1000000000, "million": 1000000, "thousand": 1000, "hundred": 100};
var tens = {"ninety":90, "eighty":80, "seventy":70, "sixty":60, "fifty":50, "fourty":40, "thirty":30, "twenty":20}

function inWords (num) {
  sections = [];

  for (var word in suffixes) {
    var suffix_val = suffixes[word];
    if (num >= suffix_val) {
      suffix_multiplier = Math.floor(num / suffix_val);
      var toPush = inWords(suffix_multiplier) + " " + word;
      sections.push(toPush);
      num %= suffix_val;
    }
  }

  for (var word in tens) {
    var ten_val = tens[word];
    if (num >= ten_val) {
      sections.push(word);
      num -= ten_val;
    }
  }

  if (num > 0 || sections.length == 0) {
    sections.push(smallNums[num]);
  }
  return sections.join(' ');
}

The problem is, instead of outputting "one thousand two hundred thirty four", it's outputting, in this case, "two two hundred thirty four". 问题是,在这种情况下,它输出的不是“输出1243”,而是输出了“ 2234”。 I've triple checked the logic, and I'm thinking this has to do with a variable scope issue. 我已经对逻辑进行了三重检查,并且我认为这与可变范围问题有关。 Basically, it looks like all of the variables (like, say the sections array) are the same across levels of recursion. 基本上,看起来所有变量(例如, sections数组)在递归级别上都是相同的。

So it dives into recursion when the first loop hits the line with toPush , and then updates the upper-level's variables from inside the lower level. 因此,当第一个循环使用toPush该行时,它会陷入递归,然后从下层内部更新上层变量。

What makes this clear is if I throw a console.log(sections) after the toPush assignment. 明确的是,是否在toPush分配之后抛出console.log(sections) If the variables were just scoped to one "level" of the recursive function, the first log of sections would be empty, cause nothing in that level had filled it with anything, but it is, in fact "[one]". 如果将变量的作用域限定为递归函数的一个“级别”,则节的第一个日志将为空,导致该级别中的任何内容均未填充任何内容,但实际上为“ [一个]”。

At any rate the question was how to deal with this in recursive programming with Javascript. 无论如何,问题是如何在Javascript递归编程中处理该问题 Can I specify that a variable should not be global or available to recursions within? 我可以指定变量不应该是全局变量,也不可以用于其中的递归吗? Or, if not, what's the Javascript-y way of dealing with something like this? 或者,如果不是,处理此类问题的Javascript-y方法是什么?

This was a hell of a weird/unexpected bug to find out (I'm new to JS and didn't see this scoping issue coming), and I'd appreciate it if anyone has any suggestions. 这真是一个奇怪/出乎意料的错误的发现(我是JS的新手,没有看到这个范围问题),如果有人有任何建议,我将不胜感激。

sections is a global variable, thats why it's the same. sections是一个全局变量,这就是为什么它相同的原因。 Write a var in front of it. 在它前面写一个var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM