Javascript处理日期显示问题
Web开发人员经常使用JavaScript来完成他们网站上的常见任务。在本教程中,我们将向您展示通过剪切和粘贴即可在网页上使用的前10个JavaScript片段! 在本文中,我们将介绍以下流行的脚本片段Javascript处理日期显示问题,javascript,Javascript,Web开发人员经常使用JavaScript来完成他们网站上的常见任务。在本教程中,我们将向您展示通过剪切和粘贴即可在网页上使用的前10个JavaScript片段! 在本文中,我们将介绍以下流行的脚本片段 <SCRIPT LANGUAGE="JavaScript"> var now = new Date(); var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturd
<SCRIPT LANGUAGE="JavaScript">
var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
}
today = days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())) ;
document.write(today);
</script>
var now=新日期();
var days=新数组(“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”);
var months=新数组(“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”);
var date=((now.getDate()在这两个函数中,您都需要计算下一个位置,因此我将其分为一个函数:
applyInstuction : Char -> Location -> Location
applyInstuction instruction loc =
case instruction of
'>' ->
{ loc | x = loc.x + 1 }
'^' ->
{ loc | y = loc.y + 1 }
'<' ->
{ loc | x = loc.x - 1 }
'v' ->
{ loc | y = loc.y - 1 }
_ ->
loc
如果您不熟悉foldl
(还有foldr
)-您可以在这里查看它,例如:。它类似于JavaScript中的map reduce
calculateFirstIntersection
接下来,我们可以使用Set
()实现calculateFirstIntersection
。Set将存储我们已经访问过的位置。
代码如下:
calculateFirstIntersection : String -> Maybe Location
calculateFirstIntersection instructions =
.result <|
List.foldl
(\instruction { visited, loc, result } ->
case result of
Nothing ->
let
nextVisited =
Set.insert ( loc.x, loc.y ) visited
nextLoc =
applyInstuction instruction loc
in
if Set.member ( loc.x, loc.y ) visited then
{ visited = nextVisited
, loc = nextLoc
, result = Just loc
}
else
{ visited = nextVisited
, loc = nextLoc
, result = Nothing
}
_ ->
{ visited = visited
, loc = loc
, result = result
}
)
{ visited = Set.empty, loc = { x = 0, y = 0 }, result = Nothing }
<|
String.toList instructions
calculateFirstIntersection:String->Maybe Location
calculateFirstIntersection指令=
.结果
案例结果
没有->
让
下一次访问=
设置插入(位置x、位置y)已访问
下一个=
应用指令loc
在里面
如果访问了Set.EMBER(位置x、位置y),则
{visted=nextvisted
,loc=nextLoc
,结果=仅限loc
}
其他的
{visted=nextvisted
,loc=nextLoc
,结果=无
}
_ ->
{visted=visted
,loc=loc
,result=结果
}
)
{visted=Set.empty,loc={x=0,y=0},result=Nothing}
不要诋毁帖子,即使是你创建的帖子。
calculateFirstIntersection : String -> Maybe Location
calculateFirstIntersection instructions =
.result <|
List.foldl
(\instruction { visited, loc, result } ->
case result of
Nothing ->
let
nextVisited =
Set.insert ( loc.x, loc.y ) visited
nextLoc =
applyInstuction instruction loc
in
if Set.member ( loc.x, loc.y ) visited then
{ visited = nextVisited
, loc = nextLoc
, result = Just loc
}
else
{ visited = nextVisited
, loc = nextLoc
, result = Nothing
}
_ ->
{ visited = visited
, loc = loc
, result = result
}
)
{ visited = Set.empty, loc = { x = 0, y = 0 }, result = Nothing }
<|
String.toList instructions