Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

javascript - 为什么我的功能使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; };   ask by Evgeny Artemov translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...