2018-12-30 19:05:56 +05:30
|
|
|
---
|
|
|
|
id: 5a23c84252665b21eecc8042
|
|
|
|
title: Sum of squares
|
|
|
|
challengeType: 5
|
2019-08-05 09:17:33 -07:00
|
|
|
forumTopicId: 302334
|
2018-12-30 19:05:56 +05:30
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-03-30 11:23:18 -05:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
<section id='description'>
|
2019-07-18 17:32:12 +02:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
Write a function to find the sum of squares of an array of integers.
|
2020-03-30 11:23:18 -05:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
2020-03-30 11:23:18 -05:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
2020-03-30 11:23:18 -05:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
<section id='tests'>
|
|
|
|
|
2020-03-30 11:23:18 -05:00
|
|
|
```yml
|
2018-12-30 19:05:56 +05:30
|
|
|
tests:
|
|
|
|
- text: <code>sumsq</code> should be a function.
|
2020-03-30 11:23:18 -05:00
|
|
|
testString: assert(typeof sumsq == 'function');
|
2018-12-30 19:05:56 +05:30
|
|
|
- text: <code>sumsq([1, 2, 3, 4, 5])</code> should return a number.
|
2020-03-30 11:23:18 -05:00
|
|
|
testString: assert(typeof sumsq([1, 2, 3, 4, 5]) == 'number');
|
2018-12-30 19:05:56 +05:30
|
|
|
- text: <code>sumsq([1, 2, 3, 4, 5])</code> should return <code>55</code>.
|
2020-03-30 11:23:18 -05:00
|
|
|
testString: assert.equal(sumsq([1, 2, 3, 4, 5]), 55);
|
2018-12-30 19:05:56 +05:30
|
|
|
- text: <code>sumsq([25, 32, 12, 7, 20])</code> should return <code>2242</code>.
|
2020-03-30 11:23:18 -05:00
|
|
|
testString: assert.equal(sumsq([25, 32, 12, 7, 20]), 2242);
|
2018-12-30 19:05:56 +05:30
|
|
|
- text: <code>sumsq([38, 45, 35, 8, 13])</code> should return <code>4927</code>.
|
2020-03-30 11:23:18 -05:00
|
|
|
testString: assert.equal(sumsq([38, 45, 35, 8, 13]), 4927);
|
2018-12-30 19:05:56 +05:30
|
|
|
- text: <code>sumsq([43, 36, 20, 34, 24])</code> should return <code>5277</code>.
|
2020-03-30 11:23:18 -05:00
|
|
|
testString: assert.equal(sumsq([43, 36, 20, 34, 24]), 5277);
|
2018-12-30 19:05:56 +05:30
|
|
|
- text: <code>sumsq([12, 33, 26, 18, 1, 16, 3])</code> should return <code>2499</code>.
|
2020-03-30 11:23:18 -05:00
|
|
|
testString: assert.equal(sumsq([12, 33, 26, 18, 1, 16, 3]), 2499);
|
2018-12-30 19:05:56 +05:30
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
2020-03-30 11:23:18 -05:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
<section id='challengeSeed'>
|
2019-07-18 17:32:12 +02:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
2019-03-10 19:14:48 +09:00
|
|
|
function sumsq(array) {
|
2020-09-15 09:57:40 -07:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
2020-03-30 11:23:18 -05:00
|
|
|
|
2018-12-30 19:05:56 +05:30
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2019-03-10 19:14:48 +09:00
|
|
|
function sumsq(array) {
|
2018-12-30 19:05:56 +05:30
|
|
|
var sum = 0;
|
|
|
|
var i, iLen;
|
|
|
|
|
|
|
|
for (i = 0, iLen = array.length; i < iLen; i++) {
|
|
|
|
sum += array[i] * array[i];
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-07-18 17:32:12 +02:00
|
|
|
</section>
|