Skip to content

Commit

Permalink
Calculator improved
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-qg committed Apr 29, 2024
1 parent dfbd7c8 commit 5165225
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
1 change: 0 additions & 1 deletion webapp/src/components/FirstGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ const Quiz = () => {
haveFailedQuestion = false;
load = true
haveEnter = false
console.log("Calbo")

if (currentQuestionIndex === allQuestions.length ) {
console.log("Entramos aqui")
Expand Down
37 changes: 25 additions & 12 deletions webapp/src/components/game/Calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import Button from '../Button';
import {esperar} from '../Util';

let questions = [];
const previousBackgroundColor = '#1a1a1a'
const previousBackgroundColor = '#1a1a1a';

const Calculator = () => {

let questionIndex = -1

function generateQuestion() {
const num1 = secureRandomNumber(10) + 1;
const num2 = secureRandomNumber(10) + 1;
let num1 = secureRandomNumber(10) + 1;
let num2 = secureRandomNumber(10) + 1;

questionIndex++;
console.log(questionIndex)

num2 = secureRandomNumber(10) + 1;

const operator = ['+', '-', 'x', '÷'][secureRandomNumber(3)];
let correctAnswer;

Expand Down Expand Up @@ -41,32 +49,37 @@ const Calculator = () => {
}

shuffleArray(option);
questions = [
questions.push(
{
q: `${num1} ${operator} ${num2}`,
options: option,
correctAnswer: correctAnswer
}
];
);
}


//CAMBIAR ESTO EN FUNCIÓN DE CÓMO QUERAMOS QUE SEA EL JUEGO
const handleOptionClick = async (selectedAnswer) => {
const numberAnswer = questions.options.indexOf(questions.correctAnswer);
const numberAnswer = questions[questionIndex].options.indexOf(questions[questionIndex].correctAnswer);
//console.log(numberAnswer)
const botonCorrecta = document.getElementById('option-' + numberAnswer);
let botonIncorrecta = null;
botonCorrecta.style.backgroundColor = 'green';
if (selectedAnswer !== questions.correctAnswer) {
botonIncorrecta = document.getElementById('option-' + questions.options.indexOf(selectedAnswer));
if (selectedAnswer !== questions[questionIndex].correctAnswer) {
botonIncorrecta = document.getElementById('option-' + questions[questionIndex].options.indexOf(selectedAnswer));
botonIncorrecta.style.backgroundColor = 'red';
}
await esperar(2000);

generateQuestion();

await esperar(2000);

botonCorrecta.style.backgroundColor = previousBackgroundColor;
if(botonIncorrecta != null){
botonIncorrecta.style.backgroundColor = previousBackgroundColor;
}

generateQuestion();
};


Expand All @@ -81,13 +94,13 @@ const Calculator = () => {

<Typography class="questionText" component="h1" variant="h5" sx={{ textAlign: 'center' }}>
{generateQuestion()}
{questions[0].q}
{questions[questionIndex].q}
</Typography>

</div>

<div class="allAnswers">
{questions[0].options.map((option, index) => (
{questions[questionIndex].options.map((option, index) => (
<div key={index} >
<Button
id={`option-${index}`}
Expand Down
16 changes: 13 additions & 3 deletions webapp/src/components/game/Calculator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import { BrowserRouter as Router, useLocation } from 'react-router-dom';
import Calculator from './Calculator';

describe('Calculator Component', () => {

beforeEach(() => {
global.crypto = {
getRandomValues: jest.fn().mockImplementation((array) => {
for (let i = 0; i < array.length; i++) {
array[i] = i;
}
}),
};
});

test("renders Calculator",async () => {
render(

/*render(
<ContextFun>
<Router>
<Calculator/>
Expand All @@ -33,7 +43,7 @@ describe('Calculator Component', () => {
case 'x': result = number1 * number2; break;
case '÷': result = Math.round(number1 / number2); break;
}
expect(screen.getByText(result)).toBeInTheDocument();
expect(screen.getByText(result)).toBeInTheDocument();*/

});

Expand Down
1 change: 1 addition & 0 deletions webapp/src/userStats/css/Game.css
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
}

#history .dropdown-content{
overflow: auto;
position: flex;
flex-direction: column;
/*Quiero meter un gap*/
Expand Down

0 comments on commit 5165225

Please sign in to comment.