Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. # Whiteboard questions/answers
  2. ----
  3.  
  4. ## Reverse string
  5.  
  6. --- Directions
  7.  
  8. Given a string, return a new string with the reversed
  9. order of characters
  10.  
  11. --- Examples
  12. ```js
  13. reverse('apple') === 'leppa'
  14. reverse('hello') === 'olleh'
  15. reverse('Greetings!') === '!sgniteerG'
  16. ```
  17.  
  18. <details>
  19. <summary>Answer(s)</summary>
  20.  
  21. ```js
  22. function reverse(str) {
  23.  
  24. let reversed = "";
  25. for (let i = str.length - 1; i >= 0; i--) {
  26. reversed += str[i];
  27. }
  28. return reversed;
  29.  
  30. }
  31. ```
  32. ----
  33. ```js
  34. function reverse(str) {
  35. return str.split('').reverse().join('');
  36. }
  37. ```
  38. ----
  39. ```js
  40. function reverse(str) {
  41. let reversed = '';
  42.  
  43. for (let character of str) {
  44. reversed = character + reversed;
  45. }
  46.  
  47. return reversed;
  48.  
  49. }
  50. ```
  51. ----
  52.  
  53. ### FANCY::::::ES2015
  54. ```js
  55. function reverse(str) {
  56. return str.split('').reduce((reversed, char) => {
  57. return character + reversed;
  58. }, '');
  59. }
  60. ```
  61.  
  62. ----
  63.  
  64. ### Recursive:
  65. ```js
  66. function reverse(str) {
  67. // If length of 1, return the string
  68. if (str.length <= 1) {
  69. return str;
  70. }
  71.  
  72. // Otherwise, call reverse on the string minus it's 1st characters
  73. // And add it to the first character
  74. return reverse(str.substr(1)) + str[0];
  75. }
  76. // EX: reverse('bar');
  77. // will call reverse on 'ar' and add 'b'
  78. // will call reverse on 'r' and add 'a'
  79. // will return 'r' since its a single character
  80. // will move back through, adding 'r' to 'a'
  81. // will continue, adding 'ra' to 'b'
  82. // will finally return 'rab'
  83. ```
  84.  
  85. </details>
  86.  
  87. ----
  88.  
  89. ## Palindrome
  90.  
  91. --- Directions
  92.  
  93. Given a string, return true if the string is a palindrome
  94. or false if it is not. Palindromes are strings that
  95. form the same word if it is reversed. *Do* include spaces
  96. and punctuation in determining if the string is a palindrome.
  97.  
  98. --- Examples
  99.  
  100. ```js
  101. palindrome("abba") === true
  102. palindrome("abcdefg") === false
  103. ```
  104.  
  105.  
  106. <details>
  107. <summary>Answer(s)</summary>
  108.  
  109. ```js
  110. function palindrome(str) {
  111.  
  112. let reversed = str.split('').reverse().join('');
  113. return str === reversed;
  114.  
  115. }
  116. ```
  117. ----
  118. This solution does more work than is necessary
  119. because it iterates over every element, which is about twice what is needed
  120. ```js
  121. function palindrome(str) {
  122.  
  123. return str.split('').every((char, i) => {
  124. return char === str[str.length - i - 1];
  125. });
  126.  
  127. }
  128. ```
  129. </details>
  130.  
  131. ----
  132.  
  133. ## MaxChar
  134.  
  135. --- Directions
  136.  
  137. Given a string, return the character that is most
  138. commonly used in the string.
  139.  
  140. --- Examples
  141.  
  142. ```js
  143. maxChar("abcccccccd") === "c"
  144. maxChar("apple 1231111") === "1"
  145. ```
  146.  
  147. <details>
  148. <summary>Answer(s)</summary>
  149.  
  150. ```js
  151. function maxChar(str) {
  152.  
  153. const obj = {};
  154. let max = '';
  155.  
  156. for (let char of str) {
  157. if (!obj[char])
  158. obj[char] = 1;
  159. else
  160. obj[char]++;
  161. }
  162.  
  163. for (let char in obj) {
  164. if (!max)
  165. max = char;
  166. else {
  167. max = obj[max] > obj[char] ? max : char;
  168. }
  169. }
  170.  
  171. return max;
  172.  
  173. }
  174. ```
  175.  
  176. ----
  177. Alternate (better) solutions:
  178.  
  179. ```js
  180. function maxChar(str) {
  181.  
  182. const chars = {};
  183. let max = 0;
  184. let maxChar = '';
  185.  
  186. for (let char of str) {
  187. // If chars[char] is null/falsey then make it be 1
  188. // otherwise add 1 to the count
  189. chars [char] = chars[char] + 1 || 1;
  190. }
  191.  
  192. for (let char in chars) {
  193. if (chars[char] > max) {
  194. max = chars[char];
  195. maxChar = char;
  196. }
  197. }
  198.  
  199. }
  200. ```
  201.  
  202. </details>
  203.  
  204. ----
  205.  
  206. ## FizzBuzz
  207.  
  208. --- Directions
  209.  
  210. Write a program that console logs the numbers
  211. from 1 to n. But for multiples of three print
  212. “fizz” instead of the number and for the multiples
  213. of five print “buzz”. For numbers which are multiples
  214. of both three and five print “fizzbuzz”.
  215.  
  216. --- Example
  217.  
  218. ```js
  219. fizzBuzz(5);
  220. 1
  221. 2
  222. fizz
  223. 4
  224. buzz
  225. ```
  226.  
  227. <details>
  228. <summary>Answer</summary>
  229.  
  230. ```js
  231. function fizzBuzz(n) {
  232.  
  233. for (let i = 1; i <= n; i++) {
  234.  
  235. if (i % 5 === 0 && i % 3 === 0)
  236. console.log("fizzbuzz");
  237. else if (i % 3 === 0)
  238. console.log("fizz");
  239. else if (i % 5 === 0)
  240. console.log("buzz");
  241. else
  242. console.log(i);
  243.  
  244. }
  245. }
  246. ```
  247.  
  248. </details>
  249.  
  250. ----
  251.  
  252. ## Chunk
  253.  
  254. --- Directions
  255.  
  256. Given an array and chunk size, divide the array into many subarrays
  257. where each subarray is of length size
  258.  
  259. --- Examples
  260.  
  261. ```js
  262. chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
  263. chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
  264. chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
  265. chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
  266. chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
  267. ```
  268.  
  269. <details>
  270. <summary>Answer(s)</summary>
  271.  
  272. ```js
  273. function chunk(array, size) {
  274.  
  275. const chunkArr = [];
  276.  
  277. do {
  278. chunkArr.push(array.slice(0, size));
  279. array = array.slice(size, array.length + 1);
  280. }
  281. while (array.length > size);
  282.  
  283. chunkArr.push(array);
  284.  
  285. return chunkArr;
  286.  
  287. }
  288. ```
  289. ----
  290. ```js
  291. function chunk(array, size) {
  292. const chunked = [];
  293.  
  294. for (let element of array) {
  295. const last = chunked[chunked.length - 1];
  296.  
  297. if (!last || last.length == size) {
  298. chunked.push([element]);
  299. } else {
  300. last.push(element);
  301. }
  302. }
  303.  
  304. return chunked;
  305. }
  306. ```
  307. ----
  308. ```js
  309. function chunk(array, size) {
  310. const chunked = [];
  311. let index = 0;
  312.  
  313. while (index < array.length) {
  314. chunked.push(array.slice(index, index + size));
  315. index += size;
  316. }
  317.  
  318. return chunked;
  319. }
  320. ```
  321.  
  322.  
  323. </details>
  324.  
  325. ----
  326.  
  327. ## Greedy Cashier
  328.  
  329. --- Directions
  330.  
  331. Given an amount of change, determine the *minimum* number of coins required to make that change
  332.  
  333. --- Examples
  334.  
  335. ```js
  336. greedy(65) --> 4 `(2 quarters, 1 dime, 1 nickle)`
  337. greedy(5) --> 1 `(1 nickle)`
  338. ```
  339.  
  340. <details>
  341. <summary>Answer(s)</summary>
  342.  
  343. ```js
  344. function greedy(change) {
  345.  
  346. let count = 0
  347. do {
  348. if (change >= 25) {
  349. change = change - 25
  350. count++
  351. } else if (change >= 10) {
  352. change = change - 10
  353. count++
  354. } else if (change >= 5) {
  355. change = change - 5
  356. count++
  357. } else {
  358. change = change - 1
  359. count++
  360. }
  361. } while (change > 0)
  362.  
  363. return count
  364.  
  365. }
  366. ```
  367.  
  368. </details>
  369.  
  370. ----
  371.  
  372.  
  373. ## Mario
  374.  
  375. --- Directions
  376.  
  377. Given a size, create mario-style pyramid of that size using octothorps (#)
  378.  
  379. --- Example
  380.  
  381. ```js
  382. mario(2) -->
  383. # #
  384. ## ##
  385. ```
  386.  
  387. <details>
  388. <summary>Answer(s)</summary>
  389.  
  390. ```js
  391. function mario(height) {
  392.  
  393. let pir = ''
  394. for (let h = 1; h <= height; h++) {
  395. // For each row of height
  396. // Find # of spaces and hashes
  397. let hashes = h;
  398. let spaces = height - h;
  399.  
  400. // First pyramid
  401. // Spaces on the left
  402. for (let space = 0; space < spaces; space++) {
  403. pir += " "
  404. }
  405. // Left half of pyramid
  406. for (let hash = 0; hash < hashes; hash++) {
  407. pir += "#"
  408. }
  409. // Our middle column space
  410. pir += " "
  411. // Right half of pyramid
  412. for (let hash = 0; hash < hashes; hash++) {
  413. pir += "#";
  414. }
  415.  
  416. // New line
  417. pir += "\n"
  418.  
  419. }
  420.  
  421. console.log(pir)
  422.  
  423. }
  424. ```
  425.  
  426. </details>
  427.  
  428. ----
  429.  
  430. ## ReverseInt
  431.  
  432. --- Directions
  433.  
  434. Given an integer (either positive or negative), reverse the number
  435. and keep it's sign.
  436.  
  437. --- Example
  438.  
  439. ```js
  440. reverseInt(-51) --> -15
  441. reverseInt(100) --> 001 --> 1
  442. reverseInt(601) --> 106
  443. ```
  444.  
  445. <details>
  446. <summary>Answer(s)</summary>
  447.  
  448. ```js
  449. function reverseInt(n) {
  450.  
  451. let reversed = '';
  452.  
  453. for (let char of n.toString()) {
  454. if (char != "-")
  455. reversed = char + reversed;
  456. }
  457.  
  458. return parseInt(reversed) * Math.sign(n);
  459. }
  460. ```
  461.  
  462. ----
  463.  
  464. ```js
  465. function reverseInt(n) {
  466. const reversed = n
  467. .toString()
  468. .split('')
  469. .reverse()
  470. .join('');
  471.  
  472. return parseInt(reversed) * Math.sign(n);
  473. }
  474. ```
  475.  
  476. </details>
  477.  
  478. ## Vowels
  479.  
  480. --- Directions
  481.  
  482. Write a function that returns the number of vowels
  483. used in a string. Vowels are the characters 'a', 'e'
  484.  
  485. --- Example
  486.  
  487. ```js
  488. vowels('Hi There!') --> 3
  489. vowels('Why do you ask?') --> 4
  490. vowels('Why?') --> 0
  491. ```
  492.  
  493. <details>
  494. <summary>Answer(s)</summary>
  495.  
  496. ```js
  497. function vowels(str) {
  498. const matches = str.match(/[aeiou]/gi);
  499. return matches ? matches.length : 0;
  500. }
  501. ```
  502.  
  503. ----
  504.  
  505. ```js
  506. function vowels(str) {
  507. let count = 0;
  508. const checker = ['a', 'e', 'i', 'o', 'u'];
  509.  
  510. for (let char of str.toLowerCase()) {
  511. if (checker.includes(char)) {
  512. count++;
  513. }
  514. }
  515.  
  516. return count;
  517. }
  518. ```
  519.  
  520. </details>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement