开发者社区> 问答> 正文

用javascript写的字符串加密程序运行出错

编程思路:
1、由用户输入要加密的字符串(仅允许小写字母不包含空格)
2、把字符串中的每一个字母按字母表顺序向后移动两位输出新字符串
注释,暂不考虑“溢出”问题(即用户输入了x以后的字母),代码如下:

<script>   
//用split()函数把用户输入的字符串分割并赋值给数组str[]
var str=prompt("请输入要加密的字符串:").split(""); 

//定义包含26个小写字母的alphabet数组
var alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

//定义alphabet数组的索引变量i
for(var i=0; i<=25;i++)

//定义str数组的索引变量n
for(var n=0; n<=str.length; n++)

//判断str数组中元素在alphabet中的位置,并往后移2位
if(str[n]==alphabet[i])
str[n]=alphabet[i+2]

//用jon()函数把str数组转换成字符串并赋值给newStr
var newStr=[str[n]].join("");

//输出结果
alert("加密后的新字符串是:"+newStr);
</script>
``
已经很晚了,我没时间翻书再检查错误了,明天一早我还要去工地搬砖,恳请列位大侠直接帮忙修改成能正确运行的代码,我明天抽空拜研之

展开
收起
小旋风柴进 2016-03-17 10:50:00 2423 0
1 条回答
写回答
取消 提交回答
  • var str=prompt("请输入要加密的字符串:").split("");var newStr=[];var alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];for(var n=0; n<=str.length; n++)for(var i=0; i<=25;i++)if(str[n]==alphabet[i])newStr[n]=alphabet[i+2];alert("加密后的新字符串是:"+newStr.join(""))
    2019-07-17 19:04:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
基于可信计算与加密计算 打造云上原生计算安全 立即下载
\"视频服务特色解决方案——直播连麦与点播加密 \" 立即下载
量子加密通信技术 立即下载