Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Commit

Permalink
Adds 'advanced' algorithm solution to challenge (#1218)
Browse files Browse the repository at this point in the history
* Adds 'advanced' algorithm solution to challenge

closes #1217

* Adds side-effect safe local var to basic and adv

* Adds real side-effect free local var - basic & adv

* Adds ```javascript declaration & update "run code"
  • Loading branch information
Gregory Orton authored and Rafase282 committed Jul 3, 2016
1 parent ba611ac commit cea5a53
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions Algorithm-Pairwise.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ It is easy to confuse indices as being numbers, but since you will be interactin
function pairwise(arr, arg) {
// Set sum of indices to zero
var sum = 0;
// make a local copy of the arguments object so we don't modify it directly
var pairArr = arr.slice();
// looping from first element
for(i = 0; i < arr.length; i++) {
for(i = 0; i < pairArr.length; i++) {
//Looping from second element by setting first element constant
for(j = i + 1; j < arr.length; j++) {
for(j = i + 1; j < pairArr.length; j++) {
// Check whether the sum is equal to arg
if(arr[i] + arr[j] == arg) {
if(pairArr[i] + pairArr[j] == arg) {
//Add the indices
sum += i + j;
//Set the indices to NaN so that they can't be used in next iteration
arr[i] = arr[j] = NaN;
pairArr[i] = pairArr[j] = NaN;
}
}
}
Expand Down Expand Up @@ -148,11 +150,52 @@ pairwise([1,4,2,3,0,5], 7);
- [JS Array Prototype IndexOf](JS-Array-Prototype-IndexOf)
- [JS Array Prototype Push](JS-Array-Prototype-Push)

## :rotating_light: Advanced Code Solution:

````javascript
function pairwise(arr, arg) {
// search array for elements that when paired, equal the second argument, then sum their indices
// make a local copy of the arguments object so we don't modify it directly
var pairArr = arr.slice();
return pairArr.reduce( function (a,b,index){ // use native reduce to collect running total of summed indices
var search = arg - b; // get difference of current item so we know what value will sum to arg

// check if search value in rest of the array, but also make sure it doesn't match current search index
if ( pairArr.indexOf(search) != -1 && pairArr.indexOf(search) != index ){
var total = index + pairArr.indexOf(search); // if found, add to the runnning total
pairArr.splice(index,1,NaN); // remove current index from the array
pairArr.splice(pairArr.indexOf(search),1,NaN); // remove the other matched element from the array
return a + total; //return the running total back to reduce for next item
}
return a; // simply return previous total if no operations needed
},0);

}

// test here
pairwise([1,4,2,3,0,5], 7);
````

:rocket: [Run Code](https://repl.it/CLpC/2)

### Code Explanation:
See comments in code
This code takes advantage of the fact that the native Array.prototype.indexOf() method will return the lowest index of the value it finds, a requirement of the challenge. Given that you start with the first item in the array (automatically the lowest of it's value), you're guaranteed to always find the lowest pairs, before removing them from the search space.

#### Relevant Links
- [JS Array Prototype Reduce](JS-Array-Prototype-Reduce)
- [JS Array Prototype IndexOf](JS-Array-Prototype-IndexOf)
- [JS Array Prototype Splice](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)
- [Global NaN property](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/NaN)
- [JS Reduce Made Easy](JS-Reduce-Made-Easy)

:rocket: [Run Code](https://repl.it/CLpC/1)

### :trophy: Credits:

If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:

**`Thanks @Rafase282 @coded9 @SaintPeter for your help with Algorithm: Pairwise`**
**`Thanks @Rafase282 @coded9 @SaintPeter @geligelu for your help with Algorithm: Pairwise`**

## :clipboard: NOTES FOR CONTRIBUTIONS:

Expand Down

0 comments on commit cea5a53

Please sign in to comment.