Chương trình JavaScript để đảo ngược một số và kiểm tra palindrome

JavaScript. Đảo ngược một sốCập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21. 50. 50 (UTC/GMT +8 giờ)

Hàm JavaScript. Bài tập-1 với lời giải

Viết hàm JavaScript đảo ngược một số
Dữ liệu mẫu và đầu ra
Ví dụ x = 32243;
Sản lượng dự kiến. 34223

Trình bày bằng hình ảnh

Chương trình JavaScript để đảo ngược một số và kiểm tra palindrome

Giải pháp mẫu

Mã HTML





Reverse a number


  



Mã JavaScript

function reverse_a_number(n)
{
	n = n + "";
	return n.split("").reverse().join("");
}
console.log(Number(reverse_a_number(32243)));

Đầu ra mẫu

34223

Giải trình
Giả sử n = 1000
Chuyển đổi một số thành một chuỗi
Mã số. -> n = n + "";
Ghi chú. Có nhiều cách khác nhau để chuyển đổi số thành chuỗi

  • Chuỗi ký tự -> str = "" + num + "";
  • Hàm tạo chuỗi -> str = String(num);
  • toString -> str = num. toString();
  • Chuỗi ký tự đơn giản -> str = "" + num;

Phương thức split() được sử dụng để chia một đối tượng Chuỗi thành một mảng các chuỗi bằng cách tách chuỗi thành các chuỗi con
Mã số. bảng điều khiển. nhật ký ('1000'. tách ra(""));
đầu ra. ["1", "0", "0", "0"]

Phương thức reverse() được sử dụng để đảo ngược một mảng tại chỗ. Phần tử mảng đầu tiên trở thành phần tử cuối cùng và phần tử cuối cùng trở thành phần tử đầu tiên
Mã số. bảng điều khiển. log(["1", "0", "0", "0"]. đảo ngược());
đầu ra. ["0", "0", "0", "1"]

Phương thức join() dùng để nối tất cả các phần tử của mảng thành một chuỗi
Mã số. bảng điều khiển. log(["1", "0", "0", "0"]. đảo ngược(). tham gia(""));
Hàm tạo Number chứa các hằng số và phương thức để làm việc với các số. Giá trị của các loại khác có thể được chuyển đổi thành số bằng cách sử dụng hàm Number()
đầu ra. 1

Sơ đồ

Chương trình JavaScript để đảo ngược một số và kiểm tra palindrome

Bản thử trực tiếp

Xem Pen JavaScript -Reverse a number-function-ex- 1 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 qua Disqus

Trước. Viết chương trình JavaScript để tìm số nguyên tố nhỏ nhất lớn hơn một số đã cho
Kế tiếp. Viết hàm JavaScript kiểm tra xem một chuỗi đã truyền có phải là palindrome hay không?

Mức độ khó của bài tập này là gì?

Dễ dàng trung bình khó

Kiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource



Theo dõi chúng tôi trên FacebookTwitter để cập nhật thông tin mới nhất.

JavaScript. Lời khuyên trong ngày

Làm trống một mảng JavaScript

Để làm trống một mảng JavaScript, chúng ta có thể đặt thuộc tính độ dài của nó thành 0
Ví dụ. chúng tôi có thể viết

const arr = ['a', 'b', 'c'];
arr.length = 0;

Sau đó, arr sẽ là một mảng không có độ dài
Tương tự như vậy, chúng ta chỉ có thể gán cho nó một mảng trống

const arr = ['a', 'b', 'c'];
arr.length = [];

Giới thiệu. https. //chút. ly/2LBj213

 



  • Xu hướng hàng tuần
  • Bài tập lập trình Java cơ bản
  • Truy vấn con SQL
  • Bài tập cơ sở dữ liệu Adventureworks
  • Bài tập cơ bản C# Sharp
  • SQL COUNT() với sự khác biệt
  • Bài tập chuỗi JavaScript
  • Xác thực biểu mẫu HTML JavaScript
  • Bài tập bộ sưu tập Java
  • hàm SQL COUNT()
  • Tham gia bên trong SQL
  • Hàm JavaScript Bài tập
  • Hướng dẫn Python
  • Bài tập mảng Python
  • Tham gia chéo SQL
  • Bài tập về mảng Sharp trong C#


