Here's a sample code that takes in a planet and weight input from the user, and calculates the weight in newtons based on the gravity of the selected planet, using JavaScript in a Svelte app.
1199 chars54 lines
In this code, we first define an array of objects, each representing a planet and its surface gravity in m/s². We then create reactive variables for the selected planet, weight input, and the computed weight in newtons.
When the user inputs a weight value, we search for the selected planet's gravity in the array, and multiply the weight value with the gravity to compute the weight in newtons. We update the result
variable with the new computed value, and display it in the UI.
You can style the UI elements with CSS as per your requirement. After compiling the code, you can test it by changing the planet and weight inputs in the form and checking the calculated newtons value.
gistlibby LogSnag