为什么它不会返回真实?简单的功能JS

时间:2018-03-08 19:36:15

标签: javascript function recursion module

function iseven(n) {
    function remainder(n) {
        if (n%2==0) {
            return true;
        } else {
            return false;
        }
    }
}
console.log(iseven(4));

返回undefined

3 个答案:

答案 0 :(得分:2)

尝试

function iseven(n) { if (n%2==0) { return true; } else { return false; } } console.log(iseven(4));

答案 1 :(得分:2)

你想要更像这样的东西

require 'benchmark'

Benchmark.bm do |b|
  (10..15).each do |num|
    str = '/foo/bar' * num
    puts str
    b.report("#{num} repeats:") { /(([^\/]+\/)+)\1\1/ === str }
  end
end

10 repeats:  0.000000   0.000000   0.000000 (  0.000015)
/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar
11 repeats:  0.000000   0.000000   0.000000 (  0.000004)
/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar
12 repeats:  0.000000   0.000000   0.000000 (  0.000004)
/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar
13 repeats:  0.000000   0.000000   0.000000 (  0.000004)
/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar
14 repeats:  0.000000   0.000000   0.000000 (  0.000004)
/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar
15 repeats:  0.000000   0.000000   0.000000 (  0.000005)

有点简洁:

GO_LINKSHARED = ""

不太确定为什么原件是这样的结构..

答案 2 :(得分:0)

您的主要功能iseven()不会返回任何内容。根据您的代码,它应该返回false。这是修复:

function iseven(n) { 
    function remainder(n) { 
        if (n%2==0) { 
            return true; 
        } 
        else { 
            return false; 
        } 
    }
    //iseven() should return something
    return false; 
}
console.log(iseven(4));