Viết chương trình lấy số và đưa ra bảng màu kết quả (nếu có). Nếu phải mất hơn 1.000 lần lặp lại (bổ sung) hoặc tạo ra một bảng màu nhạt lớn hơn 4, 294, 967, 295, hãy giả sử rằng không có bảng chữ cái nào tồn tại cho số đã cho

ví dụ.  

Đầu vào. N = 195 Đầu ra. 9339 đầu vào. N = 265 Đầu ra. 45254 đầu vào. N = 196 Đầu ra. Không tồn tại đối xứng

Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp

Tiếp cận. Tạo một hàm đảo ngược và thêm để bắt đầu bằng một số, đảo ngược các chữ số của nó và thêm số đảo ngược vào ban đầu. Nếu tổng không phải là số đối xứng, hãy lặp lại quy trình này cho đến khi

C++




// C++ Program to implement reverse and add function

#include

using namespace std;

 

/* Iterative function to reverse digits of num*/

long long ________13____11_______ long // C++ Program to implement reverse and add function2

// C++ Program to implement reverse and add function3

// C++ Program to implement reverse and add function4long long // C++ Program to implement reverse and add function7

// C++ Program to implement reverse and add function4_______5_______9 #include 0

#include 1#include 2

#include 1#include 4

// C++ Program to implement reverse and add function4#include 6

// C++ Program to implement reverse and add function4_______6_______8 #include 9

#include 6

 

using1

using2

using3 using4long long // C++ Program to implement reverse and add function2

// C++ Program to implement reverse and add function3

// C++ Program to implement reverse and add function4_______6_______8 namespace1

#include 6

 

namespace3

namespace4 namespace5_______11_______ long // C++ Program to implement reverse and add function2

// C++ Program to implement reverse and add function3

// C++ Program to implement reverse and add function4long long // C++ Program to implement reverse and add function7

// C++ Program to implement reverse and add function4_______5_______9 std;6

#include 1_______9_______8

#include 1/* Iterative function to reverse digits of num*/0

 

#include 1/* Iterative function to reverse digits of num*/2

#include 1/* Iterative function to reverse digits of num*/4

#include 1/* Iterative function to reverse digits of num*/6

/* Iterative function to reverse digits of num*/7_______10_______8

 

#include 1long0

#include 1long2

#include 1long4 long5

/* Iterative function to reverse digits of num*/7_______11_______7long8long9long0

/* Iterative function to reverse digits of num*/7_______11_______2long3

#include 1#include 6

#include 1long7 long4 long9

