fpythons 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fpythons/__init__.py +12 -0
- fpythons/__main__.py +4 -0
- fpythons/data/00_START_HERE.md +144 -0
- fpythons/data/ALL_SLIPS_CODE.txt +1854 -0
- fpythons/data/README.md +35 -0
- fpythons/data/Slip_01.md +160 -0
- fpythons/data/Slip_02.md +104 -0
- fpythons/data/Slip_03.md +95 -0
- fpythons/data/Slip_04.md +131 -0
- fpythons/data/Slip_05.md +120 -0
- fpythons/data/Slip_06.md +161 -0
- fpythons/data/Slip_07.md +121 -0
- fpythons/data/Slip_08.md +95 -0
- fpythons/data/Slip_09.md +92 -0
- fpythons/data/Slip_10.md +113 -0
- fpythons/data/Slip_11.md +109 -0
- fpythons/data/Slip_12.md +181 -0
- fpythons/data/Slip_13.md +163 -0
- fpythons/data/Slip_14.md +141 -0
- fpythons/data/Slip_15.md +100 -0
- fpythons/data/Slip_16.md +127 -0
- fpythons/data/Slip_17.md +93 -0
- fpythons/data/Slip_18.md +96 -0
- fpythons/data/Slip_19.md +94 -0
- fpythons/data/Slip_20.md +165 -0
- fpythons/data/Slip_21.md +122 -0
- fpythons/data/VIVA.md +195 -0
- fpythons/data/code/slip1/.gitignore +2 -0
- fpythons/data/code/slip1/package.json +31 -0
- fpythons/data/code/slip1/public/index.html +12 -0
- fpythons/data/code/slip1/src/App.js +7 -0
- fpythons/data/code/slip1/src/index.js +10 -0
- fpythons/data/code/slip1/src/slip1/loginForm.js +26 -0
- fpythons/data/code/slip1/src/slip1/style.css +46 -0
- fpythons/data/code/slip10/.gitignore +2 -0
- fpythons/data/code/slip10/package.json +31 -0
- fpythons/data/code/slip10/public/index.html +12 -0
- fpythons/data/code/slip10/src/App.js +7 -0
- fpythons/data/code/slip10/src/index.js +10 -0
- fpythons/data/code/slip10/src/slip10/SumOfDigits.js +37 -0
- fpythons/data/code/slip11/.gitignore +2 -0
- fpythons/data/code/slip11/package.json +31 -0
- fpythons/data/code/slip11/public/index.html +12 -0
- fpythons/data/code/slip11/src/App.js +7 -0
- fpythons/data/code/slip11/src/index.js +10 -0
- fpythons/data/code/slip11/src/slip11/StringOperation.js +33 -0
- fpythons/data/code/slip12/.gitignore +2 -0
- fpythons/data/code/slip12/package.json +33 -0
- fpythons/data/code/slip12/public/index.html +12 -0
- fpythons/data/code/slip12/src/App.js +7 -0
- fpythons/data/code/slip12/src/index.js +14 -0
- fpythons/data/code/slip12/src/slip12/NotesManagementApp.js +35 -0
- fpythons/data/code/slip12/src/slip12/redux/notesSlice.js +20 -0
- fpythons/data/code/slip12/src/slip12/redux/store.js +10 -0
- fpythons/data/code/slip13/.gitignore +2 -0
- fpythons/data/code/slip13/package.json +33 -0
- fpythons/data/code/slip13/public/index.html +12 -0
- fpythons/data/code/slip13/src/App.js +7 -0
- fpythons/data/code/slip13/src/index.js +14 -0
- fpythons/data/code/slip13/src/slip13/Counter.js +20 -0
- fpythons/data/code/slip13/src/slip13/redux/counterSlice.js +19 -0
- fpythons/data/code/slip13/src/slip13/redux/store.js +10 -0
- fpythons/data/code/slip14/.gitignore +2 -0
- fpythons/data/code/slip14/package.json +31 -0
- fpythons/data/code/slip14/public/index.html +12 -0
- fpythons/data/code/slip14/src/App.js +7 -0
- fpythons/data/code/slip14/src/index.js +10 -0
- fpythons/data/code/slip14/src/slip14/Calculator.js +64 -0
- fpythons/data/code/slip15/.gitignore +2 -0
- fpythons/data/code/slip15/package.json +31 -0
- fpythons/data/code/slip15/public/index.html +12 -0
- fpythons/data/code/slip15/src/App.js +7 -0
- fpythons/data/code/slip15/src/index.js +10 -0
- fpythons/data/code/slip15/src/slip15/Counter.js +24 -0
- fpythons/data/code/slip16/.gitignore +2 -0
- fpythons/data/code/slip16/package.json +31 -0
- fpythons/data/code/slip16/public/index.html +12 -0
- fpythons/data/code/slip16/src/App.js +7 -0
- fpythons/data/code/slip16/src/index.js +10 -0
- fpythons/data/code/slip16/src/slip16/TraditionalDOM.html +24 -0
- fpythons/data/code/slip16/src/slip16/VirtualDOM.js +16 -0
- fpythons/data/code/slip17/.gitignore +2 -0
- fpythons/data/code/slip17/package.json +31 -0
- fpythons/data/code/slip17/public/index.html +12 -0
- fpythons/data/code/slip17/src/App.js +7 -0
- fpythons/data/code/slip17/src/index.js +10 -0
- fpythons/data/code/slip17/src/slip17/Sign-in.js +17 -0
- fpythons/data/code/slip18/.gitignore +2 -0
- fpythons/data/code/slip18/package.json +31 -0
- fpythons/data/code/slip18/public/index.html +12 -0
- fpythons/data/code/slip18/public/logo192.png +0 -0
- fpythons/data/code/slip18/src/App.js +7 -0
- fpythons/data/code/slip18/src/index.js +10 -0
- fpythons/data/code/slip18/src/slip18/Profile.js +20 -0
- fpythons/data/code/slip19/.gitignore +2 -0
- fpythons/data/code/slip19/package.json +31 -0
- fpythons/data/code/slip19/public/index.html +12 -0
- fpythons/data/code/slip19/src/App.js +7 -0
- fpythons/data/code/slip19/src/index.js +10 -0
- fpythons/data/code/slip19/src/slip19/Movies.js +19 -0
- fpythons/data/code/slip2/.gitignore +2 -0
- fpythons/data/code/slip2/package.json +31 -0
- fpythons/data/code/slip2/public/index.html +12 -0
- fpythons/data/code/slip2/src/App.js +7 -0
- fpythons/data/code/slip2/src/index.js +10 -0
- fpythons/data/code/slip2/src/slip2/useEffect.js +23 -0
- fpythons/data/code/slip20/.gitignore +2 -0
- fpythons/data/code/slip20/package.json +32 -0
- fpythons/data/code/slip20/public/index.html +12 -0
- fpythons/data/code/slip20/src/App.js +16 -0
- fpythons/data/code/slip20/src/index.js +10 -0
- fpythons/data/code/slip20/src/slip20/Login.js +45 -0
- fpythons/data/code/slip20/src/slip20/Students.js +27 -0
- fpythons/data/code/slip21/.gitignore +2 -0
- fpythons/data/code/slip21/package.json +31 -0
- fpythons/data/code/slip21/public/index.html +12 -0
- fpythons/data/code/slip21/src/App.js +7 -0
- fpythons/data/code/slip21/src/index.js +10 -0
- fpythons/data/code/slip21/src/slip21/NumberString.js +46 -0
- fpythons/data/code/slip3/.gitignore +2 -0
- fpythons/data/code/slip3/package.json +31 -0
- fpythons/data/code/slip3/public/index.html +12 -0
- fpythons/data/code/slip3/src/App.js +7 -0
- fpythons/data/code/slip3/src/index.js +10 -0
- fpythons/data/code/slip3/src/slip3/toggleBtn.js +16 -0
- fpythons/data/code/slip4/.gitignore +2 -0
- fpythons/data/code/slip4/package.json +32 -0
- fpythons/data/code/slip4/public/index.html +12 -0
- fpythons/data/code/slip4/src/App.js +23 -0
- fpythons/data/code/slip4/src/index.js +10 -0
- fpythons/data/code/slip4/src/slip4/About.js +7 -0
- fpythons/data/code/slip4/src/slip4/Contact.js +7 -0
- fpythons/data/code/slip4/src/slip4/home.js +7 -0
- fpythons/data/code/slip5/.gitignore +2 -0
- fpythons/data/code/slip5/package.json +32 -0
- fpythons/data/code/slip5/public/index.html +12 -0
- fpythons/data/code/slip5/src/App.js +18 -0
- fpythons/data/code/slip5/src/index.js +10 -0
- fpythons/data/code/slip5/src/slip5/loginValidation.js +30 -0
- fpythons/data/code/slip6/.gitignore +2 -0
- fpythons/data/code/slip6/package.json +32 -0
- fpythons/data/code/slip6/public/index.html +12 -0
- fpythons/data/code/slip6/src/App.js +31 -0
- fpythons/data/code/slip6/src/index.js +10 -0
- fpythons/data/code/slip6/src/slip6/Dashboard.js +12 -0
- fpythons/data/code/slip6/src/slip6/Login.js +23 -0
- fpythons/data/code/slip6/src/slip6/ProtectedRoute.js +10 -0
- fpythons/data/code/slip7/.gitignore +2 -0
- fpythons/data/code/slip7/package.json +31 -0
- fpythons/data/code/slip7/public/index.html +12 -0
- fpythons/data/code/slip7/src/App.js +7 -0
- fpythons/data/code/slip7/src/index.js +10 -0
- fpythons/data/code/slip7/src/slip7/TodoList.js +42 -0
- fpythons/data/code/slip8/.gitignore +2 -0
- fpythons/data/code/slip8/package.json +31 -0
- fpythons/data/code/slip8/public/index.html +12 -0
- fpythons/data/code/slip8/src/App.js +7 -0
- fpythons/data/code/slip8/src/index.js +10 -0
- fpythons/data/code/slip8/src/slip8/ToggleButton.js +19 -0
- fpythons/data/code/slip9/.gitignore +2 -0
- fpythons/data/code/slip9/package.json +31 -0
- fpythons/data/code/slip9/public/index.html +12 -0
- fpythons/data/code/slip9/src/App.js +7 -0
- fpythons/data/code/slip9/src/index.js +10 -0
- fpythons/data/code/slip9/src/slip9/Counter.js +16 -0
- fpythons/main.py +226 -0
- fpythons-0.1.0.dist-info/METADATA +64 -0
- fpythons-0.1.0.dist-info/RECORD +170 -0
- fpythons-0.1.0.dist-info/WHEEL +4 -0
- fpythons-0.1.0.dist-info/entry_points.txt +2 -0
fpythons/data/Slip_06.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# Slip 6: Protected route with mock authentication
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a React application with a protected route that only shows a page if the user is authenticated (mock authentication). Display a proper message once the user authenticates.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip6/
|
|
13
|
+
src/slip6/Dashboard.js
|
|
14
|
+
src/slip6/Login.js
|
|
15
|
+
src/slip6/ProtectedRoute.js
|
|
16
|
+
src/App.js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 3. Commands (PowerShell, in this order)
|
|
20
|
+
|
|
21
|
+
```powershell
|
|
22
|
+
npx create-react-app slip6
|
|
23
|
+
cd slip6
|
|
24
|
+
npm install react-router-dom
|
|
25
|
+
mkdir src\slip6
|
|
26
|
+
code .
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 4. Write the code
|
|
30
|
+
|
|
31
|
+
### Step 1: `src/slip6/Dashboard.js`
|
|
32
|
+
|
|
33
|
+
```jsx
|
|
34
|
+
import React from 'react';
|
|
35
|
+
|
|
36
|
+
const Dashboard = () => {
|
|
37
|
+
return (
|
|
38
|
+
<div>
|
|
39
|
+
<h2>Welcome to Dashboard!</h2>
|
|
40
|
+
<p>You are successfully authenticated.</p>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export default Dashboard;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Step 2: `src/slip6/Login.js`
|
|
49
|
+
|
|
50
|
+
```jsx
|
|
51
|
+
import React from 'react';
|
|
52
|
+
import { useNavigate } from 'react-router-dom';
|
|
53
|
+
|
|
54
|
+
const Login = ({ setIsAuthenticated }) => {
|
|
55
|
+
|
|
56
|
+
const navigate = useNavigate();
|
|
57
|
+
|
|
58
|
+
const handleLogin = () => {
|
|
59
|
+
setIsAuthenticated(true);
|
|
60
|
+
navigate('/dashboard');
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<div>
|
|
65
|
+
<h2>Login Page</h2>
|
|
66
|
+
<button onClick={handleLogin}>
|
|
67
|
+
Login
|
|
68
|
+
</button>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export default Login;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Step 3: `src/slip6/ProtectedRoute.js`
|
|
77
|
+
|
|
78
|
+
```jsx
|
|
79
|
+
import React from 'react';
|
|
80
|
+
import { Navigate } from 'react-router-dom';
|
|
81
|
+
|
|
82
|
+
const ProtectedRoute = ({isAuthenticated, children}) => {
|
|
83
|
+
|
|
84
|
+
return isAuthenticated ? children : <Navigate to="/login" />;
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export default ProtectedRoute
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Step 4: `src/App.js`
|
|
92
|
+
|
|
93
|
+
```jsx
|
|
94
|
+
import { useState } from 'react';
|
|
95
|
+
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
|
|
96
|
+
import ProtectedRoute from './slip6/ProtectedRoute';
|
|
97
|
+
import Login from './slip6/Login';
|
|
98
|
+
import Dashboard from './slip6/Dashboard';
|
|
99
|
+
|
|
100
|
+
function App() {
|
|
101
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<BrowserRouter>
|
|
105
|
+
<Routes>
|
|
106
|
+
<Route path="/" element={<Navigate to="/dashboard" />} />
|
|
107
|
+
<Route
|
|
108
|
+
path="/login"
|
|
109
|
+
element={<Login setIsAuthenticated={setIsAuthenticated} />}
|
|
110
|
+
/>
|
|
111
|
+
<Route
|
|
112
|
+
path="/dashboard"
|
|
113
|
+
element={
|
|
114
|
+
<ProtectedRoute isAuthenticated={isAuthenticated}>
|
|
115
|
+
<Dashboard />
|
|
116
|
+
</ProtectedRoute>
|
|
117
|
+
}
|
|
118
|
+
/>
|
|
119
|
+
</Routes>
|
|
120
|
+
</BrowserRouter>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export default App;
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## 5. How it works
|
|
128
|
+
|
|
129
|
+
- `App.js` holds `isAuthenticated` and passes the setter to `Login`.
|
|
130
|
+
- `ProtectedRoute` returns its `children` when authenticated, otherwise `<Navigate to="/login" />`.
|
|
131
|
+
- Clicking **Login** sets the state to `true` and `useNavigate` opens `/dashboard`.
|
|
132
|
+
|
|
133
|
+
## 6. Run it
|
|
134
|
+
|
|
135
|
+
```powershell
|
|
136
|
+
npm start
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Open `/dashboard`: you are sent to `/login`. Click **Login**: the welcome message appears. Refreshing the page logs you out because the state is in memory.
|
|
140
|
+
|
|
141
|
+
## 7. Errors and fixes
|
|
142
|
+
|
|
143
|
+
| Problem | Fix |
|
|
144
|
+
|---|---|
|
|
145
|
+
| `useNavigate() may be used only in the context of a Router` | render `Login` inside `BrowserRouter` |
|
|
146
|
+
| Dashboard always redirects | pass `isAuthenticated` to `ProtectedRoute` |
|
|
147
|
+
|
|
148
|
+
## 8. Viva questions
|
|
149
|
+
|
|
150
|
+
**Q. What is a protected route?**
|
|
151
|
+
A route shown only when a condition, such as being logged in, is true.
|
|
152
|
+
|
|
153
|
+
**Q. What is `children`?**
|
|
154
|
+
The elements placed between a component's opening and closing tags.
|
|
155
|
+
|
|
156
|
+
**Q. Difference between `Navigate` and `useNavigate`?**
|
|
157
|
+
`Navigate` is a component used while rendering. `useNavigate` returns a function used in event handlers.
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
Solved project: [code/slip6](code/slip6/)
|
fpythons/data/Slip_07.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Slip 7: To-do list
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a basic React To-do list: a component for adding, displaying and deleting tasks, state for the list, a form for new tasks, and a button to remove each task.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip7/
|
|
13
|
+
src/slip7/TodoList.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip7
|
|
21
|
+
cd slip7
|
|
22
|
+
mkdir src\slip7
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip7/TodoList.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useState } from 'react'
|
|
32
|
+
|
|
33
|
+
const TodoList = () => {
|
|
34
|
+
|
|
35
|
+
const [task, setTask] = useState('');
|
|
36
|
+
const [tasks, setTasks] = useState([]);
|
|
37
|
+
|
|
38
|
+
const handleSubmit = (e) =>{
|
|
39
|
+
e.preventDefault();
|
|
40
|
+
if(task.trim() === '')
|
|
41
|
+
return;
|
|
42
|
+
setTasks([...tasks, task]);
|
|
43
|
+
setTask('');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const handleDelete = (indexToDelete) => {
|
|
47
|
+
const updatedTasks = tasks.filter(
|
|
48
|
+
(item, index) => index !== indexToDelete
|
|
49
|
+
);
|
|
50
|
+
setTasks(updatedTasks);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div>
|
|
55
|
+
<h2>To-do List</h2>
|
|
56
|
+
<form onSubmit={handleSubmit}>
|
|
57
|
+
<label>Enter task<input type='text' value={task} onChange={(e) => setTask(e.target.value)}></input></label>
|
|
58
|
+
<button type='submit'>Add Task</button>
|
|
59
|
+
</form>
|
|
60
|
+
<ul>
|
|
61
|
+
{tasks.map((item, index) => (
|
|
62
|
+
<li key={index}>
|
|
63
|
+
{item}
|
|
64
|
+
<button onClick={() => handleDelete(index)}>Delete</button>
|
|
65
|
+
</li>
|
|
66
|
+
))}
|
|
67
|
+
</ul>
|
|
68
|
+
</div>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export default TodoList
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Step 2: `src/App.js`
|
|
76
|
+
|
|
77
|
+
```jsx
|
|
78
|
+
import TodoList from './slip7/TodoList';
|
|
79
|
+
|
|
80
|
+
function App() {
|
|
81
|
+
return <TodoList />;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default App;
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## 5. How it works
|
|
88
|
+
|
|
89
|
+
- `task` holds the input text, `tasks` holds the list.
|
|
90
|
+
- On submit, blank text is ignored; otherwise `[...tasks, task]` creates a new array.
|
|
91
|
+
- `filter` creates a new array without the deleted index.
|
|
92
|
+
|
|
93
|
+
## 6. Run it
|
|
94
|
+
|
|
95
|
+
```powershell
|
|
96
|
+
npm start
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Add a few tasks, try adding a blank one, delete one from the middle.
|
|
100
|
+
|
|
101
|
+
## 7. Errors and fixes
|
|
102
|
+
|
|
103
|
+
| Problem | Fix |
|
|
104
|
+
|---|---|
|
|
105
|
+
| List does not update | do not use `tasks.push`; create a new array |
|
|
106
|
+
| Delete runs immediately | write `onClick={() => handleDelete(index)}` |
|
|
107
|
+
|
|
108
|
+
## 8. Viva questions
|
|
109
|
+
|
|
110
|
+
**Q. Why not change state directly?**
|
|
111
|
+
React re-renders only when the setter receives a new value.
|
|
112
|
+
|
|
113
|
+
**Q. What is the spread operator?**
|
|
114
|
+
`...` copies the items of an array or object.
|
|
115
|
+
|
|
116
|
+
**Q. Why is a `key` needed?**
|
|
117
|
+
It helps React match list items between renders. A unique id is better than the index when items can be deleted.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
Solved project: [code/slip7](code/slip7/)
|
fpythons/data/Slip_08.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Slip 8: Toggle button ON/OFF
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a React application with a toggle button. When clicked, the button text toggles between ON and OFF and the background colour changes, visually showing the state.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip8/
|
|
13
|
+
src/slip8/ToggleButton.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip8
|
|
21
|
+
cd slip8
|
|
22
|
+
mkdir src\slip8
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip8/ToggleButton.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useState } from 'react';
|
|
32
|
+
|
|
33
|
+
const ToggleButton = () => {
|
|
34
|
+
const [isOn, setIsOn] = useState(false);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
<h2>Toggle Button</h2>
|
|
39
|
+
<button
|
|
40
|
+
onClick={() => setIsOn(!isOn)}
|
|
41
|
+
style={{ backgroundColor: isOn ? 'lightgreen' : 'lightcoral' }}
|
|
42
|
+
>
|
|
43
|
+
{isOn ? 'ON' : 'OFF'}
|
|
44
|
+
</button>
|
|
45
|
+
</div>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export default ToggleButton;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Step 2: `src/App.js`
|
|
53
|
+
|
|
54
|
+
```jsx
|
|
55
|
+
import ToggleButton from './slip8/ToggleButton';
|
|
56
|
+
|
|
57
|
+
function App() {
|
|
58
|
+
return <ToggleButton />;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default App;
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 5. How it works
|
|
65
|
+
|
|
66
|
+
- `isOn` is a boolean state, `false` at the start.
|
|
67
|
+
- `onClick` sets it to the opposite value with `!isOn`.
|
|
68
|
+
- The button's text and `backgroundColor` are chosen with ternary operators.
|
|
69
|
+
|
|
70
|
+
## 6. Run it
|
|
71
|
+
|
|
72
|
+
```powershell
|
|
73
|
+
npm start
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Click the button: OFF (red) changes to ON (green) and back.
|
|
77
|
+
|
|
78
|
+
## 7. Errors and fixes
|
|
79
|
+
|
|
80
|
+
| Problem | Fix |
|
|
81
|
+
|---|---|
|
|
82
|
+
| `background-color` does not work | in a style object write `backgroundColor` |
|
|
83
|
+
| Colour does not change | the style must read the state variable |
|
|
84
|
+
|
|
85
|
+
## 8. Viva questions
|
|
86
|
+
|
|
87
|
+
**Q. What is JSX?**
|
|
88
|
+
HTML-like syntax inside JavaScript that React turns into elements.
|
|
89
|
+
|
|
90
|
+
**Q. What is conditional rendering?**
|
|
91
|
+
Showing different output depending on a condition.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
Solved project: [code/slip8](code/slip8/)
|
fpythons/data/Slip_09.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Slip 9: Counter with initial value as a prop
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a Counter component that displays a number with "Increment" and "Decrement" buttons. Start at 0. Use state for the number and pass the initial number as a prop.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip9/
|
|
13
|
+
src/slip9/Counter.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip9
|
|
21
|
+
cd slip9
|
|
22
|
+
mkdir src\slip9
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip9/Counter.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, {useState} from 'react'
|
|
32
|
+
|
|
33
|
+
const Counter = ({initialCount}) => {
|
|
34
|
+
|
|
35
|
+
const [count, setCount] = useState(initialCount);
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
<div>
|
|
39
|
+
<h2>Count: {count}</h2>
|
|
40
|
+
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
41
|
+
<button onClick={() => setCount(count - 1)}>Decrement</button>
|
|
42
|
+
</div>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default Counter
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Step 2: `src/App.js`
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
import Counter from './slip9/Counter';
|
|
53
|
+
|
|
54
|
+
function App() {
|
|
55
|
+
return <Counter initialCount={0} />;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default App;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 5. How it works
|
|
62
|
+
|
|
63
|
+
- `App.js` passes `initialCount={0}`.
|
|
64
|
+
- The component receives it with `({ initialCount })` and uses it as the first state value.
|
|
65
|
+
- Each button calls `setCount`.
|
|
66
|
+
|
|
67
|
+
## 6. Run it
|
|
68
|
+
|
|
69
|
+
```powershell
|
|
70
|
+
npm start
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Click both buttons. Change `initialCount` to 5 in `App.js` and see the start value.
|
|
74
|
+
|
|
75
|
+
## 7. Errors and fixes
|
|
76
|
+
|
|
77
|
+
| Problem | Fix |
|
|
78
|
+
|---|---|
|
|
79
|
+
| Count is empty or `NaN` | `<Counter />` was used without the prop |
|
|
80
|
+
| `"01"` instead of `1` | pass `{0}`, not `"0"` |
|
|
81
|
+
|
|
82
|
+
## 8. Viva questions
|
|
83
|
+
|
|
84
|
+
**Q. What are props?**
|
|
85
|
+
Read-only values passed from a parent to a child component.
|
|
86
|
+
|
|
87
|
+
**Q. Difference between props and state?**
|
|
88
|
+
Props come from the parent and cannot be changed by the child. State belongs to the component and can change.
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
Solved project: [code/slip9](code/slip9/)
|
fpythons/data/Slip_10.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Slip 10: Validate a number and sum its digits
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Check whether a number is valid, calculate the sum of its digits and display it. Show messages if the value is invalid or blank.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip10/
|
|
13
|
+
src/slip10/SumOfDigits.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip10
|
|
21
|
+
cd slip10
|
|
22
|
+
mkdir src\slip10
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip10/SumOfDigits.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useState } from 'react'
|
|
32
|
+
|
|
33
|
+
const SumOfDigits = () => {
|
|
34
|
+
|
|
35
|
+
const [number, setNumber] = useState('');
|
|
36
|
+
const [result, setResult] = useState('');
|
|
37
|
+
|
|
38
|
+
const handleSubmit = (e) =>{
|
|
39
|
+
e.preventDefault();
|
|
40
|
+
if(number.trim() === ''){
|
|
41
|
+
alert("Please enter a number");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if(!/^\d+$/.test(number)){
|
|
45
|
+
alert("Invalid input! Enter only digits");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let sum = 0;
|
|
49
|
+
for(let digit of number){
|
|
50
|
+
sum += Number(digit);
|
|
51
|
+
}
|
|
52
|
+
setResult(sum);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div>
|
|
57
|
+
<h2>Sum Of Digits</h2>
|
|
58
|
+
<form>
|
|
59
|
+
<label>Enter number:<input type='text' value={number} onChange={(e) => setNumber(e.target.value)}></input></label>
|
|
60
|
+
<button type='submit' onClick={handleSubmit}>Calculate Sum</button><br></br>
|
|
61
|
+
<p>Sum of Digits: {result}</p>
|
|
62
|
+
</form>
|
|
63
|
+
</div>
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default SumOfDigits
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Step 2: `src/App.js`
|
|
71
|
+
|
|
72
|
+
```jsx
|
|
73
|
+
import SumOfDigits from './slip10/SumOfDigits';
|
|
74
|
+
|
|
75
|
+
function App() {
|
|
76
|
+
return <SumOfDigits />;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default App;
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## 5. How it works
|
|
83
|
+
|
|
84
|
+
- A blank value shows an alert.
|
|
85
|
+
- The regular expression `/^\d+$/` allows only digits.
|
|
86
|
+
- A `for...of` loop adds each digit using `Number(digit)`.
|
|
87
|
+
|
|
88
|
+
## 6. Run it
|
|
89
|
+
|
|
90
|
+
```powershell
|
|
91
|
+
npm start
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Enter `1234` (result 10), then blank, then `12a`.
|
|
95
|
+
|
|
96
|
+
## 7. Errors and fixes
|
|
97
|
+
|
|
98
|
+
| Problem | Fix |
|
|
99
|
+
|---|---|
|
|
100
|
+
| Result looks like `01234` | convert each digit with `Number()` |
|
|
101
|
+
| Page reloads | `e.preventDefault()` is missing |
|
|
102
|
+
|
|
103
|
+
## 8. Viva questions
|
|
104
|
+
|
|
105
|
+
**Q. What does `/^\d+$/` mean?**
|
|
106
|
+
From start to end, one or more digits.
|
|
107
|
+
|
|
108
|
+
**Q. What does `trim()` do?**
|
|
109
|
+
Removes spaces at both ends of a string.
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
Solved project: [code/slip10](code/slip10/)
|
fpythons/data/Slip_11.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Slip 11: String operations
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Take a string from the user and show: uppercase, lowercase, length and reverse.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip11/
|
|
13
|
+
src/slip11/StringOperation.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip11
|
|
21
|
+
cd slip11
|
|
22
|
+
mkdir src\slip11
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip11/StringOperation.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useState } from 'react'
|
|
32
|
+
|
|
33
|
+
const StringOperation = () => {
|
|
34
|
+
|
|
35
|
+
const [str, setStr] = useState('');
|
|
36
|
+
const [result, setResult] = useState('');
|
|
37
|
+
|
|
38
|
+
const handleSubmit = (e) => {
|
|
39
|
+
e.preventDefault();
|
|
40
|
+
if(str.trim() === ''){
|
|
41
|
+
alert("Enter a string");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
setResult(str);
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
return (
|
|
49
|
+
<div>
|
|
50
|
+
<h2>String Operations</h2>
|
|
51
|
+
<form onSubmit={handleSubmit}>
|
|
52
|
+
<label>Enter string<input type='text' value={str} onChange={(e) => setStr(e.target.value)}></input></label>
|
|
53
|
+
<button type='submit'>Submit</button>
|
|
54
|
+
<p>String in UpperCase: {result.toUpperCase()}</p>
|
|
55
|
+
<p>String in LowerCae: {result.toLowerCase()}</p>
|
|
56
|
+
<p>Length of the string: {result.length}</p>
|
|
57
|
+
<p>Reverse string : {result.split('').reverse().join('')}</p>
|
|
58
|
+
</form>
|
|
59
|
+
</div>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default StringOperation
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Step 2: `src/App.js`
|
|
67
|
+
|
|
68
|
+
```jsx
|
|
69
|
+
import StringOperation from './slip11/StringOperation';
|
|
70
|
+
|
|
71
|
+
function App() {
|
|
72
|
+
return <StringOperation />;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export default App;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## 5. How it works
|
|
79
|
+
|
|
80
|
+
- `str` is the typed text, `result` is the submitted text.
|
|
81
|
+
- `toUpperCase()`, `toLowerCase()` and `length` are built into strings.
|
|
82
|
+
- Reverse: `split('')` makes an array, `reverse()` reverses it, `join('')` makes a string again.
|
|
83
|
+
|
|
84
|
+
## 6. Run it
|
|
85
|
+
|
|
86
|
+
```powershell
|
|
87
|
+
npm start
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Enter `React`: REACT, react, 5, tcaeR.
|
|
91
|
+
|
|
92
|
+
## 7. Errors and fixes
|
|
93
|
+
|
|
94
|
+
| Problem | Fix |
|
|
95
|
+
|---|---|
|
|
96
|
+
| `str.reverse is not a function` | split into an array first |
|
|
97
|
+
| `toUpperCase is not a function` | keep the state a string: `useState('')` |
|
|
98
|
+
|
|
99
|
+
## 8. Viva questions
|
|
100
|
+
|
|
101
|
+
**Q. Are strings mutable in JavaScript?**
|
|
102
|
+
No. String methods return a new string.
|
|
103
|
+
|
|
104
|
+
**Q. Difference between `split` and `join`?**
|
|
105
|
+
`split` turns a string into an array, `join` turns an array into a string.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
Solved project: [code/slip11](code/slip11/)
|