exercises/w10exceptions-svelte/src/App.svelte
2025-05-06 14:20:03 +09:00

48 lines
961 B
Svelte

<script>
// @ts-nocheck
let people = 1;
function getPecentage(divisor) {
if (divisor == 0) throw Error('Division by zero is not allowed');
if (divisor < 0) throw Error('negative numbers are not allowed');
return (100 / divisor).toFixed(0);
}
let cakeDivided = getPecentage(people);
function update() {
try {
cakeDivided = getPecentage(people);
} catch (error) {
alert(error.message);
}
}
</script>
<main>
<h3>In how many parts shall we divide the cake?</h3>
<input type="number" bind:value={people} placeholder="Enter a number" />
<button on:click={update}>Update</button>
<p>
Each person eats <span class="highlight">{cakeDivided}%</span> of the cake
</p>
</main>
<style>
input {
padding: 0.5rem;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
width: 60%;
max-width: 200px;
margin-top: 1rem;
}
.highlight {
color: #ff6347;
}
</style>