题目:

You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).

我的答案:

 function isValidWalk(walk) {
//insert brilliant code here
if(walk.length!=10){ return false;}
var x=0,y=0;
for(var i=0;i<walk.length;i++){
switch(walk[i]){
case "n":y++;break;
case "s":y--;break;
case "w":x++;break;
case "e":x--;break;
}
}
return x==0 && y==0; }

优秀答案:

 function isValidWalk(walk) {
function count(val) {
return walk.filter(function(a){return a==val;}).length;
}
return walk.length==10 && count('n')==count('s') && count('w')==count('e');
}
 function isValidWalk(walk) {
var dx = 0
var dy = 0
var dt = walk.length for (var i = 0; i < walk.length; i++) {
switch (walk[i]) {
case 'n': dy--; break
case 's': dy++; break
case 'w': dx--; break
case 'e': dx++; break
}
} return dt === 10 && dx === 0 && dy === 0
}

codewars--js--ten minutes walk的更多相关文章

  1. [CodeWars][JS]实现链式加法

    在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...

  2. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

  3. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  4. English words

    英语指路常用单词 the one-way street单行道traffic light红绿灯 fork road三叉路口intersection/crossroad 十字路口T road 丁字路口in ...

  5. 【oneday_onepage】——Ten Changes To Make A Difference In Your Life

    When you want to change something in your life, it can feel overwhelming. Whether it’s losing 50lbs ...

  6. 当Node.js遇见Docker

    Node.js Best Practices - How to Become a Better Developer in 2017提到的几点,我们Fundebug深有同感: 使用ES6 使用Promi ...

  7. Chapter 6 — Improving ASP.NET Performance

    https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...

  8. Build 2017 Revisited: .NET, XAML, Visual Studio

    For the next couple months we're going to revisit Build 2017, each post focusing on different aspect ...

  9. 越狱Season 1- Episode 22: Flight

    Season 1, Episode 22: Flight -Franklin: You know you got a couple of foxes in your henhouse, right? ...

随机推荐

  1. 域渗透之票据传递攻击(pass the ticket,ptt)

    票据传递攻击(PtT)是一种使用Kerberos票据代替明文密码或NTLM哈希的方法.PtT最常见的用途可能是使用黄金票据和白银票据,通过PtT访问主机相当简单. 1.ptt攻击的部分 就不是简单的N ...

  2. C入门题目

    37. 反转一个3位整数 反转一个只有3位数的整数. 样例 样例 1: 输入: number = 123 输出: 321 样例 2: 输入: number = 900 输出: 9 注意事项 你可以假设 ...

  3. ZJCTF预赛一个.py的逆向题

    #!/usr/bin/env python # -*- coding:utf-8 -*- from hashlib import sha256 def xor(a,b): result = [] fo ...

  4. Ubuntu 获取 root 用户权限并以 root权限登录

    操作步骤: 1.打开终端,使用 sudo passwd root 命令进行 Ubuntu 中密码的重置        2.切换到 /usr/share/lightdm/lightdm.conf.d 目 ...

  5. 使用visual studio 2013读取.mat文件

    现在有一个T.mat 文件需要在c++中处理然后以.mat 或是.txt形式返回 T.mat中存储了十个cell,每个cell中会有一个不等长的数组 1.以下是相关配置过程: 参考:http://we ...

  6. 1755: N相关孪生素数

    #include<stdio.h>int f(int n,int L,int R){ int ch[10000],i,j,count=0; j=1; for(i=L;i<=R;i++ ...

  7. python学习Day03

    [主要内容] 1. 编码 1. 最早的计算机编码是ASCII. 美国人创建的. 包含了英文字母(大写字母, 小写字母). 数字, 标点等特殊字符!@#$% 128个码位 2**7 在此基础上加了一位 ...

  8. 1-NoSQL介绍及Redis安装

    背景 随着互联网的不断发展和软件架构的不断复杂化,同时随着网站访问量的日渐上升,导致传统单机关系型数据库应用已经无法满足人们的需求,在高并发的场景下,频繁的数据库存取操作使得服务器压力剧增,甚至导致服 ...

  9. oracle和mysql区别

    1.本质的区别.oracle是对象关系数据库管理系统,简称ordbms.mysql是开源关系数据库关系系统,简称rdbms.Oracle是收费的.mysql是开源.免费的. 2.数据库安全性.myql ...

  10. 拒绝低效!Python教你爬虫公众号文章和链接

    本文首发于公众号「Python知识圈」,如需转载,请在公众号联系作者授权. 前言 上一篇文章整理了的公众号所有文章的导航链接,其实如果手动整理起来的话,是一件很费力的事情,因为公众号里添加文章的时候只 ...