/* Iterative function to reverse digits of num*/7_______11_______7long8reverse Digits(3reverse Digits(4

#include 1#include 6

// C++ Program to implement reverse and add function4#include 6

#include 6

 

// C++ Program to implement reverse and add function00

// C++ Program to implement reverse and add function01 // C++ Program to implement reverse and add function02

// C++ Program to implement reverse and add function3

// C++ Program to implement reverse and add function4_______5_______05

// C++ Program to implement reverse and add function4_______5_______07

// C++ Program to implement reverse and add function4_______6_______8 // C++ Program to implement reverse and add function10

#include 6

Java




// C++ Program to implement reverse and add function12

// C++ Program to implement reverse and add function13 // C++ Program to implement reverse and add function14 // C++ Program to implement reverse and add function15

// C++ Program to implement reverse and add function4/* Iterative function to reverse digits of num*/

// C++ Program to implement reverse and add function4long // C++ Program to implement reverse and add function20long // C++ Program to implement reverse and add function2

// C++ Program to implement reverse and add function4_______5_______3

#include 1long // C++ Program to implement reverse and add function27_______5_______28long3

#include 1// C++ Program to implement reverse and add function9 // C++ Program to implement reverse and add function32_______5_______28// C++ Program to implement reverse and add function34

/* Iterative function to reverse digits of num*/7_______5_______36// C++ Program to implement reverse and add function37 // C++ Program to implement reverse and add function38_______5_______37long3

/* Iterative function to reverse digits of num*/7_______5_______42// C++ Program to implement reverse and add function37long3

#include 1#include 6

#include 1_______6_______8 #include 9

// C++ Program to implement reverse and add function4#include 6

 

// C++ Program to implement reverse and add function4_______5_______53

// C++ Program to implement reverse and add function54_______5_______55

// C++ Program to implement reverse and add function4// C++ Program to implement reverse and add function57 using4long // C++ Program to implement reverse and add function2

// C++ Program to implement reverse and add function4_______5_______3

#include 1_______6_______8 // C++ Program to implement reverse and add function65

// C++ Program to implement reverse and add function4#include 6

 

// C++ Program to implement reverse and add function4namespace3

// C++ Program to implement reverse and add function4namespace4 namespace5long // C++ Program to implement reverse and add function2

// C++ Program to implement reverse and add function4_______5_______3

#include 1long // C++ Program to implement reverse and add function27_______5_______28long3

#include 1_______5_______9 // C++ Program to implement reverse and add function84

/* Iterative function to reverse digits of num*/7_______9_______8

/* Iterative function to reverse digits of num*/7_______5_______88

 

/* Iterative function to reverse digits of num*/7_______10_______2

/* Iterative function to reverse digits of num*/7_______10_______4

/* Iterative function to reverse digits of num*/7_______10_______6

// C++ Program to implement reverse and add function95_______10_______8

 

/* Iterative function to reverse digits of num*/7long0

/* Iterative function to reverse digits of num*/7_______11_______2

/* Iterative function to reverse digits of num*/7_______11_______4 long5

// C++ Program to implement reverse and add function95_______6_______05

// C++ Program to implement reverse and add function95_______11_______2long3

/* Iterative function to reverse digits of num*/7_______6_______6

/* Iterative function to reverse digits of num*/7_______11_______7 long4 #include 14

// C++ Program to implement reverse and add function95_______6_______16reverse Digits(3reverse Digits(4

/* Iterative function to reverse digits of num*/7_______6_______6

#include 1#include 6

// C++ Program to implement reverse and add function4#include 6

 

// C++ Program to implement reverse and add function4#include 26

// C++ Program to implement reverse and add function4// C++ Program to implement reverse and add function13 #include 29 namespace4 #include 31

// C++ Program to implement reverse and add function4_______5_______3

#include 1#include 35_______6_______36 #include 37

#include 1#include 39

#include 1#include 41

// C++ Program to implement reverse and add function4#include 6

#include 6

Python3




#include 45

 

#include 46

#include 47 #include 48

// C++ Program to implement reverse and add function4#include 50_______6_______51 // C++ Program to implement reverse and add function28

// C++ Program to implement reverse and add function4// C++ Program to implement reverse and add function9 // C++ Program to implement reverse and add function32_______5_______28#include 57

#include 1#include 50_______6_______51 #include 50_______6_______62 // C++ Program to implement reverse and add function37 #include 64 /* Iterative function to reverse digits of num*/6#include 66 // C++ Program to implement reverse and add function37

Làm cách nào để kiểm tra xem số có phải là palindrome trong JavaScript không?

Thuật toán đối xứng .
Nhận các chuỗi hoặc số từ người dùng
Lấy một biến tạm thời chứa các số
Đảo ngược số đã cho
So sánh số ban đầu với số đảo ngược
Nếu số tạm thời và số ban đầu giống nhau, thì số hoặc chuỗi đó là Palindrome

Làm cách nào để viết chương trình ac đảo ngược một số và kiểm tra xem nó có phải là palindrome hay không trong Python?

n=int(input("Nhập số. ")) temp=n rev=0 while(n>0). đào=n%10 rev=rev*10+dig n=n//10 if(temp==rev). print("Số này là một số đối xứng. ") khác. print("Số này không phải là một số đối xứng. ") 1. Trước tiên, người dùng phải nhập giá trị của số nguyên và lưu trữ nó trong một biến

Làm cách nào để tạo một chương trình JavaScript để kiểm tra xem chuỗi có phải là màu nhạt hay không?

chiều dài) % 2 === 0) { ccount = (cstr. chiều dài)/2; . chiều dài === 1) { bảng điều khiển. log("Entry là một palindrome. "); return true; } else { // Nếu độ dài của chuỗi là số lẻ thì bỏ qua số ký tự ở giữa = (cstr