Exe2 ENG
Exe2 ENG
Instructions
You will have 120 minutes to write code that must pass all the provided tests.
The HTML tags (or JSX.IntrinsicElements), file names, and any other explicitly
specified information must match the instructions exactly; otherwise, the tests will
not be accepted.
Each test that passes equals 1 point. The point-to-grade mapping is listed in the
task description on MS Teams.
Submit your solution (entire project) as a zipped archive. Any other archive
format will not be checked.
1
Any attempt at plagiarism will result in a grade of 2, with no
option to resubmit tasks.
2
Running Tests
To run tests, use the command npm test in the terminal in the main project folder. This command
will run continuously, and any changes in the project files will automatically trigger the tests.
3
Figure 3: Some tests passed
Tasks
Task 1
In file Counter.jsx create new component called Counter. Component should consist of 3 buttons
and one h1 tag.
One button will increase the counter, MUST display string Increase,
One button will decrease the counter, MUST display string Decrease,
One button will reset the counter, MUST display string Reset,
You must ensure that there will be no possibility that counter will be lower than 0, cannot be
negative number.
To manage state changes in Counter you have to use a reducer hook. Create a function named
reducer which will take two arguments ( must be named like this) :
state, which will contain the count
if user will click the Increase button the state of counter will increment by 1,
4
Task 2
In this task the scheme of LetterCounter component, in LetterCounter.jsx is provided as fallows (
do not modify provided code )
1 <>
2 <h1 > Complex Letter Counter </ h1 >
3 <div >
4 < label >
5 Text :
6 < input type =" text " value ={ state . text } onChange ={ h a n d l e T e x t C h a n g e } / >
7 </ label >
8 </ div >
9 { state . letters . map (( letter , index ) = > (
10 < div key ={ index } >
11 < label >
12 Letter { index + 1}:
13 < input type =" text " value ={ letter } onChange ={ h a n d l e L e t t e r C h a n g e ( index ) } / >
14 </ label >
15 </ div >
16 ))}
17 < button onClick ={ ad dLetter Field } > Add Letter </ button >
18 < button onClick ={ r e m o v e L e t t e r F i e l d } disabled ={ state . letters . length <= 1} >
19 Remove Letter
20 </ button >
21 < button onClick ={ c o u n t O c c u r r e n c e s } > Count </ button >
22 { results }
23 </ >
Application Structure
The application has the following primary components:
Reducer: A reducer function ( need to be named like that) is used to handle different actions,
such as setting text, adding/removing letters, and counting occurrences.
User Interface: The UI includes:
– An input field for the user to type in the text they want to analyze.
– Dynamic input fields for each letter the user wants to track.
5
– Buttons to add or remove letter input fields.
– A button to trigger the counting of letter occurrences.
– A section that displays the results showing how many times each letter appears in the
text.
based on value of action.type You have to implement component behavior. that handles
different actions:
– SET TEXT: Updates the text property when the user types in the text input field.
* send payload with value of state.text
– SET LETTER: Updates the letters array when the user modifies a specific letter input
field.
* send payload with value of letter
* send index , same as in handleLetterChange
– ADD LETTER: Adds a new empty letter input field to the letters array.
– REMOVE LETTER: Removes the last letter input field from the letters array, but ensure
that at least one letter input field remains.
– SET COUNTS: Updates the counts array, which will hold the number of occurrences of each
letter in the text.
* send payload with value of counts
* send index , same as in handleLetterChange
Add/Remove Letter Fields: Provide buttons to allow the user to add or remove letter
input fields:
6
– When the ”Add Letter” button is clicked, dispatch the ADD LETTER action to add a new
empty letter field. (add new element to letters field of state)
– When the ”Remove Letter” button is clicked, dispatch the REMOVE LETTER action to
remove the last letter field (remove last element from letters field of state), but ensure
that at least one letter input field remains.
Task 3
In this task the scheme of PokemonList and MemoizedPokemonElem component, in Pokemon-
List.jsx and PokemonElem.jsx is provided in files.
MemoizedPokemonElem
MemoizedPokemonElem component must be responsible for displayin name of the pokemon
( in a p HTML tag, and it’s picture ( in img HTML tag ) based on the parameters that are
send to this component as properties. Component must be wrapped in a li HTML tag, and
using the React.memo functionality
7
PokemonList
PokemonList component must be responsible for displaying a list of MemoizedPokemonElem
componenets. It must wrap the MemoizedPokemonElem components in a ul HTML tag.
This component is also responsible for fetchin information from PokeApi using this fetch string:
1 https :// pokeapi . co / api / v2 / pokemon ? limit = pokeNumber
Remember how to pass a variable to a string! Do not copy past this code, as it
will not work.
Where the pokeNumber is a property of component. In example:
1 < PokemonList pokeNumber ={10}/ >
2 < PokemonList pokeNumber ={14}/ >
3 < PokemonList pokeNumber ={1022}/ >
4 < PokemonList / >
First will call for fetching 10 pokemons, second will fetch 14, third 1022, and last ( where this
parameter is not set) will fetch 10 pokemons.
1 const initialState = {
2 pokemonList : [] ,
3 loading : true ,
4 error : null ,
5 filter : ’’,
6 };
8
5.2 Set Up the useReducer Hook
– Inside your main component (e.g., PokemonList), use the useReducer hook to manage
the state of your application.
– Pass the reducer function and the initial state to the useReducer hook.
9
5.7 Handle Loading and Error States
– Before rendering the Pokémon list, check if the data is still loading or if an error occurred:
* If loading is true, display a "Loading..." message.
* If there is an error (error state is not null), display the error message.
– If neither condition is true, render the filtered Pokémon list.
10