random name generator in svelte in javascript

To generate a random name in Svelte, you can use a list of names and a random number generator. Here's an implementation using JavaScript and Svelte:

<script>
  let names = ['Alice', 'Bob', 'Charlie', 'David', 'Eva'];

  function generateName() {
    const randomIndex = Math.floor(Math.random() * names.length);
    return names[randomIndex];
  }
</script>

<button on:click={generateName}>Generate Name</button>
<p>The generated name is: {generateName()}</p>
311 chars
12 lines

In this implementation, the generateName function picks a random name from the names array and returns it. We can call it in response to a button click, and display the generated name in a paragraph element.

Of course, you can customize the list of names to suit your needs, or even load it from an external file or API.

gistlibby LogSnag