Last active
August 29, 2015 14:02
-
-
Save Anebrithien/6ab4b5b057aeb7415afc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//作业1:摄氏度转换为华氏度 | |
var c = 39; | |
var f = 9/5 * c + 32; | |
console.log(c + "摄氏度等于" + f +"华氏度"); | |
//作业2:交换两个变量的值 | |
var alice = 8; | |
var bob = 5; | |
//如果我取巧直接console.log("交换后: " + bob + " , " + alice),静琴会拍死我吧 | |
//除了直观的使用中间变量temp以外,XOR最有趣了,就用这个了。JS也是用^来算的啊 | |
console.log("交换前: alice, bob分别有" + alice + ", " + bob + "个苹果"); | |
alice = alice ^ bob; | |
bob = bob ^ alice; | |
alice = alice ^ bob; | |
console.log("交换后: alice, bob分别有" + alice + ", " + bob + "个苹果"); | |
//作业3:JS作为计算器 | |
//那就计算某年某月某日距今多久吧,自然不作死算太久远的日期了 | |
var thatDate = new Date("6/4/1989"); | |
//现在的日期是啥? | |
var thisDate = new Date(); | |
//中间经过了多少毫秒呢? | |
var interval = thisDate.getTime() - thatDate.getTime(); | |
//日,时,分,秒,开算。应该不怎么精确吧,时刻点的准确值、时区我都不知道 | |
var days = Math.floor(interval / 86400000); | |
interval = interval - (days * 86400000); | |
var hours = Math.floor(interval / 3600000); | |
interval = interval - (hours * 3600000); | |
var minutes = Math.floor(interval / 60000); | |
interval = interval - (minutes * 60000); | |
var seconds = Math.floor(interval / 1000 ); | |
console.log("已经过去了:" + days + "天" + hours + "时" + minutes + "分" + seconds + "秒"); | |
//node.js v0.10.26 测试通过 by TsingYe @2014-6-1 | |
//BTW, 儿童节快乐! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment