質問するログイン新規登録

回答編集履歴

1

調整

2025/05/29 03:30

投稿

yambejp
yambejp

スコア118405

answer CHANGED
@@ -1,1 +1,55 @@
1
1
  localStorageに連勝記録を保持するとして、現在の連勝数は別途localStorageなりグローバル変数などで管理しないと、常に連勝記録をいじってしまう設定になっていませんか?
2
+
3
+ # 参考
4
+ 特に反応がないようなのでサンプル
5
+ ```html
6
+ <script>
7
+ const points=JSON.parse(localStorage.getItem('points')??`{"easy":0,"meduim":0,"hard":0}`);
8
+ window.addEventListener('DOMContentLoaded', ()=>{
9
+ easy.textContent=points.easy;
10
+ meduim.textContent=points.meduim;
11
+ hard.textContent=points.hard;
12
+ level.addEventListener('change',()=>{
13
+ consect.textContent="0";
14
+ });
15
+ });
16
+ document.addEventListener('click',e=>{
17
+ const t=e.target;
18
+ let c=parseInt(consect.textContent);
19
+ if(t.matches('#win')){
20
+ c++;
21
+ }
22
+ if(t.matches('#lose')){
23
+ c=0;
24
+ }
25
+ if(points[level.value]<c){
26
+ points[level.value]=c;
27
+ document.querySelector(`#${level.value}`).textContent=c;
28
+ localStorage.setItem('points',JSON.stringify(points));
29
+ }
30
+ consect.textContent=c;
31
+ });
32
+ </script>
33
+ <nav>
34
+ <dl>
35
+ <dt>Easy</dt>
36
+ <dd id="easy">0</dd>
37
+ <dt>Medim</dt>
38
+ <dd id="meduim">0</dd>
39
+ <dt>Hard</dt>
40
+ <dd id="hard">0</dd>
41
+ </dl>
42
+ </nav>
43
+ <select id="level">
44
+ <option value="easy">Easy</option>
45
+ <option value="meduim">Medium</option>
46
+ <option value="hard">Hard</option>
47
+ </select>
48
+ <input type="button" id="win" value="win">
49
+ <input type="button" id="draw" value="draw">
50
+ <input type="button" id="lose" value="lose">
51
+ <dl>
52
+ <dt>consecut</dt>
53
+ <dd id="consect">0</dd>
54
+ </dl>
55
+ ```