var MyApp = angular.module('MyApp', []);
MyApp.service('Set', function(){
return {
intoSet: function(arr){
var set = [];
arr.forEach(function(item){
if(set.indexOf(item) < 0){
set.push(item);
}
});
return set;
},
union: function(arrA, arrB){
var setA = this.intoSet(arrA);
var setB = this.intoSet(arrB);
setB.forEach(function(item){
if(setA.indexOf(item) < 0){
setA.push(item);
}
});
return setA;
},
intersection: function(arrA, arrB){
var setA = this.intoSet(arrA);
var setB = this.intoSet(arrB);
var intersected = [];
setA.forEach(function(item){
if(setB.indexOf(item) >= 0){
intersected.push(item);
}
});
return intersected;
}
}
});
MyApp.controller('MyController', function($scope, Set) {
$scope.union = function(){
var arrA = $scope.arrA.split(',');
var arrB = $scope.arrB.split(',');
$scope.result = Set.union(arrA, arrB).toString();
}
$scope.intersection = function(){
var arrA = $scope.arrA.split(',');
var arrB = $scope.arrB.split(',');
$scope.result = Set.intersection(arrA, arrB).toString();
}
});
<div ng-controller="MyController">
<p>
<input type="text" placeholder="Ensimmäinen joukko" ng-model="arrA">
</p>
<p>
<input type="text" placeholder="Toinen joukko" ng-model="arrB">
</p>
<p>
<button ng-click="union()">Unioni</button>
<button ng-click="intersection()">Leikkaus</button>
</p>
<p>
Lopputulos: {{result}}
</p>
</div>