Skip to content

Commit 14c4b05

Browse files
committed
Add function to check for enough points to upgrade item
1 parent d26b8f3 commit 14c4b05

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/App.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CostDisplay from './components/CostDisplay/CostDisplay';
12
import './App.css';
23
import { useState, useEffect } from 'react';
34
function App() {
@@ -15,21 +16,31 @@ function App() {
1516
clearInterval(interval); //clear interval when component unmounts
1617
};
1718
}, [points, pointsPerSecond]);
19+
function checkPointsForUpgrade(points, pointsRequired) {
20+
//check if user has enough points to upgrade
21+
if (pointsRequired !== 0 && points >= pointsRequired) {
22+
//make sure points required is not 0 and user has enough points to upgrade
23+
return true;
24+
} else {
25+
//not enough points required to upgrade
26+
return false;
27+
}
28+
}
1829
function addPointsFromClick() {
1930
//add points from clicking a button
2031
setPoints(points + clickMultiplier); //increase points by 1 when button clicked
2132
setClicks(clicks + clickMultiplier); //increase clicks made by 1
2233
}
2334
function upgradeClicker() {
2435
//upgrade clicker (points per click)
25-
if (points >= 10 * Math.pow(2, clickMultiplier - 1)) {
36+
if (checkPointsForUpgrade(points, 10 * Math.pow(2, clickMultiplier - 1))) {
2637
setPoints(points - 10 * Math.pow(2, clickMultiplier - 1)); //spend points
2738
setClickMultiplier(clickMultiplier + 1); //increase click multiplier by 1
2839
}
2940
}
3041
function upgradePointsPerSecond() {
3142
//upgrade points per second
32-
if (points >= 10 * Math.pow(2, pointsPerSecond)) {
43+
if (checkPointsForUpgrade(points, 10 * Math.pow(2, pointsPerSecond))) {
3344
setPoints(points - 10 * Math.pow(2, pointsPerSecond));
3445
setPointsPerSecond(pointsPerSecond + 1); //increase points per second
3546
}
@@ -53,13 +64,13 @@ function App() {
5364
<button onClick={() => upgradeClicker()}>
5465
Upgrade Clicker (Points Per Click)
5566
<br />
56-
Cost: {10 * Math.pow(2, clickMultiplier - 1)}
67+
<CostDisplay cost={10 * Math.pow(2, clickMultiplier - 1)} />
5768
</button>
5869
{/*upgrade points per second*/}
5970
<button onClick={() => upgradePointsPerSecond()}>
6071
Upgrade Points Per Second
6172
<br />
62-
Cost: {10 * Math.pow(2, pointsPerSecond)}
73+
<CostDisplay cost={10 * Math.pow(2, pointsPerSecond)} />
6374
</button>
6475
</div>
6576
);

0 commit comments

Comments
 (0)