codewars--js--Happy numbers++无穷大判断
问题描述:
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 Luckvar 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; }
哈哈哈!
转载于:https://www.cnblogs.com/hiluna/p/8916729.html
codewars--js--Happy numbers++无穷大判断相关推荐
- js根据身份证号码判断性别和年龄
/**这是一个超实用的用js根据身份证号码判断性别男女的特效代码,详细看下面的*/ /** 根据身份证号码判断性别 15位身份证号码:第7.8位为出生年份(两位数),第9.10位为出生月份,第11.1 ...
- js在上传图片前判断大小
js在上传图片前判断大小 这个可以用javascript实现,效果还可以,主要是因为javascript里居然有Image对象,可以取得其属性 <script language=javascri ...
- js判断wifi_使用JS在浏览器中判断当前网络连接状态的几种方法
使用JS在浏览器中判断当前网络状态的几种方法如下: 1. navigator.onLine 2. ajax请求 3. 获取网络资源 4. bind() 1. navigator.onLine 通过na ...
- [完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器...
[完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器 原文:[完美]原生JS获取浏览器版本判 ...
- [js] 写一个方法判断数组内元素是否全部相同
[js] 写一个方法判断数组内元素是否全部相同 const isSameArray = function (array) {if (Array.isArray(array)) {return new ...
- 基于asp.net + easyui框架,js实现上传图片之前判断图片格式,同时实现预览,兼容各种浏览器+下载...
2019独角兽企业重金招聘Python工程师标准>>> 最近在做图片上传的一个前台页面,上传图片功能虽然很简单,但是需要我们学习的地方很多.在上传图片之前验证图片的格式,并同时实现预 ...
- JS中,如何判断一个被转换的数是否是NaN
var x="abc";//isNaN()函数判断是否是NaNif (isNaN(parseInt(x))) {alert("非数字");} else{aler ...
- js布尔类型+数字判断_C ++中的布尔数据类型
js布尔类型+数字判断 In this article, we'll take a look at the Bool datatype in C++. 在本文中,我们将介绍C ++中的Bool数据类型 ...
- js对时间日期判断今天昨天明天
js对时间日期判断今天昨天明天 let time = function(date) {let time_str = "";if (new Date(date).getDate() ...
最新文章
- 端到端TVM编译器(下)
- docker配置cdn-容器内可以通过域名访问
- 我给 Apache 顶级项目提了个 Bug
- python(numpy,pandas1)——numpy(ndim,shape,size,zero, empty, arange, linspace)
- Linux配置编程环境+云服务器上传文件
- LeetCode算法入门- Search Insert Position -day19
- url中 斜杠如何传输_如何在父子页面中传输数据(layer)
- 信息学奥赛C++语言: 数字三角形1
- 38动感菜单 38 jQuery And CSS Drop Down Multi Level Menu Solutions
- 对无线编码缓存的一些理解
- python2 爬虫value_53. Python 爬虫(2)
- LY.JAVA面向对象编程.内存图
- 软件的接口设计图_软件工程中的分析、设计与实例
- cpda数据分析师证书含金量高吗
- 梦熊杯-十二月月赛-白银组题解-A.自由
- 域服务器文件备份,怎么备份域服务器?
- Spring框架的作用
- 微信小程序播放背景音乐,关闭背景音乐的操作
- Python+爬虫技术获取斗鱼直播图片(整理)
- 2022继电保护判断题及答案