Advertisement
Crevice

Advanced Loops Practice

Nov 11th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <script>
  5.  
  6. //create the function that console logs every number from the firstNum to the lastNum
  7. function simpleLoop( firstNum, lastNum ){
  8. }
  9. simpleLoop(0, 8);//0, 1, 2, 3, 4, 5, 6, 7, 8
  10. //create the function that takes in 1 number, and console logs out every number from 0 down to this number. the number will always be less than 0
  11. function backwardsLoop( ){
  12.  
  13. }
  14. backwardsLoop( -5 ); //0, -1, -2, -3, -4, -5
  15. backwardsLoop( -2 ); //0, -1, -2
  16. //create the function that takes in 1 number. it console logs all the numbers from 0 to this number. It may be postive or negative!
  17. function biDirectionalLoop(){
  18. }
  19. biDirectionalLoop(-3); //0, -1, -2, -3
  20. biDirectionalLoop(4); //0, 1, 2, 3, 4
  21. //create the function that takes in 2 numbers.
  22. //logs out every number from 0 to the first num
  23. //increments by the second number
  24. //will always be postive
  25. function jumpLoop(){
  26. }
  27. jumpLoop(10,2); //0,2,4,6,8,10
  28. jumpLoop(1000, 100)// 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000
  29. //create the function that takes in 4 positive numbers
  30. //console logs out 2 numbers
  31. //the first is where to end the first number, second is how far to increment
  32. //3rd is where to end second number, the other is how far to increment
  33. //stops the loop when either number reaches the end
  34. //both numbers start from 0
  35. function dualDirection(firstEnd, firstDistance, secondEnd, secondDistance){
  36. }
  37. dualDirection(3, 1, 10, 2); // 0, 0, 1, 2, 2, 4, 3, 6
  38. dualDirection(1000, 250, 10, 3);//0,0,250,3,500,6,750,9 //would end because 12>10
  39. //don't modify this function, it will be used next
  40. function getRandomNumber( min, max ){
  41. var randomPercent = Math.random();
  42. var scaledNumber = randomPercent * (max-min+1) + min;
  43. var wholeNumber = Math.floor( scaledNumber );
  44. return wholeNumber;
  45. }
  46. //make a function that keeps looping until the random number it gets is greater than the passed in number
  47. //call the getRandomNumber function with arguments 1,10
  48. function stopWhenGreater( targetNum ){
  49. console.log('starting');//do this, so we know it started to run
  50. //example of call: var num = getRandomNumber(1,10)
  51. }
  52. //these numbers are examples. they will be random and therefore could be anything
  53. stopWhenGreater(5);// 'starting', 3, 1
  54. stopWhenGreater(9);// 'starting', 8, 4, 5, 2, 1, 9
  55. //fibonacci loop
  56. //start with 2 numbers, 0 and 1.
  57. //keep looping them while the combined result is greater than the target
  58. //hint: you should not need to make more variables. move one number
  59. //into the other number and then result into the first num
  60. function fibonacciLoop( targetNumber ){
  61. var result = null;
  62. var num1 = 0, num2 = 1;
  63. while(result < targetNum ){
  64. //?
  65. console.log(result);
  66. }
  67. }
  68. </script>
  69. </head>
  70. <body>
  71.  
  72. </body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement