Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. function arrayManipulator(theArray){
  2. console.log(theArray);
  3. var processedArray = theArray.filter(function(element){
  4. var debugOne = (element === 0);
  5. var debugTwo = (isNaN(element)===false);
  6. var debugThree = element === 0 || isNaN(element)===false;
  7. if(/*typeof element == 'number' ||*/ element === 0 || isNaN(element)===false){
  8. return element;
  9. }
  10. });
  11.  
  12.  
  13.  
  14. console.log(processedArray);//////////////////////////////expected [0,2] but is [2]
  15.  
  16. Array.prototype.max = function(){
  17. return Math.max.apply(null, this);
  18. };
  19.  
  20. Array.prototype.min = function(){
  21. return Math.min.apply(null, this);
  22. };
  23.  
  24. console.log("Max number:");
  25. console.log(processedArray.max());
  26. console.log("Min number:");
  27. console.log(processedArray.min());
  28.  
  29. Array.prototype.frequentElement = function(){
  30. if(this.length == 0){
  31. return null;
  32. }
  33. var tempMap = {};
  34. var maxElement = this[0];
  35. var maxCount = 1;
  36. for(var i = 0; i < this.length; i++)
  37. {
  38. var tempElement = this[i];
  39.  
  40. if(tempMap[tempElement] == null){
  41. tempMap[tempElement] = 1;
  42. }
  43.  
  44. else{
  45. tempMap[tempElement]++;
  46. }
  47.  
  48. if(tempMap[tempElement] > maxCount)
  49. {
  50. maxElement = tempElement;
  51. maxCount = tempMap[tempElement];
  52. }
  53. }
  54. return maxElement;
  55. }
  56.  
  57. console.log("Most frequent number:");
  58. console.log(processedArray.frequentElement());
  59. console.log("Sorted descending array:");
  60. processedArray.sort(function(a, b){return b-a});
  61.  
  62. for (i=0;i<processedArray.length;i++)
  63. {
  64. console.log(processedArray[i]);
  65. }
  66.  
  67. }
  68.  
  69. //arrayManipulator(["Pesho", 2, "Gosho", 12, 2, "true", 9, undefined, 0, "Penka", { bunniesCount : 10}, [10, 20, 30, 40]]);
  70.  
  71. arrayManipulator([0, 2, "test"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement