练习JavasScript第2练习:TypeError:(0,_leap.isLeap)不是来自

时间:2018-11-12 06:27:37

标签: javascript

我正在执行Exercism的JavaScript练习第二次练习,以找出哪些年份是leap年。

const isLeap = (year) => {

    if (year % 400 === 0) {
        return true
    } else if (year % 4 === 0 && year % 100 !== 0) {
        return true
    } else {
        return false
    }
}

console.log(isLeap(2015))
console.log(isLeap(2016))
console.log(isLeap(2100))
console.log(isLeap(2000))
console.log(isLeap(1800))

以及当我运行以下测试时:

import { isLeap } from './leap';

describe('A leap year', () => {
  test('year not divisible by 4: common year', () => {
    expect(isLeap(2015)).toBeFalsy();
  });

  xtest('year divisible by 4, not divisible by 100: leap year', () => {
    expect(isLeap(2016)).toBeTruthy();
  });

  xtest('year divisible by 100, not divisible by 400: common year', () => {
    expect(isLeap(2100)).toBeFalsy();
  });

 xtest('year divisible by 400: leap year', () => {
    expect(isLeap(2000)).toBeTruthy();
  });

  xtest('year divisible by 200, not divisible by 400: common year', () => {
    expect(isLeap(1800)).toBeFalsy();
  });
});

它给出了一条错误消息       TypeError:(0,_leap.isLeap)不是函数

我经历了条件逻辑,一切似乎都很好。我通过粗箭头函数声明了isLeap函数,但是错误指出它不是函数。我想念什么吗?

1 个答案:

答案 0 :(得分:2)

您没有从文件中导出功能。您需要:

export const isLeap = () => {...}

在声明时,或

export { isLeap }

声明后