create a page that takes in a planet from a menu with all planets in the solar system and a weight in kg in svelte, and shows the heaviness in newtons in javascript in javascript

<!-- PlanetSelect.svelte -->
<script>
  const planets = [
    { name: 'Mercury', weight: 0.38 },
    { name: 'Venus', weight: 0.91 },
    { name: 'Earth', weight: 1 },
    { name: 'Mars', weight: 0.38 },
    { name: 'Jupiter', weight: 2.34 },
    { name: 'Saturn', weight: 1.06 },
    { name: 'Uranus', weight: 0.92 },
    { name: 'Neptune', weight: 1.19 }
  ];

  let selectedPlanet = planets[0];
  let inputWeight = 0;
  let result = 0;

  function calculateWeight() {
    result = inputWeight * selectedPlanet.weight * 9.8;
  }
</script>

<main>
  <form on:submit|preventDefault="{calculateWeight}">
    <fieldset>
      <label for="planet-select">Select a planet:</label>
      <select id="planet-select" bind:value="{selectedPlanet}">
        {#each planets as planet}
          <option value="{planet}">{planet.name}</option>
        {/each}
      </select>
    </fieldset>
    <fieldset>
      <label for="weight-input">Enter your weight (kg):</label>
      <input type="number" id="weight-input" bind:value="{inputWeight}" />
    </fieldset>
    <button type="submit">Calculate</button>
    {#if result > 0}
      <p>The actual weight in newtons is: {result.toFixed(2)}</p>
    {/if}
  </form>
</main>
1210 chars
43 lines

The PlanetSelect.svelte component display a form that allows the user to select a planet from a menu of all planets in the solar system and enter their weight in kg. Upon submitting the form, the component calculates the actual weight in newtons according to the selected planet's gravity and display the result on the page.

The planet's data is stored in an array of objects, each object contains the planet name and weight relative to earth's gravity. The selected planet and user input weight is stored in reactive variables using the let keyword and the bind directive for two-way binding.

The calculateWeight function is called when the user submits the form. It multiplies the user input weight with the selected planet's weight relative to earth and the gravitational acceleration constant of 9.8 m/s² to get the actual weight in newtons, which is stored in the result variable.

The result variable is conditionally rendered using the if block to display the result message when the result value is greater than 0.

This way, the user can quickly check their weight on other planets by selecting from the drop-down menu and entering their weight on earth, which makes it easy to perform the necessary calculations.

gistlibby LogSnag