Javascript处理日期显示问题

时间:2018-07-13 19:25:24

标签: javascript

Web开发人员经常将JavaScript用于其网站上的常见任务。在本教程中,我们将向您展示可以剪切和粘贴的十大JavaScript代码段! 在本文中,我们将介绍以下流行的脚本片段!

<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>

1 个答案:

答案 0 :(得分:1)

在两个函数中,您都需要计算下一个位置,因此我将其分为一个函数:

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

基本上,它需要一个位置,并根据指令计算下一个位置。

calculateFinalLocation

然后calculateFinalLocation可以像这样实现:

calculateFinalLocation : Location -> String -> Location
calculateFinalLocation { x, y } instructions =
    List.foldl applyInstuction { x = 0, y = 0 } <| String.toList instructions

如果您不熟悉foldl(也有foldr),则可以在以下位置进行检出,例如:https://www.brianthicks.com/guide/functional-sets/4-and-a-half/。它类似于JavaScript中的map-reduce

calculateFirstIntersection

接下来,我们可以使用calculateFirstIntersectionhttp://package.elm-lang.org/packages/elm-lang/core/5.1.1/Set)来实现Set。 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

foldl函数中,我们使用{ visited, loc, result }是因为我们不仅需要知道calculateFinalLocation中的新位置,还需要知道已经访问过的位置和结果(我们已经知道了吗?找到一个路口?)。我在这里使用了lambda函数,但是您可以根据需要将其移至常规函数。

这是它的实时代码示例:https://ellie-app.com/MDbmPFsjvwa1