编写一个名为" isOldEnoughToDrinkAndDrive"的函数。
给出一个数字,在这种情况下是一个年龄," isOldEnoughToDrinkAndDrive"返回这个特定年龄的人是否足够大,可以在美国合法饮酒和开车。
注意: *美国的法定饮酒年龄为21岁。 *在美国喝酒和开车总是违法的。
var output = isOldEnoughToDrinkAndDrive(22);
console.log(output); // --> false
入门代码:
function isOldEnoughToDrinkAndDrive(age) {
// your code here
}
我的解决方案:
function isOldEnoughToDrinkAndDrive(age) {
// your code here
var output = age >= 21;
console.log(output);
return output;
}
var output = isOldEnoughToDrinkAndDrive(21);
我在提交结果时遇到的错误如下。
This is the error I am getting
有人可以帮忙吗?
答案 0 :(得分:2)
您的代码应该返回false
,因为总是非法在美国喝酒和开车。
return false;
答案 1 :(得分:1)
你的代码应该总是返回false,那么为什么不返回FALSE(因为它在美国总是非法饮酒和开车)?
无论如何,你的代码返回TRUE,因为21岁大于或等于(在这种情况下)21岁的饮酒年龄。但是结果与传递的参数没有任何关系。
答案 2 :(得分:1)
但我认为您从表单中获取数据类型。你应该在比较之前将它转换为Int:
function isOldEnoughToDrinkAndDrive(age) {
return false;
}
但此问题的正确答案是这样的代码:
#include <iostream>
#include <string>
struct port_t {
int id;
std::string name;
};
struct node_t {
struct port_t port;
struct node_t *next;
};
void create_node(node_t **list, int id, std::string name) {
node_t *node = *list;
if(node == NULL) {
node = new (node_t);
node->port.id = id;
node->port.name.assign(name);
node->next = NULL;
*list = node;
return;
}
while (node != NULL) {
if (node->next == NULL) {
node->next = new (node_t);
node = node->next;
node->port.id = id;
node->port.name.assign(name);
node->next = NULL;
break;
} else {
node = node->next;
}
}
}
int print_list(node_t *list) {
node_t *tmp = list;
if(tmp == NULL) {
std::cout << "List is empty!" << std::endl;
return -1;
}
std::cout << "Print linked list:" << std::endl;
while(tmp != NULL) {
std::cout << "Port Id:" << tmp->port.id << " Name:" << tmp->port.name << std::endl;
tmp = tmp->next;
}
return 0;
}
void erase_list(node_t **list) {
node_t *node = *list;
node_t *tmp = NULL;
while (node != NULL) {
if (node->next == NULL) {
delete node;
node = NULL;
*list = node;
} else {
tmp = node->next;
delete node;
node = tmp;
}
}
}
void remove_node(node_t **list, node_t *r_node) {
if (list == NULL) {
std::cout << "Cannot remove node from an empty list" << std::endl;
return;
}
if(r_node->port.id == (*list)->port.id) {
node_t *temp = *list;
std::cout << "After Removal of element ID:" << temp->port.id << std::endl;
*list = (*list)->next;
delete temp;
temp = NULL;
return;
}
node_t *current = (*list)->next;
node_t *previous = *list;
while (current != NULL) {
if(current->port.id == r_node->port.id) {
std::cout << "After Removal of element ID:" << current->port.id << std::endl;
previous->next = current->next;
delete current;
current = NULL;
return;
}
previous = current;
current = current->next;
}
}
int main() {
struct node_t *list = NULL;
node_t tmp;
create_node(&list, 1, "node1");
create_node(&list, 2, "node2");
create_node(&list, 3, "node3");
create_node(&list, 4, "node4");
create_node(&list, 5, "node5");
print_list(list);
erase_list(&list);
print_list(list);
tmp.port.id = 4;
tmp.port.name.assign("node4");
create_node(&list, 1, "node1");
create_node(&list, 2, "node2");
create_node(&list, 3, "node3");
create_node(&list, 4, "node4");
create_node(&list, 5, "node5");
remove_node(&list, &tmp);
print_list(list);
tmp.port.id = 2;
tmp.port.name.assign("node2");
remove_node(&list, &tmp);
tmp.port.name.clear();
print_list(list);
erase_list(&list);
print_list(list);
if(list) {
delete list;
}
list = NULL;
return 0;
}
因为:在美国喝酒和开车总是违法的。