Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrayManipulator(theArray){
- console.log(theArray);
- var processedArray = theArray.filter(function(element){
- var debugOne = (element === 0);
- var debugTwo = (isNaN(element)===false);
- var debugThree = element === 0 || isNaN(element)===false;
- if(/*typeof element == 'number' ||*/ element === 0 || isNaN(element)===false){
- return element;
- }
- });
- console.log(processedArray);//////////////////////////////expected [0,2] but is [2]
- Array.prototype.max = function(){
- return Math.max.apply(null, this);
- };
- Array.prototype.min = function(){
- return Math.min.apply(null, this);
- };
- console.log("Max number:");
- console.log(processedArray.max());
- console.log("Min number:");
- console.log(processedArray.min());
- Array.prototype.frequentElement = function(){
- if(this.length == 0){
- return null;
- }
- var tempMap = {};
- var maxElement = this[0];
- var maxCount = 1;
- for(var i = 0; i < this.length; i++)
- {
- var tempElement = this[i];
- if(tempMap[tempElement] == null){
- tempMap[tempElement] = 1;
- }
- else{
- tempMap[tempElement]++;
- }
- if(tempMap[tempElement] > maxCount)
- {
- maxElement = tempElement;
- maxCount = tempMap[tempElement];
- }
- }
- return maxElement;
- }
- console.log("Most frequent number:");
- console.log(processedArray.frequentElement());
- console.log("Sorted descending array:");
- processedArray.sort(function(a, b){return b-a});
- for (i=0;i<processedArray.length;i++)
- {
- console.log(processedArray[i]);
- }
- }
- //arrayManipulator(["Pesho", 2, "Gosho", 12, 2, "true", 9, undefined, 0, "Penka", { bunniesCount : 10}, [10, 20, 30, 40]]);
- arrayManipulator([0, 2, "test"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement