Toán JavaScript: Bài tập-3 với giải pháp
Viết hàm JavaScript để chuyển đổi số thập phân sang số nhị phân, thập lục phân hoặc bát phân.
Dữ liệu kiểm tra: console.log [dec_to_bho [120, 'b']]; Console.log [dec_to_bho [120, 'H']]; Console.log [dec_to_bho [120, 'o']]; "1111000" "78" "170"
console.log[dec_to_bho[120,'B']];
console.log[dec_to_bho[120,'H']];
console.log[dec_to_bho[120,'O']];
"1111000"
"78"
"170"
Trình bày bằng hình ảnh:
Giải pháp mẫu:-
Mã HTML:
Convert a decimal number to binary, hexadecimal or octal number
Mã JavaScript:
dec_to_bho = function[n, base] {
if [n < 0] {
n = 0xFFFFFFFF + n + 1;
}
switch [base]
{
case 'B':
return parseInt[n, 10].toString[2];
break;
case 'H':
return parseInt[n, 10].toString[16];
break;
case 'O':
return parseInt[n, 10].toString[8];
break;
default:
return["Wrong input........."];
}
}
console.log[dec_to_bho[120,'B']];
console.log[dec_to_bho[120,'H']];
console.log[dec_to_bho[120,'O']];
Đầu ra mẫu:
1111000 78 170
Sơ đồ:
Bản thử trực tiếp:
Xem bút JavaScript-Math-Escise-3 của W3Resource [@W3Resource] trên CodePen.
Cải thiện giải pháp mẫu này và đăng mã của bạn thông qua Disqus
Trước đây: Viết hàm JavaScript để chuyển đổi số nhị phân thành số thập phân. Write a JavaScript function
to convert a binary number to a decimal number.
Next: Write a JavaScript function to generate a random integer.
JavaScript: Lời khuyên trong ngày
const
function checkAge[age] { if [age < 18] { const message = "Sorry, you're too young."; } else { const message = "Yay! You're old enough!"; } return message; } console.log[checkAge[21]];
Các biến với const và từ khóa được phân tích khối. Một khối là bất cứ điều gì giữa dấu ngoặc xoăn [{}]. Trong trường hợp này, dấu ngoặc xoăn của các câu lệnh if/other. Bạn không thể tham khảo một biến bên ngoài khối mà nó được khai báo, một tham chiếu HOÀN TOÀN bị ném.
Tham khảo: //bit.ly/3jfrbje
Ví dụ 1: Chuyển đổi thập phân thành nhị phân
// program to convert decimal to binary
function convertToBinary[x] {
let bin = 0;
let rem, i = 1, step = 1;
while [x != 0] {
rem = x % 2;
console.log[
`Step ${step++}: ${x}/2, Remainder = ${rem}, Quotient = ${parseInt[x/2]}`
];
x = parseInt[x / 2];
bin = bin + rem * i;
i = i * 10;
}
console.log[`Binary: ${bin}`];
}
// take input
let number = prompt['Enter a decimal number: '];
convertToBinary[number];
Đầu ra
Step 1: 9/2, Remainder = 1, Quotient = 4 Step 2: 4/2, Remainder = 0, Quotient = 2 Step 3: 2/2, Remainder = 0, Quotient = 1 Step 4: 1/2, Remainder = 1, Quotient = 0 Binary: 1001
Trong chương trình trên, người dùng được nhắc nhập số thập phân. Số được nhập bởi người dùng được truyền dưới dạng đối số cho hàm convertToBinary[]
.
Vòng lặp
dec_to_bho = function[n, base] {
if [n < 0] {
n = 0xFFFFFFFF + n + 1;
}
switch [base]
{
case 'B':
return parseInt[n, 10].toString[2];
break;
case 'H':
return parseInt[n, 10].toString[16];
break;
case 'O':
return parseInt[n, 10].toString[8];
break;
default:
return["Wrong input........."];
}
}
console.log[dec_to_bho[120,'B']];
console.log[dec_to_bho[120,'H']];
console.log[dec_to_bho[120,'O']];
0 được sử dụng cho đến khi số được nhập bởi người dùng trở thành 0.0.Giá trị nhị phân được tính bằng:
bin = bin + rem * i;
Ở đây,
dec_to_bho = function[n, base] {
if [n < 0] {
n = 0xFFFFFFFF + n + 1;
}
switch [base]
{
case 'B':
return parseInt[n, 10].toString[2];
break;
case 'H':
return parseInt[n, 10].toString[16];
break;
case 'O':
return parseInt[n, 10].toString[8];
break;
default:
return["Wrong input........."];
}
}
console.log[dec_to_bho[120,'B']];
console.log[dec_to_bho[120,'H']];
console.log[dec_to_bho[120,'O']];
1 là giá trị mô đun dec_to_bho = function[n, base] {
if [n < 0] {
n = 0xFFFFFFFF + n + 1;
}
switch [base]
{
case 'B':
return parseInt[n, 10].toString[2];
break;
case 'H':
return parseInt[n, 10].toString[16];
break;
case 'O':
return parseInt[n, 10].toString[8];
break;
default:
return["Wrong input........."];
}
}
console.log[dec_to_bho[120,'B']];
console.log[dec_to_bho[120,'H']];
console.log[dec_to_bho[120,'O']];
2 của số khi chia cho 2 & nbsp; và tôi đưa ra giá trị vị trí của số nhị phân.2 and
i gives the place value of the binary number.Ví dụ 2: Chuyển đổi thập phân sang nhị phân bằng cách sử dụng toString []
// program to convert decimal to binary
// take input
const number = parseInt[prompt['Enter a decimal number: ']];
// convert to binary
const result = number.toString[2];
console.log['Binary:' + ' ' + result];
Đầu ra
Enter a decimal number: 9 Binary: 1001
Trong chương trình trên, người dùng được nhắc nhập số thập phân. Số được nhập bởi người dùng được truyền dưới dạng đối số cho hàm convertToBinary[]
.
Vòng lặp
dec_to_bho = function[n, base] {
if [n < 0] {
n = 0xFFFFFFFF + n + 1;
}
switch [base]
{
case 'B':
return parseInt[n, 10].toString[2];
break;
case 'H':
return parseInt[n, 10].toString[16];
break;
case 'O':
return parseInt[n, 10].toString[8];
break;
default:
return["Wrong input........."];
}
}
console.log[dec_to_bho[120,'B']];
console.log[dec_to_bho[120,'H']];
console.log[dec_to_bho[120,'O']];
0 được sử dụng cho đến khi số được nhập bởi người dùng trở thành 0.