开发者社区> 问答> 正文

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

小旋风柴进 2016-03-17 10:50:00 933

编程思路:
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>
``
已经很晚了,我没时间翻书再检查错误了,明天一早我还要去工地搬砖,恳请列位大侠直接帮忙修改成能正确运行的代码,我明天抽空拜研之
JavaScript 前端开发 数据安全/隐私保护 索引
分享到
取消 提交回答
全部回答(1)
  • 小旋风柴进
    2019-07-17 19:04:47
    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(""))
    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程