调用具有不同参数的函数不起作用

时间:2019-06-21 00:53:45

标签: javascript

我看不到为什么没有打印“ name2”。这是我的第一个示波器练习。有什么技巧可以帮助您更好地发现这些错误,或者只是练习?我已经看了半个小时了,对我来说似乎很完美。

// The scope of `random` is too loose 
const random = Math.floor(Math.random() * 3);

const getRandEvent = () => {
  if (random === 0) {
    return 'Marathon';
  } else if (random === 1) {
    return 'Triathlon';
  } else if (random === 2) {
    return 'Pentathlon';
  }
};

// The scope of `days` is too tight 
const getTrainingDays = event => {
  let days;
  if (event === 'Marathon') {
    days = 50;
  } else if (event === 'Triathlon') {
    days = 100;
  } else if (event === 'Pentathlon') {
    days = 200;
  }

  return days;
};
const name = 'Nala';
const name2 = 'Warren'
// The scope of `name` is too tight 
const logEvent = event => {
  console.log(`${name}'s event is: ${event}`);
};

const logTime = days => {
  console.log(`${name}'s time to train is: ${days} days`);
};

const event = getRandEvent();
const days = getTrainingDays(event);
// Define a `name` variable. Use it as an argument after updating logEvent and logTime 


logEvent(name, event);
logTime(name, days);

const event2 = getRandEvent();
const days2 = getTrainingDays(event2);

// Define a `name` variable. Use it as an argument after updating logEvent and logTime 


logEvent(event2, name2);
logTime(days2, name2);

这就是我得到的:

Nala's event is: Marathon
Nala's time to train is: 200 days
Nala's event is: Triathlon
Nala's time to train is: 100 days

这就是我应该得到的

Nala's event is: Marathon
Nala's time to train is: 200 days
Warren's event is: Triathlon
Warren's time to train is: 100 days

2 个答案:

答案 0 :(得分:0)

好,我进行了更改,现在我得到了

“ Nala的活动是:马拉松 纳拉的培训时间是:50天 马拉松的活动是:沃伦 50的训练时间是:沃伦(Warren)天”


    // The scope of `random` is too loose 
const random = Math.floor(Math.random() * 3);

const getRandEvent = () => {
  if (random === 0) {
    return 'Marathon';
  } else if (random === 1) {
    return 'Triathlon';
  } else if (random === 2) {
    return 'Pentathlon';
  }
};

// The scope of `days` is too tight 
const getTrainingDays = event => {
  let days;
  if (event === 'Marathon') {
    days = 50;
  } else if (event === 'Triathlon') {
    days = 100;
  } else if (event === 'Pentathlon') {
    days = 200;
  }

  return days;
};
const name = 'Nala';
const name2 = 'Warren';
// The scope of `name` is too tight 
const logEvent = (name,event) => {
  console.log(`${name}'s event is: ${event}`);
};

const logTime = (name,days) => {
  console.log(`${name}'s time to train is: ${days} days`);
};

const event = getRandEvent();
const days = getTrainingDays(event);
// Define a `name` variable. Use it as an argument after updating logEvent and logTime 


logEvent(name, event);
logTime(name, days);

const event2 = getRandEvent();
const days2 = getTrainingDays(event2);

// Define a `name` variable. Use it as an argument after updating logEvent and logTime 


logEvent(event2, name2);
logTime(days2, name2);

答案 1 :(得分:0)

随着更改函数的参数列表,您在第二次调用函数时反转了参数。见下文。

// The scope of `random` is too loose 
const random = Math.floor(Math.random() * 3);

const getRandEvent = () => {
  if (random === 0) {
    return 'Marathon';
  } else if (random === 1) {
    return 'Triathlon';
  } else if (random === 2) {
    return 'Pentathlon';
  }
};

// The scope of `days` is too tight 
const getTrainingDays = event => {
  let days;
  if (event === 'Marathon') {
    days = 50;
  } else if (event === 'Triathlon') {
    days = 100;
  } else if (event === 'Pentathlon') {
    days = 200;
  }

  return days;
};
const name = 'Nala';
const name2 = 'Warren'
// The scope of `name` is too tight 
const logEvent = (name, event) => {
  console.log(`${name}'s event is: ${event}`);
};

const logTime = (name, days) => {
  console.log(`${name}'s time to train is: ${days} days`);
};

const event = getRandEvent();
const days = getTrainingDays(event);
// Define a `name` variable. Use it as an argument after updating logEvent and logTime 


logEvent(name, event);
logTime(name, days);

const event2 = getRandEvent();
const days2 = getTrainingDays(event2);

// Define a `name` variable. Use it as an argument after updating logEvent and logTime 


logEvent(name2, event2);
logTime(name2, days2);

相关问题