codewars--js--ten minutes walk
题目:
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的更多相关文章
- [CodeWars][JS]实现链式加法
在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...
- [CodeWars][JS]实现大整数加法
问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...
- [CodeWars][JS]如何判断给定的数字是否整数
问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...
- English words
英语指路常用单词 the one-way street单行道traffic light红绿灯 fork road三叉路口intersection/crossroad 十字路口T road 丁字路口in ...
- 【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 ...
- 当Node.js遇见Docker
Node.js Best Practices - How to Become a Better Developer in 2017提到的几点,我们Fundebug深有同感: 使用ES6 使用Promi ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
- 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 ...
- 越狱Season 1- Episode 22: Flight
Season 1, Episode 22: Flight -Franklin: You know you got a couple of foxes in your henhouse, right? ...
随机推荐
- RainbowPlan团队项目-总结
博客介绍 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求在哪里 https:// ...
- FileZilla 报错“the server's certificate is unknown”
FileZilla 是非常好用的一款FTP SFTP 管理工具. 但是filezilla会报错“the server's certificate is unknown” 并且会在window中看到以下 ...
- [集训]FWT基础练习题
题意 给出n个长度为20的二进制数和数字k,每次询问给出一个二进制数,问从n个数中挑k个数(不能重复)的按位或能包含询问的组合有多少个.数字均小于等于5E5,1s. 思考 强行算出2^20个答案,再O ...
- [JSOI2008]最大数(并查集)
并查集的神奇用法:[JSOI2008]最大数 Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出 ...
- markdown时序图语法
语法 - 代表实线 , 主动发送消息,比如 request请求 > 代表实心箭头 , 同步消息,比如 AJAX 的同步请求 -- 代表虚线,表示返回消息,spring Controller re ...
- 理解Java虚拟机中的栈、堆、堆栈
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 栈区: 每个线程包含一个栈区,栈中只保存方法中(不包括对象的成员变量)的基础数据类型和自定义对象的引用(不 ...
- Windows玩转Kubernetes系列3-Centos安装K8S
以往文章参考: Windows玩转Kubernetes系列1-VirtualBox安装Centos Windows玩转Kubernetes系列2-Centos安装Docker 安装K8S yum in ...
- DjangoBBS项目功能拆分
目录 1.随机验证码 2.注册功能 3.登录功能 4.登录认证装饰器配置 5.修改密码模态框方法 6.修改头像 7.修改签名模态框方法 8.注销功能模态框 9.用户上传静态文件配置 10.图片防盗链 ...
- 用上自己的线程池,实现自己的RPC框架
package github.com.AllenDuke.rpc.customer; import github.com.AllenDuke.rpc.netty.NettyClient; import ...
- HTTP&&Fiddler教程
很不错的学习资料! HTTP http://www.cnblogs.com/TankXiao/category/415412.html http://www.cnblogs.com/TankXiao/ ...