我试图提示用户输入一个4位数字。然后,更换每个 数字乘以(该数字的总和加上7)模数10.然后将第一个数字与第三个数字交换,并将第二个数字与第四个数字交换。然后输出加密的数字。 因此,如果我输入1234,它应该加密到0189或输入5948并将其加密到1526
问题是我得到了9810和6251.所以,它向后看它。我很接近,但它的顺序错了。
<script type="text/javascript">
var temp;
var number;
var first;
var second;
var third;
var fourth;
var fifth;
//prompt for first number
do {
inputNumber = window.prompt("Enter only a 4 digit number");
if ((isNaN(inputNumber) || !(inputNumber.length == 4)))
window.alert("please enter a number or length of 4");
} while ((isNaN(inputNumber)) || !(inputNumber.length == 4));
//temp = inputNumber;
temp = parseInt(inputNumber);
first = temp % 10; //process each number one by one
temp = temp / 10;
second = temp % 10;
temp = temp / 10;
third = temp % 10;
temp = temp / 10;
fourth = temp % 10;
swap = first;
first = third;
third = swap;
swap = second;
second = fourth;
fourth = swap;
first = parseInt(first);
second = parseInt(second);
third = parseInt(third);
fifth = parseInt(fifth);
fourth = parseInt(fourth);
first = (first + 7) % 10
second = (second + 7) % 10
third = (third + 7) % 10
fourth = (fourth + 7) % 10
var incrypted = first * 1000 + second * 100 + third * 10 + fourth * 1;
//var incrypted = first * 1000 + second + third * 10 + fourth * 1;
document.writeln("<h1>The number " + inputNumber + " is encrypted as " + incrypted + ".</h1><br />");
</script>
答案 0 :(得分:1)
您可能最好单独处理您的号码。
var digits = inputNumber.split("");
digits[0] = (+digits[0]+7)%10;
digits[1] = (+digits[1]+7)%10;
digits[2] = (+digits[2]+7)%10;
digits[3] = (+digits[3]+7)%10;
digits.push(digits.shift());
digits.push(digits.shift());
// rotating by two results in 1234 becoming 3412, same result just more efficient!
var result = digits.join("");
答案 1 :(得分:0)
试
var num = "1234"
var digits = num.split("");
var out = [];
for(var i=0; i<digits.length; i++){
digits[i] = (parseInt(digits[i])+7)%10;
}
var end = [digits[2],digits[3],digits[0],digits[1]].join("");
console.log(end.join(""));
答案 2 :(得分:0)
随机播放功能:
function shuffle(val){
val = val + "";
function transform(num) { return (+num + 7) % 10 + ""; }
return transform(val[2]) +
transform(val[3]) +
transform(val[0]) +
transform(val[1]);
}
并通过一些验证:
function shuffle(val){
val = val + "";
if(isNaN(val)) throw "a valid number is required";
if(val.length != 4) throw "a four-digit number is required";
function transform(num) { return (+num + 7) % 10 + ""; }
return transform(val[2]) +
transform(val[3]) +
transform(val[0]) +
transform(val[1]);
}
答案 3 :(得分:0)
这归结为对%
运算符如何工作的基本误解。
MDN引用说明了%
运算符:
模数函数是将var1除以var2的整数余数。 例如,12%5返回2。
以输入1234
为例,您首先在此代码中找到10
的模1234
:
first = temp % 10;
1234
的模数为4
,因为1230
可以被10
整除,剩下4
。因此,你的逻辑逆转了事情。你可以在代码的开头改变一些东西来反转:
fourth = temp % 10; //process each number one by one
temp = temp / 10;.
third = temp % 10;.
temp = temp / 10;
second = temp % 10;
temp = temp / 10;
first = temp % 10;
还有一个问题,如果修复如上所述,代码将输出189
作为答案,因为您将数字与加法算法而不是字符串连接组合在一起,如0 + 1 + 8 + 9 = 189
中所示。将最后一行更改为如下所示并修复:
var incrypted = first + '' + second + '' + third + '' + fourth;
答案 4 :(得分:0)
这对我有用;
<script>
var vec = new Array("1","2","3","4");
for(var i = 0; i<4; ++i){
vec[i] = (+vec[i] + 7) % 10;
}
var tmp = vec[1];
vec[1] = vec[3];
vec[3] = tmp;
vec = vec.reverse();
tmp = Number(vec.splice(0,1));
vec.push(tmp);
console.log("vec:", vec);
</script>
记录vec: [0, 1, 8, 9]