Skip to content

Commit

Permalink
Correct Guess Record and Local Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 24, 2022
1 parent aad89be commit 723b238
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions evens-or-odds/src/components/GameState.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import React from 'react';
import { connect } from 'react-redux';

const correctGuessesRecordKey = 'CORRECT_GUESSES_RECORD_foo123';

const checkRecord = correctGuesses => {
const record = Number(localStorage.getItem(correctGuessesRecordKey));

if (correctGuesses > record) {
localStorage.setItem(correctGuessesRecordKey, correctGuesses);

return { record: correctGuesses, isNewRecord: true };
}

return { record, isNewRecord: false };
}

const GameState = ({ correctGuesses, remaining }) => {
const guessText = correctGuesses === 1 ? 'guess' : 'guesses';

const { record, isNewRecord } = checkRecord(correctGuesses);
const recordLabel = isNewRecord ? '🎉 New record' : 'Record';

return (
<div>
<h3>{recordLabel}: {record}</h3>
<p>{remaining} cards remaining</p>
<p>{correctGuesses} correct {guessText}</p>
</div>
Expand Down

0 comments on commit 723b238

Please sign in to comment.