简体   繁体   English

为什么我的函数让 Ghrome Chrome 停止工作?(JavaScript)

[英]Why my function make Ghrome Chrome stop working ?(JavaScript)

I've been training on the CodeWars site and I should have done following exercise:我一直在 CodeWars 网站上接受培训,我应该做以下练习:

"Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_')." “完成解决方案,以便将字符串分成两个字符对。如果字符串包含奇数个字符,则它应该用下划线 ('_') 替换最后一对中缺少的第二个字符。”

BUT my problem is that I've got nothing from the output on the site and when I tried to test my function in Google Chrome console it's just don't return or print anything and the whole browser just stop working...What did I do wrong?但我的问题是,我从网站上的输出中一无所获,当我尝试在 Google Chrome 控制台中测试我的功能时,它只是不返回或打印任何内容,整个浏览器就停止工作了……我做了什么做错了? There are no "infinite" conditions... What may cause the problem?没有“无限”条件......可能导致问题的原因是什么?

Screenshot of output from the CodeWars CodeWars 的输出截图

 function solution(str){ var result = []; if((str.length % 2) === 0) { for (var i = 0; i < str.length; i + 2){ result.push(str.substr(i, 2)); } } else { for (var i = 0; i < (str.length); i + 2){ result.push(str.substr(i, 2)); } result.push(str.substr(str.length - 1, 1) + '_'); }; return result; };

Your for loops are wrong.你的 for 循环是错误的。 They do not increment i .它们不会增加i Use i += 2 .使用i += 2

for (let i = 0; i < (str.length); i += 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM