Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 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.  
  117.  
  118. // ------------------------------------------------------------------
  119. // Name: Shawon Roberts
  120. // ------------------------------------------------------------------
  121.  
  122. // ------------------------------------------------------------------
  123. // Exercise: Object Oriented Programming - Car
  124. // ------------------------------------------------------------------
  125.  
  126. // the following is in EC5
  127.  
  128. function Car(speed){ // constructor function to create instances of Car
  129. this._speed = speed; //sets parameter speed to be the instance's speed
  130.  
  131. getSpeed() { // returns the value of speed in the object instance
  132. return this._speed;
  133. };
  134.  
  135. setSpeed(speed) { // sets speed in the object instance to be equal to the value passed in the method into if speed is greater than 0
  136. if (speed >=0){
  137. this._speed = speed;
  138. }
  139. };
  140.  
  141. stop() { // sets the speed in the object instance to 0
  142. this._speed = 0;
  143. };
  144. }
  145. </script>
  146.  
  147.  
  148.  
  149. <script id="jsbin-source-javascript" type="text/javascript">// ----------------------------------------
  150. // Name: Shawon Roberts
  151. // ---------------------------------------
  152. // --------------------------------------
  153. // Exercise: Property Value Evaluation
  154. // --------------------------------------
  155.  
  156. var obj = {
  157. a:1,
  158. b:{
  159. c: 2,
  160. d: 3
  161. }
  162. };
  163.  
  164.  
  165. function propertyValueAt(obj, key){
  166. for (key in obj) {
  167. if (obj.hasOwnProperty(key)) {
  168. console.log(key + " -> " + obj[key]);
  169. }
  170. }
  171. }
  172.  
  173. propertyValueAt(obj, ['a']);
  174.  
  175. // ----------------------------------------
  176. // Name: Shawon Roberts
  177. // ---------------------------------------
  178. // --------------------------------------
  179. // Exercise: fizzbuzz
  180. // --------------------------------------
  181.  
  182. var n = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
  183. function fizzbuzz (n) {
  184. for(var i = 1; i < n.length; i++){
  185. if( i % 3 === 0 && i % 5 === 0){
  186. console.log(i + "fizzbuzz");
  187. }else if( i % 3 === 0){
  188. console.log(i + "fizz");
  189. }else if( i % 5 === 0){
  190. console.log(i + "buzz");
  191. }
  192. else{
  193. console.log(i);
  194. }
  195. }
  196. }
  197. console.log(fizzbuzz(n))
  198.  
  199.  
  200. // ----------------------------------------
  201. // Name: Shawon Roberts
  202. // ---------------------------------------
  203. // --------------------------------------
  204. // Exercise: Anagram tester
  205. // --------------------------------------
  206.  
  207. function anagram(stringA, stringB){
  208. return cleanString(stringA) === cleanString(stringB);
  209. }
  210. function cleanString(str){
  211. return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
  212. }
  213. console.log(anagram('abc', 'bca'));
  214.  
  215. // ----------------------------------------
  216. // Name: Shawon Roberts
  217. // ---------------------------------------
  218. // --------------------------------------
  219. // Exercise: Sum nested
  220. // --------------------------------------
  221.  
  222.  
  223. function sumNested(i) {
  224. var sum=0;
  225. for(var a=0;a<i.length;a++){
  226. if(typeof i[a]=="number"){
  227. sum+=i[a];
  228. }else if(i[a] instanceof Array){
  229. sum+=arraySum(i[a]);
  230. }
  231. }
  232. return sum;
  233. }
  234.  
  235.  
  236. // ----------------------------------------
  237. // Name: Shawon Roberts
  238. // ---------------------------------------
  239. // --------------------------------------
  240. // Exercise: WordCount
  241. // --------------------------------------
  242.  
  243.  
  244. function wordCount(str) {
  245. var count = 0;
  246. for (var i = 1; i <= str.length; i++) {
  247. if (str.charAt(i) == " ") {
  248. count ++;
  249. }
  250. }
  251. return count + 1;
  252. }
  253. console.log(wordCount("This is the sentence I want to test my word count on"));
  254.  
  255.  
  256. // ------------------------------------------------------------------
  257. // Name: Shawon Roberts
  258. // ------------------------------------------------------------------
  259.  
  260. // ------------------------------------------------------------------
  261. // Exercise: Object Oriented Programming - Car
  262. // ------------------------------------------------------------------
  263.  
  264. // the following is in EC5
  265.  
  266. function Car(speed){ // constructor function to create instances of Car
  267. this._speed = speed; //sets parameter speed to be the instance's speed
  268.  
  269. getSpeed() { // returns the value of speed in the object instance
  270. return this._speed;
  271. };
  272.  
  273. setSpeed(speed) { // sets speed in the object instance to be equal to the value passed in the method into if speed is greater than 0
  274. if (speed >=0){
  275. this._speed = speed;
  276. }
  277. };
  278.  
  279. stop() { // sets the speed in the object instance to 0
  280. this._speed = 0;
  281. };
  282. }</script></body>
  283. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement