Create a `isPositive` function that takes a number as
Create a function named `isPositive` that takes a number as a argument, returning `true` if the number is strictly positive, and `false` otherwise.
parameter and return true if the given number is
stricly positive, or false otherwise
Create the `abs` function that takes one number argument
Create a function named `abs` that takes a number as an argument and returns its absolute value. You must make your own implementation. You **must not** use `Math.abs()`.
and returns its absolute value.
You are not allowed to use `Math.abs`, make your own.
Create a `blockChain` that create a block in your very own block chain.
Create a function named `blockChain` that creates a block in your very own block chain. It takes 2 arguments:
the function takes 2 arguments:
- `data`: any valid JSON data.
- `prev`: the previous block, if no block are given it should use the genesis block: `{ index: 0, hash: '0' }`.
- `data` any valid JSON data
- `prev` the previous block, if no block are given it should use the
genesis block: `{ index: 0, hash: '0' }`
A block must have the following properties:
A block must have the following properties:
- `index`
- `index`
- `hash` a computed hash using the concatenation of the `index`, `hash`
- `hash`: a computed hash using the `hashCode` function provided. You will need to pass it a concatenation of the block's `index`, the previous block's `hash` and the block's stringified `data`.
and stringified `data` and hashing all of it using the provided `hashCode`.
- `data`: any valid object.
- `data` the data (not encoded in JSON)
- `prev`: the previous block.
- `prev` the previous block
- `chain`: a function that accepts `data` as an argument, and creates the next block with it.
- `chain` a function that takes a new `data` and create the next block with it.
Someone once said that a dog makes 7 years for each human year.
Someone once said that a human year is equal to 7 dog years.
Create a `dogYears` function that if given a planet name and an age in seconds,
Create a function named `dogYears`, that accepts the name of a planet, and the age of the dog in seconds. Your function should return the age of the dog on that planet in dog years.
calculates how old a dog would be on the given planet.
- `earth`: orbital period 1.0 Earth years, 365.25 Earth days, or 31,557,600 seconds
- `earth`: orbital period 1.0 Earth years, 365.25 Earth days, or 31,557,600 seconds.
- `mercury`: orbital period 0.2408467 Earth years
- `mercury`: orbital period 0.2408467 Earth years.
- `venus`: orbital period 0.61519726 Earth years
- `venus`: orbital period 0.61519726 Earth years.
- `mars`: orbital period 1.8808158 Earth years
- `mars`: orbital period 1.8808158 Earth years.
- `jupiter`: orbital period 11.862615 Earth years
- `jupiter`: orbital period 11.862615 Earth years.
- `saturn`: orbital period 29.447498 Earth years
- `saturn`: orbital period 29.447498 Earth years.
- `uranus`: orbital period 84.016846 Earth years
- `uranus`: orbital period 84.016846 Earth years.
- `neptune`: orbital period 164.79132 Earth years
- `neptune`: orbital period 164.79132 Earth years.
So if you were told someone that their dog were 1,000,000,000 seconds old, you should be able to say that the dog is 221.82 Earth-years old.
If you were told that a dog was 1,000,000,000 seconds old, you should calculate that the dog would be 221.82 Earth-years old.
You will have to format the number so that the result is rounded like the example above.
You will have to format the number so that the result is rounded like the example above.
Hello and welcome to the JS piscine, first you will have to learn
Welcome to the JS piscine 👋.
to execute javascript.
Being a special child, JS can run in different **runtime**, what you can
First you will have to learn to execute JavaScript.
do with it greatly depend of your runtime.
Luckily you don't need to install anything for that since all you
JavaScript can run in different **runtime** environments. What you can do with JavaScript will greatly depend on the runtime. Even different web browsers (Chrome, Firefox, Safari etc) count as different runtime environments.
need is a web browser.
> Main runtime for executing JS are: any web browser, NodeJS and Deno.
We'll start by running JavaScript in your browser, so you don't need to install anything to get started.
Let's make a hello world:
Some common JavaScript runtime environments:
- Web browsers
- [Node.js](https://nodejs.org/)
- [Deno](https://deno.land/)
```bash
Let's make a hello world. First we create the JavaScript file
# first we create the javascript file
echo "console.log('Hello World')" > how-2-js.js
# To run JS in your browser you need to import it from an HTML page:
Now open your browser at the specified port. You'll use an appropriate command for your system:
- Linux: `xdg-open`
- macOS: `open`
- Windows: `start`
```sh
xdg-open 'http://localhost:8000'
xdg-open 'http://localhost:8000'
```
```
> `xdg-open` find your default application for the given argument
> on mac it's just `open` and it's `start` on windows
You can now open your web browser console (`ctrl`+`shift`+`i`)
and you should see your hello world.
> The console is a very handy place to test code and explore how the language
You can now open your web browser console. From Google Chrome, press **Command+Option+J** (Mac) or **Control+Shift+J** (Windows, Linux, ChromeOS), and you should see your hello world.
> works, don't be shy and play in it !
Great ! you are all set, if you want to re-execute your script, just refresh.
The console is a handy place to test your code.
You now just have to create a repository named `((ROOT))`,
You are all set. If you want to re-execute your script, just refresh.
which will hold all your solutions for this piscine
and just add your 2 generated files to it, we will start slow for now... 🐢
### Recommendation
Create a repository named `((ROOT))` which will hold all your solutions for this piscine. Add your 2 generated files to it.
Videos designed to give **hints** are assigned to each quest. It is strongly suggested to watch them as you go.
- a `first` function that takes an array or a string
- `first`: that takes an array or a string and returns its first element or character.
and return the first element.
- a `last` function that takes an array or a string
- `last`: that takes an array or a string and return its last element or character.
and return the last element.
- a `kiss` function that returns an array of 2 elements
- `kiss`: that takes an array or string, and returns an array of 2 elements. The returned array should contain the last and first elements or characters, in that order.
Create the `sign` function that takes one number argument
Create a function named `sign` that takes one number argument; returning `1` if the number is positive, `-1` if the number is negative and `0` if the number is exactly 0.
and return 1 if the number is positive, -1 if the number is negative
> You must not just use `Math.sign`, make your own.
and 0 if the number is exactly 0
You must not just use `Math.sign`, make your own.
Create the `sameSign` function that takes 2 numbers as arguments and return true
Create a function named `sameSign` that takes 2 numbers as arguments and returns `true` if they both have the same sign, or false otherwise.
if they both have the same sign, or false otherwise.