Javascript If语句
我们正在尝试使用javascript数组来存储信息,以打印有生日(实际日期)的人 我们有三个数组,一个用来存储日、月和人 这就是我们现在的情况:Javascript If语句,javascript,html,css,if-statement,Javascript,Html,Css,If Statement,我们正在尝试使用javascript数组来存储信息,以打印有生日(实际日期)的人 我们有三个数组,一个用来存储日、月和人 这就是我们现在的情况: <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="css/main.css"> <!-- Look Into Connectors
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
<!--
Look Into Connectors
28 JANUARY 2015
Assignment 1
Author:
Date:
This is the index/ homepage of the website. It is the main page that a user should land on.
Filename: webpage
supporting files:
-->
<title>A Look Into IT</title>
</head>
<meta charset="UTF-8">
<meta name="description" content="This is a website that offers free information on IT">
<body>
<script type="text/javascript">
<!--
var bmonth = ["4","4","4","4", "4", "4", "4"]
var bday = ["8","6","27","22", "23", "23", "9"]
var person = ["Jesse", "john","billy" , " Buddy Dyer", "John Morgan", "Will Smith", "Jonny"]
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
for(i=0; i<=6; i++){
if (month = bmonth[i]){
if(day = bday[i]){
document.write(person[i])
}
}else{
document.write("There are no birthdays today")
}
}
//-->
</script>
</body>
</html>
调查一下
以下是输出:
2015年4月22日Jessejohnbill BuddydyerJohnmorganwillSmithJonny
它只打印所有数组信息。而不是if(month=b month[i])
和if(day=b day[i])
分别使用if(月==b月[i])
和if(日==b日[i])
您可能还希望使用三重等于
==
,但这是另一个问题。问题是您使用赋值运算符=
而不是相等(有时称为相同)运算符=
但是,您可能仍然想知道为什么会得到输出:
jessejohnbillbuddyyerjohnmorganwillsmithjonny
将对month
和day
的值求值,并且数组中的所有值求值为true。这就是为什么document.write(person[i])
正在打印所有的名字
此外,您确实需要使用分号。您确实需要更改问题标题。这不是关于if语句(动词),而是关于为什么您的代码没有按预期工作,而不是硬编码您的
for
循环停在6
您可能需要考虑添加更多的人/生日,以便您的代码更灵活。也许数组
的某些属性可以,我不知道,计算它所包含的项的数量,这样你就可以把它存储在变量中正如其他人所指出的,考察分配与平等:=
与=
或==
之间的关系。添加中断也是一个好主意
在for
语句的末尾,以停止计数