2つの変数を交換するJavaScriptプログラム

Javascript

目次

例1:一時変数の使用

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//create a temporary variable
let temp;

//swap variables
temp = a;
a = b;
b = temp;

console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

出力

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

ここに、

  1. 作成しました 臨時雇用者 の値を格納する変数 A 一時的に。
  2. の値を割り当てました bA
  3. の値 臨時雇用者 に割り当てられています b

その結果、変数の値が交換されます。

注意: この方法を使用して、文字列または他のデータ型を交換することもできます。


例2:es6(ES2015)の破壊割り当ての使用

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//using destructuring assignment
[a, b] = [b, a];

console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

出力

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

ここでは、destructuringassignmentと呼ばれる新しいes6機能があります [a, b] = [b, a]、は2つの変数の値を交換するために使用されます。 場合 [a, b] = [1, 2, 3]、の値 A になります 1 との価値 b になります 2

  • 最初に一時配列 [b, a] 創造された。 ここでの値 [b, a] になります [2, 4]
  • アレイの破棄が行われます。 [a, b] = [2, 4]

その結果、変数の値が交換されます。

あなたはについてもっと学ぶことができます 破壊する JavaScript DestructingAssignmentで。

注意:このメソッドを使用して、文字列または他のデータ型を交換することもできます。


を使用して変数の値を交換することもできます 算術 演算子。

例3:算術演算子の使用

//JavaScript program to swap two variables

//take input from the users
let a = parseInt(prompt('Enter the first variable: '));
let b = parseInt(prompt('Enter the second variable: '));

// addition and subtraction operator
a = a + b;
b = a - b;
a = a - b;

console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

出力

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

このメソッドは2つの変数のみを使用し、算術演算子を使用して変数の値を交換します + そして -

ここに、 parseInt() 使用される理由 prompt() ユーザーからの入力を文字列として受け取ります。 また、数値文字列を追加すると、文字列として動作します。 例えば、 '2' + '3' = '23'。 そう parseInt() 数値文字列を数値に変換します。

型変換の詳細については、JavaScriptの型変換にアクセスしてください。

上記のプログラムがどのように値を交換するかを見てみましょう。 当初、 A です 4 そして b です 2

  • a = a + b 値を割り当てます 4 + 2A (今 6)。
  • b = a - b 値を割り当てます 6 - 2b (今 4)。
  • a = a - b 値を割り当てる 6 - 4A (今 2)。

最終的に、 A です 2 そして b です 4

注意:算術演算子を使用できます(+-)両方の変数が数値型の場合。


例4:ビット単位のXOR演算子を使用する

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

// XOR operator
a = a ^ b
b = a ^ b
a = a ^ b

console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

出力

Enter the first variable: 4
Enter the second variable: 2
The value of a after swapping: 2
The value of b after swapping: 4

ビットごとのXOR演算子は、 true 両方のオペランドが異なる場合。 ビット単位演算子の詳細については、JavaScriptビット単位演算子にアクセスしてください。

上記のプログラムがどのように値を交換するかを見てみましょう。 当初、 A です 4 そして b です 2

  • a = a ^ b 値を割り当てます 4 ^ 2A (今 6)。
  • b = a ^ b 値を割り当てます 6 ^ 2b (今 4)。
  • a = a ^ b 値を割り当てる 6 ^ 4A (今 2)。

最終的に、 A です 2 そして b です 4

注意:このメソッドは、整数(整数)値にのみ使用できます。



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です