Guest User

Untitled

a guest
Aug 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. Question 1:
  2.  
  3. var each = function(collection, callback) {
  4. if (Array.isArray(collection)){
  5. for (var i = 0; i < collection.length; i++) {
  6. callback(collection[i], i, collection);
  7. }
  8. } else if (typeof collection === 'object'){
  9. for (var key in collection) {
  10. callback(collection[key], key, collection);
  11. }
  12. }else{
  13. return 'not an array or object';
  14. }
  15. };
  16.  
  17. Type of is incorrect should be array.isarray.
  18. Problems with iteartor function does not pass
  19. Needs to have a call back function
  20.  
  21. Question 2
  22.  
  23. var addRandomAgeInclusive = function(string, a, b) {
  24. if(a > b) {
  25. return string;
  26. }
  27. else if (typeof string === "string") {
  28. var name_object = {};
  29. name_object['name'] = string;
  30. name_object['age'] = Math.round(Math.random()*(b-a)+a);
  31. return name_object
  32. }
  33. else if (typeof string === "object") {
  34. for (var key in string) {
  35. if (key === "name") {
  36. string['age'] = Math.round(Math.random()*(b-a)+a);
  37. }
  38. else {
  39. return string;
  40. }
  41. }
  42. }
  43. return string;
  44. }
  45.  
  46. addRandomAgeInclusive({ name: 'Sean', id: 10330293 }, 30, 40)
Add Comment
Please, Sign In to add comment