回答編集履歴

1

omake

2022/12/12 16:01

投稿

Cocode
Cocode

スコア2314

test CHANGED
@@ -92,3 +92,61 @@
92
92
  - [分割代入 - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)
93
93
  - [Array.prototype.flatMap() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)
94
94
  - [.formatDate() | Class Utilities | Apps Script - Google Developers](https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate,-timezone,-format)
95
+
96
+ ---
97
+
98
+ ### おまけ:アロー関数を使わずにかく
99
+ `searchNeedleInHaystack3()`関数の以下の1行を、アロー関数を使わずに、かみくだいた書き方で3種類ほどご用意しました。
100
+ 理解の助けになれば幸いです。
101
+
102
+ ```javascript
103
+ const result = needles.map(n => haystack.some(h => h - n === 0) ? ['●'] : ['']);
104
+ ```
105
+
106
+ ##### アロー関数やめてみる
107
+ ```javascript
108
+ const result = needles.map(function(n) {
109
+ const isExists = haystack.some(function(h) {
110
+ return h - n === 0;
111
+ });
112
+
113
+ return isExists === true ? ['●'] : [''];
114
+ });
115
+ ```
116
+
117
+ ##### 三項演算子やめてみる
118
+ ```javascript
119
+ const result = needles.map(function(n) {
120
+ const isExists = haystack.some(function(h) {
121
+ return h - n === 0;
122
+ });
123
+
124
+ if (isExists === true) {
125
+ return ['●'];
126
+ } else {
127
+ return [''];
128
+ }
129
+ });
130
+ ```
131
+
132
+ ##### `.map()`さえやめてみる
133
+ ```javascript
134
+ const result = [];
135
+ for (let n=0; n<needles.length; n++) {
136
+ let flag;
137
+
138
+ for (let h=0; h<haystack.length; h++) {
139
+ const diff = haystack[h] - needles[n];
140
+ if (diff === 0) {
141
+ flag = true;
142
+ break;
143
+ }
144
+ }
145
+
146
+ if (flag === true) {
147
+ result.push(['●']);
148
+ } else {
149
+ result.push(['']);
150
+ }
151
+ }
152
+ ```