JavaScriptでの2つの文字列の2回の結合
2つの文字列を受け取るJavaScript関数を作成する必要があります。最初の文字列の最初の2単語、2番目の文字列の次の2単語、次に最初、次に2番目というように新しい文字列を作成して返します。
例:文字列が-
の場合const str1 = 'Hello world'; const str2 = 'How are you btw';
その場合、出力は-
になります。const output = 'HeHollw o arwoe rlyodu btw';
したがって、この関数のコードを書いてみましょう-
例
このためのコードは-
になりますconst str1 = 'Hello world';
const str2 = 'How are you btw';
const twiceJoin = (str1 = '', str2 = '') => {
let res = '', i = 0, j = 0, temp = '';
for(let ind = 0; i < str1.length; ind++){
if(ind % 2 === 0){
temp = (str1[i] || '') + (str1[i+1] || '')
res += temp;
i += 2;
}else{
temp = (str2[j] || '') + (str2[j+1] || '')
res += temp;
j += 2;
}
};
while(j < str2.length){
res += str2[j++];
};
return res;
};
console.log(twiceJoin(str1, str2)); 出力
コンソールの出力は-
になりますHeHollw o arwoe rlyodu btw
-
JavaScriptConst
JavaScriptのconst宣言は、他の値に再割り当てしたり、後で再宣言したりできない変数を作成します。 ES2015で導入されました。 以下はJavaScriptconst宣言のコードです- 例 <!DOCTYPE html> <html> <head> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } </sty
-
JavaScriptで2つの配列を結合する方法は?
以下は、JavaScriptで2つの配列を結合するコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> &