Passed
Push — master ( 5f67fe...8bce0b )
by Jesús
02:16
created

state.ts ➔ toggleBox   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
export const state = {
2
  boxes: new Array(12).fill(false) as boolean[],
3
  wordlist: [] as string[],
4
  currentLanguage: 'english',
5
};
6
7
export function resetBoxes(): void {
8
  state.boxes.fill(false);
9
}
10
11
export function toggleBox(index: number): void {
12
  state.boxes[index] = !state.boxes[index];
13
}
14
15
export function calculateBinaryValue(): number {
16
  let value = 0;
17
  for (let i = 0; i < 12; i++) {
18
    if (state.boxes[i]) {
19
      value += Math.pow(2, 11 - i);
20
    }
21
  }
22
  return value;
23
}
24
25
export function getBinaryString(): string {
26
  return state.boxes.map(b => b ? '1' : '0').join('');
27
}
28
29
export function setStateFromIndex(wordIndex: number): void {
30
  // Convert word index (0-2047) to binary and set the boxes
31
  // Note: wordIndex + 1 gives us the actual number (1-2048)
32
  const value = wordIndex + 1;
33
34
  resetBoxes();
35
36
  for (let i = 0; i < 12; i++) {
37
    const bitPosition = 11 - i;
38
    const bitValue = Math.pow(2, bitPosition);
39
    state.boxes[i] = (value & bitValue) !== 0;
40
  }
41
}
42