All files / components/exercises AddSubtractExercise.js

84.28% Statements 59/70
71.05% Branches 27/38
86.66% Functions 13/15
87.09% Lines 54/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371        12x                                                                                                                                                                                                                                                                                                                                                                                                                                               27x 27x       39x   39x 39x 39x   39x 12x           27x                 1x                   1x 1x 1x 1x 1x 1x     1x 1x 1x     1x       5x   5x       5x   5x 4x   1x     5x       8x 8x 8x 8x         8x           1x 1x 1x                                       8x 8x 8x 8x 8x   8x       2x     2x   1x 1x     1x   1x     2x 2x         9x     9x     9x       12x    
export default {
    name: 'AddSubtractExercise',
    emits: ['back'],
    data() {
        return {
            isSetupMode: true,
            settings: {
                mode: null,      // 'learn' lub 'challenge'
                range: null,     // 'small', 'medium', 'large'
                operation: null, // 'add' lub 'subtract'
                digits: null     // 'single' lub 'double'
            },
            exercise: {
                num1: 0,
                num2: 0,
                operation: '+',
                currentIndex: 0,
                totalQuestions: 10,
                isComplete: false
            },
            userAnswer: '',
            stats: {
                points: 0,
                correctAnswers: 0,
                mistakes: 0,
                currentStreak: 0
            },
            feedback: {
                message: '',
                type: ''
            },
            setupValidation: {
                mode: { required: true, selected: false },
                range: { required: true, selected: false },
                operation: { required: true, selected: false }
            },
            setupFeedback: {
                show: true,
                message: 'Wybierz opcje aby rozpocząć ćwiczenie! 😊',
                type: 'info'
            }
        }
    },
    template: `
        <div class="max-w-4xl mx-auto p-4">
            <!-- Tryb konfiguracji -->
            <div v-if="isSetupMode" class="space-y-8">
                <div class="flex items-center mb-4">
                    <button @click="$emit('back')" class="text-indigo-600 hover:text-indigo-800">← Powrót</button>
                    <div class="ml-auto space-x-4">
                        <span class="bg-blue-100 text-blue-800 px-3 py-1 rounded-full">Punkty: {{ stats.points }}</span>
                        <span class="bg-green-100 text-green-800 px-3 py-1 rounded-full">Seria: {{ stats.currentStreak }}</span>
                    </div>
                </div>
 
                <div>
                    <h3 class="text-lg font-medium mb-4">Wybierz tryb:</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div 
                            @click="settings.mode = 'learn'"
                            :class="['p-6 rounded-lg cursor-pointer border-2 text-center',
                                    settings.mode === 'learn' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-3xl mb-2">📚</div>
                            <h4 class="font-medium">Tryb nauki</h4>
                            <p class="text-sm text-gray-600">Ucz się w swoim tempie</p>
                        </div>
                        <div 
                            @click="settings.mode = 'challenge'"
                            :class="['p-6 rounded-lg cursor-pointer border-2 text-center',
                                    settings.mode === 'challenge' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-3xl mb-2">⭐</div>
                            <h4 class="font-medium">Tryb wyzwań</h4>
                            <p class="text-sm text-gray-600">Sprawdź swoje umiejętności</p>
                        </div>
                    </div>
                </div>
 
                <div>
                    <h3 class="text-lg font-medium mb-4">Wybierz zakres:</h3>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div 
                            @click="settings.range = 'small'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.range === 'small' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">🚂</div>
                            <h4 class="font-medium">Mała podróż</h4>
                            <p class="text-sm text-gray-600">Liczby 1-20</p>
                        </div>
                        <div 
                            @click="settings.range = 'medium'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.range === 'medium' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">🚅</div>
                            <h4 class="font-medium">Średnia podróż</h4>
                            <p class="text-sm text-gray-600">Liczby 1-50</p>
                        </div>
                        <div 
                            @click="settings.range = 'large'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.range === 'large' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">✈️</div>
                            <h4 class="font-medium">Długa podróż</h4>
                            <p class="text-sm text-gray-600">Liczby 1-100</p>
                        </div>
                    </div>
                </div>
 
                <div>
                    <h3 class="text-lg font-medium mb-4">Wybierz rodzaj liczb:</h3>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div 
                            @click="settings.digits = 'single'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.digits === 'single' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">1️⃣</div>
                            <h4 class="font-medium">Pojedyncze</h4>
                            <p class="text-sm text-gray-600">np. 5+3</p>
                        </div>
                        <div 
                            @click="settings.digits = 'double'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.digits === 'double' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">2️⃣</div>
                            <h4 class="font-medium">Podwójne</h4>
                            <p class="text-sm text-gray-600">np. 55+44</p>
                        </div>
                        <div 
                            @click="settings.digits = 'mixed'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.digits === 'mixed' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">🎲</div>
                            <h4 class="font-medium">Mieszane</h4>
                            <p class="text-sm text-gray-600">np. 5+44</p>
                        </div>
                    </div>
                </div>
 
                <div>
                    <h3 class="text-lg font-medium mb-4">Wybierz działanie:</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div 
                            @click="settings.operation = '+'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.operation === '+' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">➕</div>
                            <h4 class="font-medium">Dodawanie</h4>
                        </div>
                        <div 
                            @click="settings.operation = '-'"
                            :class="['p-4 rounded-lg cursor-pointer border-2 text-center',
                                    settings.operation === '-' ? 'border-indigo-600 bg-indigo-50' : 'border-gray-200']"
                        >
                            <div class="text-2xl mb-2">➖</div>
                            <h4 class="font-medium">Odejmowanie</h4>
                        </div>
                    </div>
                </div>
 
                <button 
                    class="start-button"
                    :disabled="!isSetupComplete"
                    @click="startExercise"
                >
                    Rozpocznij ćwiczenie
                </button>
            </div>
 
            <!-- Tryb ćwiczenia -->
            <div v-else class="space-y-6">
                <div class="flex justify-between items-center">
                    <button @click="isSetupMode = true" class="text-indigo-600 hover:text-indigo-800">← Wróć do ustawień</button>
                    <div class="space-x-4">
                        <span class="bg-blue-100 text-blue-800 px-3 py-1 rounded-full">Punkty: {{ stats.points }}</span>
                        <span class="bg-green-100 text-green-800 px-3 py-1 rounded-full">Seria: {{ stats.currentStreak }}</span>
                    </div>
                </div>
 
                <div class="text-center">
                    <div class="text-4xl font-bold mb-8">
                        {{ exercise.num1 }} {{ exercise.operation }} {{ exercise.num2 }} = 
                        <input 
                            v-model="userAnswer"
                            type="number"
                            class="text-center text-2xl w-32 p-2 border-2 rounded-lg"
                            @keyup.enter="checkAnswer"
                            :disabled="exercise.isComplete"
                        >
                    </div>
                </div>
 
                <div v-if="feedback.message" 
                     :class="['p-4 rounded-lg text-center',
                             feedback.type === 'success' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800']">
                    {{ feedback.message }}
                </div>
 
                <div class="text-center" v-if="exercise.isComplete">
                    <h3 class="text-xl font-bold mb-4">Ćwiczenie zakończone!</h3>
                    <p>Poprawne odpowiedzi: {{ stats.correctAnswers }} / {{ exercise.totalQuestions }}</p>
                    <button 
                        @click="isSetupMode = true"
                        class="mt-4 bg-indigo-600 text-white px-6 py-2 rounded-lg hover:bg-indigo-700 transition-colors"
                    >
                        Spróbuj ponownie
                    </button>
                </div>
            </div>
        </div>
    `,
    methods: {
        updateSetupValidation(field, value) {
            this.setupValidation[field].selected = !!value;
            this.checkSetupCompleteness();
        },
 
        checkSetupCompleteness() {
            const missing = [];
 
            if (!this.settings.mode) missing.push('tryb ćwiczenia');
            if (!this.settings.range) missing.push('zakres liczb');
            if (!this.settings.operation) missing.push('rodzaj działania');
 
            if (missing.length > 0) {
                this.setupFeedback = {
                    show: true,
                    message: `Wybierz jeszcze: ${missing.join(', ')} 😊`,
                    type: 'info'
                };
            } else {
                this.setupFeedback = {
                    show: true,
                    message: 'Świetnie! Możesz rozpocząć ćwiczenie! 🎉',
                    type: 'success'
                };
            }
        },
 
        startExercise() {
            Iif (!this.settings.mode || !this.settings.range ||
                !this.settings.operation || !this.settings.digits) {
                this.setupFeedback = {
                    show: true,
                    message: 'Wybierz wszystkie opcje przed rozpoczęciem 😊',
                    type: 'warning'
                };
                return;
            }
 
            this.exercise.operation = this.settings.operation;
            this.exercise.currentIndex = 0;
            this.exercise.isComplete = false;
            this.isSetupMode = false;
            this.userAnswer = '';
            this.feedback.message = '';
 
            // Reset statystyk
            this.stats.correctAnswers = 0;
            this.stats.mistakes = 0;
            this.stats.currentStreak = 0;
 
            // Generowanie pierwszego pytania
            this.generateQuestion();
        },
 
        checkAnswer() {
            Iif (!this.userAnswer) return;
 
            const expectedAnswer = this.exercise.operation === '+'
                ? this.exercise.num1 + this.exercise.num2
                : this.exercise.num1 - this.exercise.num2;
 
            const isCorrect = parseInt(this.userAnswer) === expectedAnswer;
 
            if (isCorrect) {
                this.handleCorrectAnswer();
            } else {
                this.handleIncorrectAnswer(expectedAnswer);
            }
 
            this.userAnswer = '';
        },
 
        handleCorrectAnswer() {
            this.stats.correctAnswers++;
            this.stats.currentStreak++;
            this.addPoints();
            this.feedback = {
                message: 'Świetnie! To prawidłowa odpowiedź!',
                type: 'success'
            };
 
            setTimeout(() => {
                this.moveToNext();
            }, 1000);
        },
 
        handleIncorrectAnswer(expectedAnswer) {
            this.stats.mistakes++;
            this.stats.currentStreak = 0;
            this.feedback = {
                message: this.settings.mode === 'learn'
                    ? `Spróbuj jeszcze raz! Wynik ${this.exercise.num1} ${this.exercise.operation} ${this.exercise.num2} powinien być ${expectedAnswer}`
                    : 'Nieprawidłowa odpowiedź!',
                type: 'error'
            };
        },
 
        moveToNext() {
            this.exercise.currentIndex++;
 
            if (this.exercise.currentIndex >= this.exercise.totalQuestions) {
                this.exercise.isComplete = true;
                return;
            }
 
            this.generateQuestion();
        },
 
        addPoints() {
            let points = 10;
            if (this.settings.mode === 'challenge') points *= 1.5;
            if (this.stats.currentStreak > 2) points *= 1.2;
            Iif (this.settings.range === 'medium') points *= 1.2;
            Iif (this.settings.range === 'large') points *= 1.5;
 
            this.stats.points += Math.round(points);
        },
 
        generateQuestion() {
            const maxNumber = this.settings.range === 'small' ? 20 :
                this.settings.range === 'medium' ? 50 : 100;
 
            if (this.exercise.operation === '+') {
                // Dla dodawania - obie liczby losowe
                this.exercise.num1 = Math.floor(Math.random() * maxNumber) + 1;
                this.exercise.num2 = Math.floor(Math.random() * maxNumber) + 1;
            } else {
                // Dla odejmowania - najpierw losujemy mniejszą liczbę
                this.exercise.num2 = Math.floor(Math.random() * (maxNumber - 2)) + 1;
                // Następnie losujemy większą liczbę z zakresu (num2, maxNumber>
                this.exercise.num1 = this.exercise.num2 + Math.floor(Math.random() * (maxNumber - this.exercise.num2)) + 1;
            }
 
            this.userAnswer = '';
            this.feedback.message = '';
        }
    },
    watch: {
        'settings.mode'(newValue) {
            this.updateSetupValidation('mode', newValue);
        },
        'settings.range'(newValue) {
            this.updateSetupValidation('range', newValue);
        },
        'settings.operation'(newValue) {
            this.updateSetupValidation('operation', newValue);
        }
    },
    mounted() {
        this.checkSetupCompleteness();
    }
};