我的if / else条件没有显示出来

时间:2018-04-18 10:15:22

标签: javascript

我目前正在学习Javascript,我从课程中删除了脚本,只是玩弄了我目前所掌握的知识。

作为一个挑战,看看我是否理解控制流和函数参数,我决定制作一个Container Dock日志。我们的想法是根据容器的到达和停靠时间记录容器:

  • 新容器是停靠不到30分钟的容器。
  • 旧容器是停靠至少30分钟的容器。

根据该信息,我制作了以下代码:

// Containers type: Old and New based on 30min arrivals.

const containerDocked = (container,arrivalTime) => {
  newContainer = arrivalTime < 30;
  oldContainer = arrivalTime >= 30;
  if (newContainer) {
    console.log('New Container: ' + container + ' just arrived ' + arrivalTime + 'min ago.' );
  } 
    else if (oldContainer) {
      console.log('Docked Container: ' + container + ' has been docked for ' + arrivalTime + 'min.');
    }
    else if(container === false && arrivalTime === true) {
      console.log('A container that arrived '+ arrivalTime + 'mins ago, has not been logged!!!');
    }
    else if(container === true && arrivalTime === false) {
      console.log('Container: ' + container + 'has not been logged!!!');
    }
    else {
      console.log('Container has not been logged!!!!');
    }
};

containerDocked('John', 24);
containerDocked('Elizabeth', 57);
containerDocked('Harry', 30);
containerDocked('Mike');

它似乎确实有效,但正如您所看到的那样,“Mike”不会记录我的if / else规则。你能指出我做错了吗?

3 个答案:

答案 0 :(得分:3)

这是修复

container === truearrivalTime === true

的问题

// Containers type: Old and New based on 30min arrivals.

const containerDocked = (container,arrivalTime) => {
  newContainer = arrivalTime < 30;
  oldContainer = arrivalTime >= 30;
  if (newContainer) {
    console.log('New Container: ' + container + ' just arrived ' + arrivalTime + 'min ago.' );
  } 
    else if (oldContainer) {
      console.log('Docked Container: ' + container + ' has been docked for ' + arrivalTime + 'min.');
    }
    else if(!container && arrivalTime) {
      console.log('A container that arrived '+ arrivalTime + 'mins ago, has not been logged!!!');
    }
    else if(container && !arrivalTime) {
      console.log('Container: ' + container + 'has not been logged!!!');
    }
    else {
      console.log('Container has not been logged!!!!');
    }
};

containerDocked('John', 24);
containerDocked('Elizabeth', 57);
containerDocked('Harry', 30);
containerDocked('Mike');

答案 1 :(得分:0)

你必须在else if语句中检查wetTime arrivalTime是否为空。

// Containers type: Old and New based on 30min arrivals.

const containerDocked = (container,arrivalTime) => {
  newContainer = arrivalTime < 30;
  oldContainer = arrivalTime >= 30;
  if (newContainer) {
    console.log('New Container: ' + container + ' just arrived ' + arrivalTime + 'min ago.' );
  } 
    else if (oldContainer) {
      console.log('Docked Container: ' + container + ' has been docked for ' + arrivalTime + 'min.');
    }
    else if(container === false && arrivalTime === true) {
      console.log('A container that arrived '+ arrivalTime + 'mins ago, has not been logged!!!');
    }
    else if(container !=null && !arrivalTime) {
      console.log('Container: ' + container + ' has not been logged!!!');
    }
    else {
      console.log('Container has not been logged!!!!');
    }
};

containerDocked('John', 24);
containerDocked('Elizabeth', 57);
containerDocked('Harry', 30);
containerDocked('Mike');

答案 2 :(得分:0)

我相信迈克不会记录,因为您在调用arrivalTime时尚未指定containerDocked。这意味着arrivalTimeundefined,所有这些条件都会返回false,并执行else块。

请注意,arrivalTime === falsefalse