2018-10-04 13:37:37 +00:00
|
|
|
---
|
|
|
|
title: Greatest common divisor
|
|
|
|
id: 5a23c84252665b21eecc7e82
|
|
|
|
challengeType: 5
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
Write a function that returns the greatest common divisor of two integers.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd</code> should be a function.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert(typeof gcd=='function','<code>gcd</code> should be a function.');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd(24,36)</code> should return a number.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert(typeof gcd(24,36)=='number','<code>gcd(24,36)</code> should return a number.');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd(24,36)</code> should return <code>12</code>.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(gcd(24,36),12,'<code>gcd(24,36)</code> should return <code>12</code>.');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd(30,48)</code> should return <code>6</code>.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(gcd(30,48),6,'<code>gcd(30,48)</code> should return <code>6</code>.');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd(10,15)</code> should return <code>5</code>.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(gcd(10,15),5,'<code>gcd(10,15)</code> should return <code>5</code>.');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd(100,25)</code> should return <code>25</code>.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(gcd(100,25),25,'<code>gcd(100,25)</code> should return <code>25</code>.');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd(13,250)</code> should return <code>1</code>.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(gcd(13,250),1,'<code>gcd(13,250)</code> should return <code>1</code>.');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>gcd(1300,250)</code> should return <code>50</code>.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(gcd(1300,250),50,'<code>gcd(1300,250)</code> should return <code>50</code>.');
|
2018-10-04 13:37:37 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function gcd(a, b) {
|
|
|
|
// Good luck!
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
function gcd(a, b) {
|
|
|
|
return b==0 ? Math.abs(a):gcd(b, a % b);
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|