Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // ----------------------------------------
  12. // Name: Shawon Roberts
  13. // ---------------------------------------
  14. // --------------------------------------
  15. // Exercise: Property Value Evaluation
  16. // --------------------------------------
  17.  
  18. var obj = {
  19. a:1,
  20. b:{
  21. c: 2,
  22. d: 3
  23. }
  24. };
  25.  
  26.  
  27. function propertyValueAt(obj, key){
  28. for (key in obj) {
  29. if (obj.hasOwnProperty(key)) {
  30. console.log(key + " -> " + obj[key]);
  31. }
  32. }
  33. }
  34.  
  35. propertyValueAt(obj, ['a']);
  36.  
  37. // ----------------------------------------
  38. // Name: Shawon Roberts
  39. // ---------------------------------------
  40. // --------------------------------------
  41. // Exercise: fizzbuzz
  42. // --------------------------------------
  43.  
  44. var n = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
  45. function fizzbuzz (n) {
  46. for(var i = 1; i < n.length; i++){
  47. if( i % 3 === 0 && i % 5 === 0){
  48. console.log(i + "fizzbuzz");
  49. }else if( i % 3 === 0){
  50. console.log(i + "fizz");
  51. }else if( i % 5 === 0){
  52. console.log(i + "buzz");
  53. }
  54. else{
  55. console.log(i);
  56. }
  57. }
  58. }
  59. console.log(fizzbuzz(n))
  60.  
  61.  
  62. // ----------------------------------------
  63. // Name: Shawon Roberts
  64. // ---------------------------------------
  65. // --------------------------------------
  66. // Exercise: Anagram tester
  67. // --------------------------------------
  68.  
  69. function anagram(stringA, stringB){
  70. return cleanString(stringA) === cleanString(stringB);
  71. }
  72. function cleanString(str){
  73. return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
  74. }
  75. console.log(anagram('abc', 'bca'));
  76.  
  77. // ----------------------------------------
  78. // Name: Shawon Roberts
  79. // ---------------------------------------
  80. // --------------------------------------
  81. // Exercise: Sum nested
  82. // --------------------------------------
  83.  
  84.  
  85. function sumNested(i) {
  86. var sum=0;
  87. for(var a=0;a<i.length;a++){
  88. if(typeof i[a]=="number"){
  89. sum+=i[a];
  90. }else if(i[a] instanceof Array){
  91. sum+=arraySum(i[a]);
  92. }
  93. }
  94. return sum;
  95. }
  96.  
  97.  
  98. // ----------------------------------------
  99. // Name: Shawon Roberts
  100. // ---------------------------------------
  101. // --------------------------------------
  102. // Exercise: WordCount
  103. // --------------------------------------
  104.  
  105.  
  106. function wordCount(str) {
  107. var count = 0;
  108. for (var i = 1; i <= str.length; i++) {
  109. if (str.charAt(i) == " ") {
  110. count ++;
  111. }
  112. }
  113. return count + 1;
  114. }
  115. console.log(wordCount("This is the sentence I want to test my word count on"));
  116. </script>
  117.  
  118.  
  119.  
  120. <script id="jsbin-source-javascript" type="text/javascript">// ----------------------------------------
  121. // Name: Shawon Roberts
  122. // ---------------------------------------
  123. // --------------------------------------
  124. // Exercise: Property Value Evaluation
  125. // --------------------------------------
  126.  
  127. var obj = {
  128. a:1,
  129. b:{
  130. c: 2,
  131. d: 3
  132. }
  133. };
  134.  
  135.  
  136. function propertyValueAt(obj, key){
  137. for (key in obj) {
  138. if (obj.hasOwnProperty(key)) {
  139. console.log(key + " -> " + obj[key]);
  140. }
  141. }
  142. }
  143.  
  144. propertyValueAt(obj, ['a']);
  145.  
  146. // ----------------------------------------
  147. // Name: Shawon Roberts
  148. // ---------------------------------------
  149. // --------------------------------------
  150. // Exercise: fizzbuzz
  151. // --------------------------------------
  152.  
  153. var n = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
  154. function fizzbuzz (n) {
  155. for(var i = 1; i < n.length; i++){
  156. if( i % 3 === 0 && i % 5 === 0){
  157. console.log(i + "fizzbuzz");
  158. }else if( i % 3 === 0){
  159. console.log(i + "fizz");
  160. }else if( i % 5 === 0){
  161. console.log(i + "buzz");
  162. }
  163. else{
  164. console.log(i);
  165. }
  166. }
  167. }
  168. console.log(fizzbuzz(n))
  169.  
  170.  
  171. // ----------------------------------------
  172. // Name: Shawon Roberts
  173. // ---------------------------------------
  174. // --------------------------------------
  175. // Exercise: Anagram tester
  176. // --------------------------------------
  177.  
  178. function anagram(stringA, stringB){
  179. return cleanString(stringA) === cleanString(stringB);
  180. }
  181. function cleanString(str){
  182. return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
  183. }
  184. console.log(anagram('abc', 'bca'));
  185.  
  186. // ----------------------------------------
  187. // Name: Shawon Roberts
  188. // ---------------------------------------
  189. // --------------------------------------
  190. // Exercise: Sum nested
  191. // --------------------------------------
  192.  
  193.  
  194. function sumNested(i) {
  195. var sum=0;
  196. for(var a=0;a<i.length;a++){
  197. if(typeof i[a]=="number"){
  198. sum+=i[a];
  199. }else if(i[a] instanceof Array){
  200. sum+=arraySum(i[a]);
  201. }
  202. }
  203. return sum;
  204. }
  205.  
  206.  
  207. // ----------------------------------------
  208. // Name: Shawon Roberts
  209. // ---------------------------------------
  210. // --------------------------------------
  211. // Exercise: WordCount
  212. // --------------------------------------
  213.  
  214.  
  215. function wordCount(str) {
  216. var count = 0;
  217. for (var i = 1; i <= str.length; i++) {
  218. if (str.charAt(i) == " ") {
  219. count ++;
  220. }
  221. }
  222. return count + 1;
  223. }
  224. console.log(wordCount("This is the sentence I want to test my word count on"));
  225. </script></body>
  226. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement