function getNumber():
提示用户,直到输入无符号整数。将无符号整数返回给调用代码。
function getBase():
在输入有效基数之前提示用户。唯一有效的是“b”或“B”或“o”或“O”或“h”或“H”。返回调用代码的基础。
function baseConversion(number, newBase):
返回转换为newBase的基数10的字符串版本。例如,如果number为255且newBase为16,则返回“FF”,因为255是基数16的FF。
function getNumber(callback) {
var num = prompt("Enter an unsigned base 10 number");
if (num >= 0) {
callback();
return num;
}
}
function getBase() {
var base = prompt("Enter b for binary, o for octal, or h for hexadecimal");
return base;
}
function baseConversion(num, base) {
var num = getNumber(function() {
var base = getBase();
});
// problem here is that after calling the above two methods the program stops
//does not return to the calling function to continue excuting
if (base == "b" || base == "B") {
var bin = [];
while (num > 0) {
bin.unshift(num % 2);
num >>= 1; // basically /= 2 without remainder if any
}
alert("That decimal in binary is " + bin.join(''));
return bin;
}
if (base == "o" || base == "O") {
var oct = [];
while (num > 0) {
oct.unshift(num % 8);
num = ~~ (num / 8);
}
alert("That decimal in octal is " + oct.join(''));
return oct;
}
if (base == "h" || base == "H") {
var hex = [];
while (num > 0) {
x = (num % 16);
if (x > 9) {
if (x == 10) {
hex.unshift("A")
}
if (x == 11) {
hex.unshift("B")
}
if (x == 12) {
hex.unshift("C")
}
if (x == 13) {
hex.unshift("D")
}
if (x == 14) {
hex.unshift("E")
}
if (x == 15) {
hex.unshift("F")
}
}
if (x <= 9) {
hex.unshift(x)
}
num = Math.floor((num / 16));
}
alert("That decimal in hexadecimal is " + hex.join(''));
return hex;
}
}
当我尝试运行它时,代码停止工作。任何帮助将不胜感激
答案 0 :(得分:1)
这个回调系统的脚本过于复杂,但主要的错误是你访问了一个不存在的变量 base :
function baseConversion(num,base){
var num = getNumber(function() {
var base = getBase(); // this variable only exists within this function
});
// ... not here, where *base* is the argument passed to the function.
修复:
function baseConversion(num,base){
// now the parameter *base* is updated:
var num = getNumber(function() {
base = getBase();
});
// ... and still has that same value here.
建议的改进:
由于函数 baseConversion 已接受 num 和 base 值作为参数,因此在该函数内请求输入是错误的地方。相反,你应该在调用函数之前获得输入。
而不是回调方法,以线性方式请求输入,甚至更好:创建一个输入表单,用户可以选择填写顺序和何时启动函数:
<input type="text" id="num"><br>
<input type="text" id="base"><br>
<button id="convert">Convert!</button>
这比使用prompt
要好得多,但是好吧,如果是你对prompt
的转让,我想你最好听一听:)。
假设您不允许在代码中使用toString(base)
,这会使其变得非常微不足道,以下是符合要求的内容:
function getNumber () {
var num, input;
do {
input = prompt("Enter an unsigned base 10 number");
num = parseInt(input);
} while (isNaN(num) || num < 0 || num+''!==input);
return num;
}
function getBase () {
var base;
do {
base = prompt("Enter b for binary, o for octal, or h for hexadecimal");
} while ('bBoOhH'.indexOf(base) === -1);
return base;
}
function baseConversion(num, base){
var baseNum = base === 'b' ? 2 :
base === 'o' ? 8 : 16;
var res = [], digit;
do {
digit = num % baseNum;
res.unshift('0123456789ABCDEFGH'.charAt(digit));
num = (num - digit) / baseNum;
} while (num > 0);
return res.join('');
}
var num = getNumber();
var base = getBase();
var result = baseConversion(num, base);
alert('result is ' + result);
答案 1 :(得分:0)