diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx index 488bc934..949fff4c 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx @@ -2,9 +2,68 @@ import BatteryInput from "~/components/batteryInput"; import Header from "~/components/header"; import SpeedInput from "~/components/speedInput"; import WeatherInput from "~/components/weatherInput"; +import React, { useState } from "react"; const App = () => { + const[speed, setSpeed] = useState(""); + const[battery, setBattery] = useState(""); + const[weather, setWeather] = useState(0); + const[range, setRange] = useState(0); + + // Error messages + const [speedError, setSpeedError] = useState("") + const [batteryError, setBatteryError] = useState("") + + //to avoid the type errors: + const handleSpeedChange = (e: React.ChangeEvent) => { + const value = e.target.value; + setSpeed(value === "" ? "" : Number(value)) + console.log("Speed:", value); // Add this line + + } + const handleBatteryChange = (e: React.ChangeEvent) => { + const value = e.target.value; + // Allow empty input, or convert to a number + setBattery(value === "" ? "" : Number(value)); + console.log("Battery:", value); // Add this line + + }; + const validInputs = () =>{ + let valid = true; + + //reset battery and speed + setSpeedError(""); + setBatteryError(""); + + //check the input + if(speed === ""){ + valid = false; + setSpeedError("Speed is required"); + }else if(typeof speed === 'number' && (speed > 90 || speed < 0)){ + valid = false; + setSpeedError("The speed should be within the range of 0 to 90"); + } + + if(battery === ""){ + valid = false; + setSpeedError("Battery is required"); + } + else if (typeof battery === "number" && (battery < 0 || battery > 100)) { + valid = false; + setSpeedError("The battery percentage should be within the range of 0 to 100"); + } + return valid; + } const calculateRange = () => { + //range = -(s * s * b / 2500) + (4 * b) + w + //Where s = speed, b = battery percentage w = weather + if(validInputs()){ //only calculates if the inputs are valid + const calculatedRange = -(Number(speed) * Number(speed) * Number(battery) / 2500) + (4 * Number(battery)) + weather; + setRange(calculatedRange); + }else { + setRange(-1); // Reset range if inputs are invalid + } + return; } @@ -14,12 +73,31 @@ const App = () => {
- - + + {speedError &&
{speedError}
} {/* speed error message */} + + + {batteryError &&
{batteryError}
} {/* Battery error message */} +
+ + {/*make the button*/} +
+ + {range !== -1 && ( +
+

The predicted range of the Eylsia is {range.toFixed(2)} km.

+
+ )} +
+
diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx index ca4586c0..33fb5777 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx @@ -1,4 +1,9 @@ -const BatteryInput = () => { +interface BatteryInputProps { + value: number | string; // Expecting a string for the input value + onChange: (e: React.ChangeEvent) => void; // Function to handle changes +} + +const BatteryInput: React.FC = ({ value, onChange }) => { return (
@@ -8,6 +13,9 @@ const BatteryInput = () => { name="battery" type="number" placeholder="Battery" + value={value} + onChange={onChange} + required />
); diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx index aa381c14..f321857d 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx @@ -1,4 +1,10 @@ -const SpeedInput = () => { +// Define the props type +interface SpeedInputProps { + value: number | string; // Expecting a string for the input value + onChange: (e: React.ChangeEvent) => void; // Function to handle changes +} + +const SpeedInput: React.FC = ({ value, onChange }) => { return ( <>
@@ -9,6 +15,9 @@ const SpeedInput = () => { name="speed" type="number" placeholder="Speed" + value={value} + onChange={onChange} + required />
diff --git a/hello.txt b/hello.txt new file mode 100644 index 00000000..3266b2d2 --- /dev/null +++ b/hello.txt @@ -0,0 +1 @@ +hello world, hello world diff --git a/ideen.txt b/ideen.txt index 980a0d5f..2ae12dda 100644 --- a/ideen.txt +++ b/ideen.txt @@ -1 +1,2 @@ Hello World! +Hello Ideen!