質問編集履歴

1

実際のjs,htmlコードを加えた

2017/08/26 23:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,367 @@
48
48
 
49
49
 
50
50
 
51
-
51
+ 追記:
52
+
53
+ 上記のコードは今回の説明用に実際のコードを簡略化して書いたものです。
54
+
55
+ 実際のコード(js,html)は以下の通りです(jsは一部のみ)。
56
+
57
+ 実際に使用している画像は多いのですが、
58
+
59
+ まだ配列を画像表示に応用することができていないため、
60
+
61
+ 配列に代入しようとしている画像は、test0(mypic[0])とtest1(mypic[1])のみです。
62
+
63
+
64
+
65
+ ```js
66
+
67
+ function start(){
68
+
69
+
70
+
71
+ /* 写真の読み込み */
72
+
73
+
74
+
75
+ var mypic = new Array(2);
76
+
77
+ for(var i=0;i<2;i++){
78
+
79
+ mypic += "<img src='images/test" + i + ".jpg'>";
80
+
81
+ }
82
+
83
+
84
+
85
+ /* 朝食の決定 */
86
+
87
+
88
+
89
+ var asama = ['ごはん','パン'];
90
+
91
+ var asamacal = [300,400];
92
+
93
+ var max = 1, goukei = 0, asameshi = "";
94
+
95
+
96
+
97
+ asameshi += '朝 主菜<br>';
98
+
99
+ food = Math.floor(Math.random() * (max + 1));
100
+
101
+ asamain = food /* 主菜が何かを表す */
102
+
103
+ asameshi += asama[food] + '&nbsp;' + asamacal[food] + 'kカロリー<br>';
104
+
105
+ goukei = asamacal[food];
106
+
107
+
108
+
109
+ if (asamain == 0){ /* ごはんかパンかによっておかずが異なる。以下同様 */
110
+
111
+ /* asameshi += '<img src="images/gohan.jpg"><br>'; ここでご飯の画像を挿入 */
112
+
113
+ asameshi += mypic[0];
114
+
115
+ var asasub1 = ['焼き魚','納豆'];
116
+
117
+ var asasub1cal = [200,100];
118
+
119
+ var max = 1; /* 以下、何回もmaxに任意の数値を代入するのは、メニューの数が増えることを想定してのもの */
120
+
121
+ asameshi += '<br>朝 おかず1<br>';
122
+
123
+ food = Math.floor(Math.random() * (max + 1));
124
+
125
+ asameshi += asasub1[food] + '&nbsp;' + asasub1cal[food] + 'kカロリー<br>';
126
+
127
+ if (food == 0){
128
+
129
+ asameshi += '<img src="images/sakana.jpg"><br>';
130
+
131
+ } else {
132
+
133
+ asameshi += '<img src="images/nattou.jpg"><br>';
134
+
135
+ }
136
+
137
+ goukei = goukei + asasub1cal[food];
138
+
139
+ } else {
140
+
141
+ /* asameshi += '<img src="images/pan.jpg"><br>'; ここでパンの画像を挿入 */
142
+
143
+ asameshi += mypic[1];
144
+
145
+ var asasub2 = ['ハムエッグ','ソーセージ'];
146
+
147
+ var asasub2cal = [250,300];
148
+
149
+ var max = 1;
150
+
151
+ asameshi += '<br>朝 おかず1<br>';
152
+
153
+ food = Math.floor(Math.random() * (max + 1));
154
+
155
+ asameshi += asasub2[food] + '&nbsp;' + asasub2cal[food] + 'kカロリー<br>';
156
+
157
+ if (food == 0){
158
+
159
+ asameshi += '<img src="images/hamegg.jpg"><br>';
160
+
161
+ } else {
162
+
163
+ asameshi += '<img src="images/sausage.jpg"><br>';
164
+
165
+ }
166
+
167
+ goukei = goukei + asasub2cal[food];
168
+
169
+ }
170
+
171
+
172
+
173
+ if (asamain == 0){
174
+
175
+ var asasmall1 = ['味噌汁','おしんこ'];
176
+
177
+ var asasmall1cal = [50,50];
178
+
179
+ var max = 1;
180
+
181
+ asameshi += '<br>朝 おかず2<br>';
182
+
183
+ food = Math.floor(Math.random() * (max + 1));
184
+
185
+ asameshi += asasmall1[food] + '&nbsp;' + asasmall1cal[food] + 'kカロリー<br>';
186
+
187
+ if (food == 0){
188
+
189
+ asameshi += '<img src="images/misoshiru.jpg"><br>';
190
+
191
+ } else {
192
+
193
+ asameshi += '<img src="images/oshinko.jpg"><br>';
194
+
195
+ }
196
+
197
+ asagoukei = goukei + asasmall1cal[food];
198
+
199
+ } else {
200
+
201
+ var asasmall2 = ['チーズ','レタス'];
202
+
203
+ var asasmall2cal = [200,50];
204
+
205
+ var max = 1;
206
+
207
+ asameshi += '<br>朝 おかず2<br>';
208
+
209
+ food = Math.floor(Math.random() * (max + 1));
210
+
211
+ asameshi += asasmall2[food] + '&nbsp;' + asasmall2cal[food] + 'kカロリー<br>';
212
+
213
+ if (food == 0){
214
+
215
+ asameshi += '<img src="images/cheese.jpg"><br>';
216
+
217
+ } else {
218
+
219
+ asameshi += '<img src="images/leaf.jpg"><br>';
220
+
221
+ }
222
+
223
+ asagoukei = goukei + asasmall2cal[food];
224
+
225
+ }
226
+
227
+
228
+
229
+ asameshi += '<br>合計&nbsp;' + asagoukei + 'kカロリー';
230
+
231
+
232
+
233
+ /* 昼食の決定 */
234
+
235
+
236
+
237
+ var hiruma = ['ごはん','パン'];
238
+
239
+ var hirumacal = [300,400];
240
+
241
+ var max = 1, goukei = 0, hirumeshi = "";
242
+
243
+
244
+
245
+ hirumeshi += '昼 主菜<br>';
246
+
247
+
248
+
249
+ /* 朝と昼の主菜が同じでも構わない場合は、ここを採用。その場合、この下のifから始まるコードは不採用
250
+
251
+ food = Math.floor(Math.random() * (max + 1));
252
+
253
+ hirumain = food
254
+
255
+ hirumeshi += hiruma[food] + '&nbsp;' + hirumacal[food] + 'kカロリー<br>';
256
+
257
+ goukei = goukei + hirumacal[food];
258
+
259
+ */
260
+
261
+
262
+
263
+ if (asamain ==0){
264
+
265
+ hirumain = 1; /* もし朝の主菜がごはんなら、昼は強制的にパン*/
266
+
267
+ hirumeshi += hiruma[1] + '&nbsp;' + hirumacal[1] + 'kカロリー<br>';
268
+
269
+ goukei = goukei + hirumacal[1];
270
+
271
+ } else {
272
+
273
+ hirumain = 0;
274
+
275
+ hirumeshi += hiruma[0] + '&nbsp;' + hirumacal[0] + 'kカロリー<br>';
276
+
277
+ goukei = goukei + hirumacal[0];
278
+
279
+ }
280
+
281
+
282
+
283
+ if (hirumain == 0){
284
+
285
+ hirumeshi += '<img src="images/gohan.jpg"><br>';
286
+
287
+ var hirusub1 = ['鳥のから揚げ','野菜炒め'];
288
+
289
+ var hirusub1cal = [400,250];
290
+
291
+ var max = 1;
292
+
293
+ hirumeshi += '<br>昼 おかず1<br>';
294
+
295
+ food = Math.floor(Math.random() * (max + 1));
296
+
297
+ hirumeshi += hirusub1[food] + '&nbsp;' + hirusub1cal[food] + 'kカロリー<br>';
298
+
299
+ if (food == 0){
300
+
301
+ hirumeshi += '<img src="images/tori.jpg"><br>';
302
+
303
+ } else {
304
+
305
+ hirumeshi += '<img src="images/itame.jpg"><br>';
306
+
307
+ }
308
+
309
+ goukei = goukei + hirusub1cal[food];
310
+
311
+ } else {
312
+
313
+ hirumeshi += '<img src="images/pan.jpg"><br>';
314
+
315
+ var hirusub2 = ['ハンバーグ','白身魚'];
316
+
317
+ var hirusub2cal = [300,200];
318
+
319
+ var max = 1;
320
+
321
+ hirumeshi += '<br>昼 おかず1<br>';
322
+
323
+ food = Math.floor(Math.random() * (max + 1));
324
+
325
+ hirumeshi += hirusub2[food] + '&nbsp;' + hirusub2cal[food] + 'kカロリー<br>';
326
+
327
+ if (food == 0){
328
+
329
+ hirumeshi += '<img src="images/humberg.jpg"><br>';
330
+
331
+ } else {
332
+
333
+ hirumeshi += '<img src="images/shiromi.jpg"><br>';
334
+
335
+ }
336
+
337
+ goukei = goukei + hirusub2cal[food];
338
+
339
+ }
340
+
341
+
342
+
343
+ 以下、省略
344
+
345
+ ```
346
+
347
+
348
+
349
+ ```html
350
+
351
+ <!DOCTYPE html>
352
+
353
+ <html lang="en">
354
+
355
+ <head>
356
+
357
+ <meta charset="UTF-8">
358
+
359
+ <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!--IE対策-->
360
+
361
+ <title></title>
362
+
363
+ <meta name="viewport" content="width=device-width, initial-scale=1">
364
+
365
+ <meta name="description" content="">
366
+
367
+ <meta name="keywords" content="">
368
+
369
+ <link rel="stylesheet" href="css_template.css"> <!--cssファイル名-->
370
+
371
+ <script type="text/javascript" src="js_template.js"></script> <!--jsファイル名-->
372
+
373
+ </head>
374
+
375
+
376
+
377
+ <body>
378
+
379
+ <div id="formarea">
380
+
381
+ <form name="frm">
382
+
383
+ <label>上限カロリーを設定して下さい<input type="text" name="maxcal" value=""></label>
384
+
385
+ <input type="button" value="スタート" id="calbtn">
386
+
387
+ <input type="button" value="クリア" id="clrbtn">
388
+
389
+ </form>
390
+
391
+ </div>
392
+
393
+
394
+
395
+ <div id="asaarea"></div>
396
+
397
+ <div id="hiruarea"></div>
398
+
399
+ <div id="yoruarea"></div>
400
+
401
+ <div id="sumarea"></div>
402
+
403
+ </body>
404
+
405
+
406
+
407
+ </html>
408
+
409
+
410
+
411
+ ```
52
412
 
53
413
  どなたかヘルプ頂けますと幸いです。
54
414