Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/problem1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Problem 1: Three Ways to Sum to N

This solution provides three unique JavaScript implementations of `sum_to_n`.

## Files

- `solution.js`: contains all three implementations.

## Implementations

1. `sum_to_n_a`

Iterative loop approach. It loops from `1` to `|n|`, adds each number to an accumulator, and applies the original sign of `n`.

Complexity: `O(n)` time, `O(1)` space.

2. `sum_to_n_b`

Mathematical approach using the Gauss formula:

```js
n * (n + 1) / 2
```

The implementation uses `Math.abs(n)` and reapplies the sign, so negative integers are handled consistently.

Complexity: `O(1)` time, `O(1)` space.

3. `sum_to_n_c`

Array reduce approach. It builds a range from `1` to `|n|`, applies the sign, then sums the values with `reduce`.

Complexity: `O(n)` time, `O(n)` space.

## Notes

The task states that `n` can be any integer. This implementation supports:

- positive integers, for example `sum_to_n(5) === 15`
- zero, where `sum_to_n(0) === 0`
- negative integers, for example `sum_to_n(-3) === -6`

## Quick test

Open `solution.js` in a browser console or include it with a script tag. Then call:

```js
sum_to_n_a(5); // 15
sum_to_n_b(5); // 15
sum_to_n_c(5); // 15
```
45 changes: 45 additions & 0 deletions src/problem1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var sum_to_n_a = function(n) {
// Idea: Iterative approach. Use a loop to add each number from 1 to |n|.
// Complexity: Time O(n), Space O(1).
if (n === 0) {
return 0;
}

var sign = n > 0 ? 1 : -1;
var total = 0;

for (var i = 1; i <= Math.abs(n); i++) {
total += i * sign;
}

return total;
};

var sum_to_n_b = function(n) {
// Idea: Mathematical approach. Use the Gauss formula n * (n + 1) / 2.
// Complexity: Time O(1), Space O(1).
if (n === 0) {
return 0;
}

var absN = Math.abs(n);
var result = absN * (absN + 1) / 2;

return n > 0 ? result : -result;
};

var sum_to_n_c = function(n) {
// Idea: Array reduce approach. Build a range from 1 to |n| and reduce it to a sum.
// Complexity: Time O(n), Space O(n).
if (n === 0) {
return 0;
}

var sign = n > 0 ? 1 : -1;

return Array.from({ length: Math.abs(n) }, function(_, index) {
return (index + 1) * sign;
}).reduce(function(total, current) {
return total + current;
}, 0);
};
35 changes: 35 additions & 0 deletions src/problem2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Problem 2: Fancy Form

A Vite + React currency swap form using live token prices from Switcheo.

## Preview

![Currency swap form preview](./docs/screenshot.png)

## Run locally

```bash
npm install
npm run dev
```

Open the local URL printed by Vite, usually:

```text
http://localhost:5173/
```

## Build

```bash
npm run build
```

## Features

- Live token price loading with fallback demo prices.
- Token selector with token icons and USD prices.
- Automatic receive amount and exchange-rate calculation.
- Input validation for amount, same-token swaps, and insufficient balance.
- Mock swap confirmation with loading state.
- Responsive UI built with Tailwind CSS.
Binary file added src/problem2/docs/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 5 additions & 17 deletions src/problem2/index.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
<html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fancy Form</title>

<!-- You may add more stuff here -->
<link href="style.css" rel="stylesheet" />
</head>

<body>

<!-- You may reorganise the whole HTML, as long as your form achieves the same effect. -->
<form onsubmit="return !1">
<h5>Swap</h5>
<label for="input-amount">Amount to send</label>
<input id="input-amount" />

<label for="output-amount">Amount to receive</label>
<input id="output-amount" />

<button>CONFIRM SWAP</button>
</form>
<script src="script.js"></script>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>

</html>
Loading