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? ...
随机推荐
- git 控制版本
git:版本控制工具 1.进入想要进行版本控制项目的文件夹内 2.右击->Git Bash Here 3.初始化 git init 会多出一个.git隐藏文件夹 4.查看文件状体 git sta ...
- IDEA新建maven项目没有webapp目录解决方法
转载地址:https://www.cnblogs.com/oldzhang1222/p/10429827.html 先创建的页面修改路径 修改路径如下 添加并完善路径\src\main\webapp ...
- [DP][SA][可持久化线段树]黑红兔
源自 xyz32768 菜鸡的 FJ 省冬令营模拟赛题 原题 CF1063F Statement 给定一个长度为 \(n\) 的字符串 \(s\),仅包含小写英文字母 要从中从左往右选出若干段不相交的 ...
- .net mvc Bundle 自己配置
遇到了个坑 来和大家分享一下 1.一个空的mvc项目需要引用 System.Web.Optimization 2.然后nuget添加 microsoft ASP.NET WEB OPTIMIZATIO ...
- 巨坑练习题!!!—— Car的旅行线路
在看题目之前,请童鞋们做好心理准备╮(╯▽╰)╭ 题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游. 她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之 ...
- SpringBoot配置嵌入式Servlet容器
1).如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): ...
- 解决luajit ffi cdata引用cdata的问题
使用luajit ffi会遇到cdata引用cdata的情况.官方说明是必须手动保存所有cdata的引用,否则会被gc掉. ffi.cdef[[ struct A { int id; }; struc ...
- ubuntu “快捷方式”
1.创建.Desktop文件 sudo gedit /usr/share/applications/pycharm.desktop 2.建立pycharm.desktop [Desktop Entry ...
- nginx文件压缩
nginx文件压缩 如果我们租用了一个带宽很低的服务器,网站访问速度会很慢,这时我们可以通过让nginx开启GZIP压缩来提高网站的访问速度. 首先我们对nginx进行限速操作,限制每个连接的访问速度 ...
- learn more ,study less(二):整体性学习技术(下)
随意信息的处理 随意信息,或者内容太多.太复杂的信息,都不容易理解,它们需要不同的技术.假 如你发现联想法不能帮助你理解材料,或者需要花费的时间太长,这时候处理随意信息的方 法就很适合了. 这些处理随 ...