问题描述:

A happy number is a number defined by the following process: starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.

Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers) (Wikipedia).

For example number 7 is happy because after a number of steps the computed sequence ends up with a 1: 7, 49, 97, 130, 10, 1

while 3 is not, and would give us an infinite sequence: 3, 9, 81, 65, 61, 37, 58, 89, 145, 42, 20, 4, 16,3 7, 58, 89, 145, 42, 20, 4, 16, 37, ...

Write a function that takes n as parameter and return true if and only if n is an happy number.

Happy coding!

我的思路:

当求得sum=1时结束循环(sum=当前数各个位的平方之和)。对于有的数不是happy数,这个while就得一直做下去,设置一个flag判断是都为无穷大数(Number.POSITIVE_INFINITY),若是,则直接返回false。但是这样仍然超时。

我的答案:

function isHappy(n) {
// Good Luck
var sum=0;
var flag=1;
while(sum!=1){
var a=n.toString().split("");
for(var i=0;i<a.length;i++){
sum=sum+Math.pow((+a[i]),2);
}
n=sum;
flag+=1;
if(flag=="Infinity"){return false;}
}
return true;
}

优秀答案:

(1)通过判断n是不是循环出现,跳出while循环

function isHappy(n) {
let arr = []
while (n !== 1 && arr.indexOf(n) === -1) {
arr.push(n);
n = n.toString().split('').map(x => Math.pow(Number(x), 2)).reduce((p, n) => p + n, 0);
}
return n ==1?true:false;
}

哈哈哈!

codewars--js--Happy numbers++无穷大判断的更多相关文章

  1. 动态加载JS过程中如何判断JS加载完成

    在正常的加载过程中,js文件的加载是同步的,也就是说在js加载的过程中,浏览器会阻塞接下来的内容的解析.这时候,动态加载便显得尤为重要了,由于它是异步加载,因此,它可以在后台自动下载,并不会妨碍其它内 ...

  2. JS中,如何判断一个被转换的数是否是NaN

    var x="abc"; //isNaN()函数判断是否是NaN if (isNaN(parseInt(x))) { alert("非数字"); } else{ ...

  3. 原生 js基础常用的判断和循环

    原生 js基础常用的判断和循环 以下部分是个人实践及和搜集的资料: 最常用的if判断语句: if (/* 条件表达式 */){ // 成立执行语句 } else { // 否则执行语句 } 原生js的 ...

  4. js一些if语句判断条件为fasle的情况

    js一些if语句判断条件为fasle的情况 之前有写一个if判断条件产生的bug,当时写逻辑处理数据是在后台给接口之前,所以自己拟定了字段值为number类型的0或者1来进行判断,最后接口出来的时候是 ...

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

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

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

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

  7. JS魔法堂:判断节点位置关系

    一.前言 在polyfill querySelectorAll 和写弹出窗时都需要判断两个节点间的位置关系,通过jQuery我们可以轻松搞定,但原生JS呢?下面我将整理各种判断方法,以供日后查阅. 二 ...

  8. JS - IE or not:判断是否为IE浏览器方法

    问题:使用JS判断是否为IE浏览器 方法: 1.IE='\v'=='v'  (失败!) if('\v'=='v') // true only in IE 2.IE=(!+"\v1" ...

  9. 模板引擎doT.js介绍及如何判断对象为空、如何嵌套循环···

    doT.js 灵感来源于搜寻基于 V8 和 Node.js ,强调性能,最快速最简洁的 JavaScript 模板函数 引入 javascript 文件: <script type=" ...

随机推荐

  1. python小知识点总结

    小知识点总结 1.python2和python3的区别   python2 python3 默认编码 ascii utf-8 input() raw_input() input() print 可以不 ...

  2. 美食家App开发日记4

    研究了卡片式布局中的Recyclerview的用法,但是调试了很长时间,导入包总是有问题,一到手机上运行就会闪退.还是在网上查了很多方法,很不开心我还是解决不了.

  3. 个人任务day7

    今日计划: 整合程序,排除错误. 昨日成果: 写注册界面.

  4. basic-pentesting-1 靶机提权

    原文地址:https://www.payload.com.cn/   basic-pentesting-1 下载地址: https://www.vulnhub.com/entry/basic-pent ...

  5. Bate冲刺博客(3次)

    Bate冲刺博客 课程介绍 课程 (https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience) 作业要求 https://www ...

  6. 团队项目——Alpha发布1

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/homework 这个作业要求在哪里 https ...

  7. jmeter使用—响应断言

    断言的作用:一个HTTP请求发出去,怎么判断执行的任务是否成功呢?通过检查服务器响应数据,是否返回预期想要的数据,如果是,判断任务成功,反之任务失败. 1.添加断言:选中一个取样器,右键->添加 ...

  8. Shell环境变量文件

    /etc/profile 系统级的初始化环境变量文件,由登录Shell调用执行 /etc/profile.d 当/etc/profile运行时,会调用该目录下的一些脚本 /etc/bashrc 每个交 ...

  9. ubuntu-14.04.6配置IP

    配置环境与要求: 网卡列表如下: eth0:DHCP模式 eth1:静态模式 网络概况与要求: 192.168.2.0/24为外网(获取网络资源) 10.5.1.0/24为内网(终端服务管理) 系统默 ...

  10. 初识Redis,看这一篇就够了

    环境的搭建和安装网上有很多教程,在这里就不再重复了. 1. Redis是什么? Redis(全称:Remote Dictionary Server 远程字典服务)是一个开源的使用ANSI C语言编写. ...