var x = 'red';
var y = 'yellow';
var tulip = 'red';
var sunflower = 'yellow';
if (sunflower === 'yellow')
{
console.log("The flower is sunflower");
} else
{
console.log("This is not a sunflower");
}
if (tulip === 'red')
{
console.log("The flower is tulip");
} else
{
console.log("This is not a tulip");
}
答案 0 :(得分:1)
要按颜色区分花的类型,这意味着你需要一个只有颜色属性的花卉对象,而不知道实际的花型。
因此我们创建一个花类,为它指定一种颜色,并检查花的类型。
您还可以为对象(花朵)创建一个类型属性,并使用其类型预设花朵。
这是一个简短的演示,用一种颜色创造2朵花,将它们放在一个阵列中,然后检查花的类型。
https://jsfiddle.net/55c01zde/
-00:00:01.9989999
-01:00:02
-00:00:00.9989999
答案 1 :(得分:0)
也许这会引导你朝着正确的方向前进?
var colors = ["yellow", "red"];
var flowers = ["sunflower", "tulip"];
function choice (color) {
if (color == colors[0]) {
return ("The flower is a " + flowers[0]);
} else if (color == colors[1]) {
return ("The flower is a " + flowers[1]);
}
else {
return ("This is not a flower");
}
}
document.getElementById('choice').innerHTML = choice("red");