0
ユーザーがプログラムを停止させたくない場合にループを作成してループを作成する問題が発生しました。停止させたい場合は、プログラムは他の機能を継続します)。私はどのように前の機能にループバックするのですか?
私はロジックを示しながら、ベースの変換を行うための機能のリストを作成する必要があります。
- ステップ1:アルファベットのプロンプト(8進数のバイナリ/ O用B:数
- ステップ2のプロンプトベース
- ステップ3として進用/ H):文字列に変換し(例えば
"108sup10 = 1101100sup2"
&"63300268sup10 = 3C5E2A7sup16"
) - ステップ4:文の中で文字列の答えを警告する(例:基本10数は3C5E2A7(16進数)です。
- ステップ5:プロンプトを停止します。ユーザーの入力が "s"でなければ、手順1〜4を繰り返します。それ以外の場合は手順6に進みます。
- 手順6:(繰り返し)手順1の入力から入力された最大値と最小値を警告します。
ステップ1,2,3,4,6では、関数を使用することが必須です。 停止が促されたときにステップ1-4からループバックするためにSTEP5をコード化する方法を知っていますか?これには機能が必要ですか?
//prompt to get number
function getNumber() {
var myNumber;
do {
myNumber = Number(prompt("Enter an unsigned base 10 number:")); //prompt user's input to be excecuted first
} while (myNumber < 0) //loop will run again and again as long as the number is less than zero
return myNumber;
}
//prompt to get base
function getBase() {
var myBase;
do {
myBase = (prompt("Enter b for binary, o for octal and h for hexadecimal"));
} while (!(myBase == "b" || myBase == "B" || myBase == "s" || myBase == "S"|| myBase == "h" || myBase == "H")) //loop if the input is not b, s or h
return myBase;
}
//converting the base to the number
function baseConversion(number, newBase) {
var arr = [];
if (newBase == "b" || newBase == "B") {
newBase = 2;
} else if (newBase == "o" || newBase == "O") {
newBase = 8;
}else if (newBase == "h" || newBase == "H") {
newBase = 16;
}
do { //putting the each remainder at the front of the array
arr.unshift(number%newBase);
number = Math.floor(number/newBase); //round down the divided answer
} while (number>newBase-1) //loop as long as this condition holds
arr.unshift(number);
return arr;
}
//function to string the arrays
function convertToString(number, base) {
var resultString = ""
for (var i = 0; i < results.length; i++) {
var digit = results[i];
if (digit > 9) {
switch (digit) {
case 10:
digit = 'A'
break;
case 11:
digit = 'B'
break;
case 12:
digit = 'C'
break;
case 13:
digit = 'D'
break;
case 14:
digit = 'E'
break;
case 15:
digit = 'F'
break;
}
}
resultString += digit;
}
return resultString
}
//function to alert the answer statement
function alertAnswer() {
var statement = alert("Base 10 number:" + myNumber + "is" + base + "in" + myBase);
return statement;
}
//function to find the maximum number in the array
function myMax(myArray) {
var max = myArray[0];
for (var z = 0; z < myArray.length; z++) {
if (myArray[z] > max) {
max = myArray[z];
}
}
return max;
}
//function to find the minimum number in the array
function myMin(myArray) {
var min = myArray[0];
for (var z = 0; z < myArray.length; z++) {
if (myArray[z] > min) {
min = myArray[z];
}
}
return min;
}
long switch文の代わりに 'hexString = yourNumber.toString(16);'を使用することを検討してください –