Advertisement
Guest User

DP#164

a guest
May 26th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <body>
  3. <script>
  4. //--------------------------------------
  5. function message(input_message){
  6.     document.write(input_message);
  7.     };
  8.  
  9. function three_five(maxlen){
  10.     var nums = [];
  11.     var num = 1;
  12.     while (nums.length < maxlen){
  13.         if (num%3==0&&num%5==0){
  14.             nums.push(num);
  15.         }
  16.         num = num + 1
  17.      
  18.     }
  19.     return nums;
  20. };
  21.  
  22. //--------------------------------------
  23. function is_anagram(word1,word2){
  24.     if (word1.length != word2.length){
  25.     return false;
  26.     }
  27.     for (var i=0,len=word1.length;i<len;i++){
  28.         if (word1.split(word1[i]).length-1 != word2.split(word1[i]).length-1){
  29.             return false;
  30.         }
  31.     }  
  32.     return true;
  33. };
  34.  
  35. //--------------------------------------
  36. function replace_letter(word,letter){
  37.     while (word.indexOf(letter) != -1){
  38.         word = word.replace(letter,"");
  39.     }
  40.     return word;
  41. };
  42.  
  43. //--------------------------------------
  44. function sum_array(array){
  45.     var total = 0;
  46.     for (var i=0,len=array.length;i<len;i++){
  47.         total += array[i];
  48.     }
  49.     return total;
  50. }
  51. //--------------------------------------
  52. function bubble_sort(array){
  53.     var sorted = false;
  54.     while (sorted == false){
  55.         sorted = true;
  56.         for (var i=0,len=array.length;i<len;i++){
  57.             if (array[i] > array[i+1]){
  58.                 sorted = false;
  59.                 var x = array[i+1];
  60.                 array[i+1] = array[i];
  61.                 array[i] = x;
  62.                
  63.             }
  64.         }
  65.     }
  66.     return array;
  67. }
  68.  
  69. document.write("--Output 'Hello World' to the console.<br>")
  70. message("Hello World<p>");
  71. document.write("--Return an array of the first 100 numbers that are divisible by 3 and 5.<br>")
  72. document.write(three_five(100));
  73. document.write("<p>")
  74. document.write("--Create a program that verifies if a word is an anagram of another word.<br>")
  75. document.write("word1 = 'racecar'<br>word2='carrace'<br>result = ");
  76. document.write(is_anagram("racecar","carrace"));
  77. document.write("<p>");
  78. document.write("--Create a program that removes a specificed letter from a word.<br>")
  79. document.write("word = 'aaaaaaaeeeeaaa'<br>letter = 'a'<br>result = ");
  80. document.write(replace_letter("aaaaaaaeeeeaaa","a"));
  81. document.write("<p>");
  82. document.write("--Sum all the elements of an array.<br>")
  83. document.write("array = [1,2,3,4,5,6,7,8,9,10]<br>result = ")
  84. document.write(sum_array([1,2,3,4,5,6,7,8,9,10]))
  85. document.write("<p>");
  86. document.write("--Implement a bubble-sort.<br>");
  87. document.write("array = [3,1,2,7,5,4,9,10,8]<br>")
  88. document.write(bubble_sort([3,1,2,7,5,4,9,10,8]));
  89.  
  90. </script>
  91. </body>
  92. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement