Guest User

CodeHS JavaControlStructure Answers

a guest
Mar 17th, 2017
7,127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function start(){
  2.     var doggo = false;
  3.     println ("Do I have a dog? " + doggo);
  4.    
  5. }
  6. -------------------------------------------------------------------
  7. function start(){
  8.     var credits = readBoolean ("Do you have enough credits?");
  9.     var requirementsMet = readBoolean ("Do you meet all the the requirement?");
  10.     var canGraduate = credits && requirementsMet;
  11.     println("Can Graduate = " + canGraduate);
  12.    
  13.    
  14. }
  15. --------------------------------------------------------------------
  16. function start(){
  17.     var weekday = readBoolean("is it a weekday? ");
  18.     var holiday = readBoolean("is it a holiday? ");
  19.     var noSchoolToday = holiday || !weekday;
  20.     println (" their is no school today?" + noSchoolToday);
  21.    
  22.  
  23.    
  24. }
  25. ----------------------------------------------------------------------
  26. function start(){
  27.     var firstDice = readInt("What did you get on the first dice roll");
  28.     var secondDice = readInt("What did you get on the second dice roll");
  29.     var dubs = secondDice == firstDice;
  30.     println("Your two dice are equal " + dubs);
  31. }
  32. ----------------------------------------------------------------------
  33. function start(){
  34.     var points = readInt ("how many points did you score? ");
  35.     var rebounds = readInt ("how many rebounds did you score? ");
  36.     var assists = readInt ("how many assists did you score? ");
  37.     var allstar = points >= 25 || rebounds >=10 && points >=10 && assists >=10;
  38.     println("You are an allstar = " + allstar);
  39.    
  40.    
  41. }
  42. ----------------------------------------------------------------------
  43. function start(){
  44.     var age = readInt ("How old are you ");
  45.     if (age >= 13 && age <= 19){
  46.     println("Yes, you are a teenager");
  47.     }else{
  48.         println("No, you are not a teenager.");
  49.     }
  50. }
  51. --------------------------------------------------------------------
  52. function start(){
  53.     var color = readLine ("What color is it? (all lowercase)");
  54.     if (color == 'red'){
  55.         println ("If its red you should stop");
  56.     }else{
  57.         if (color == 'yellow'){
  58.             println ("If its yellow proceed with caution");
  59.         }else{
  60.             if (color == 'green'){
  61.                 println ("Green Light: Go!");
  62.             }
  63.         }
  64.     }
  65.    
  66.    
  67. }
  68. ----------------------------------------------------------------------
  69. function start(){
  70.     for(var i = 0; i < 100; i++){
  71.         println("I will not come to school later");
  72.     }
  73.    
  74. }
  75. ---------------------------------------------------------------------
  76. function start(){
  77.     var radius = getWidth()/NUM_CIRCLES/2;
  78.     for(var i = 0; i < NUM_CIRCLES; ++i){
  79.         var circle = new Circle(radius);
  80.         circle.setColor(Color.black);
  81.         circle.setPosition(radius + radius * i * 2, getHeight()/2);
  82.         add(circle);
  83.     }
  84.    
  85. }
  86. -----------------------------------------------------------------------
  87. function start(){
  88.     var radius = getWidth()/NUM_CIRCLES/2;
  89.     for(var i = 0; i < NUM_CIRCLES; ++i){
  90.         var circle = new Circle(radius);
  91.         if((i + 1) % 2 == 0){
  92.             circle.setColor(Color.green);
  93.         }else{
  94.             circle.setColor(Color.red);
  95.         }
  96.         circle.setPosition(radius + radius * i * 2, getHeight()/2);
  97.         add(circle);
  98.     }
  99.    
  100. }
  101. ----------------------------------------------------------------------
  102. function start(){
  103.     for(var i = 0; i < 500; i += 7){
  104.     println(i)
  105.    
  106.     }
  107. }
  108. ----------------------------------------------------------------------
  109. function start(){
  110.     for(var i = 1; i < 1000000; i *= 2){
  111.         println(i);
  112.        
  113.     }  
  114. }
  115. ----------------------------------------------------------------------
  116. function start(){
  117.     var firstNum = readInt ("Enter an integer");
  118.     var secondNum = readInt ("Enter a second number");
  119.     var sum = 0;
  120.    
  121.     for(var i = firstNum; i <= secondNum; i++){
  122.         sum += i;
  123.     }
  124.     println (sum);
  125.    
  126.    
  127. }
  128. -----------------------------------------------------------------------
  129. var N = 5;
  130.  
  131. function start(){
  132.     var factorial = 1;
  133.     for(var i = N; i >= 1; i--){
  134.         if(i == N){
  135.             print(i);
  136.         }else{
  137.             print(" * " + i);
  138.         }
  139.         factorial *= i;
  140.         }
  141.         print(" = " + factorial);
  142.     }
  143. ------------------------------------------------------------------------
  144. var SIDES_ON_DICE = 6;
  145.  
  146. function start() {
  147.     for(var i = 1; i <= SIDES_ON_DICE; i++){
  148.         for(var j = 1; j <= SIDES_ON_DICE; j++){
  149.             println(i + "," + j);
  150.         }
  151.     }
  152.    
  153. }
  154. ------------------------------------------------------------------------
  155. function start(){
  156.     var numRolled = 0;
  157.     for(var i = 0; i < 100; i++){
  158.         numRolled = Randomizer.nextInt(1, 6);
  159.         println (numRolled)
  160.     }
  161. }
  162. ------------------------------------------------------------------------
  163. var SIDE_LENGTH = 100;
  164.  
  165. function start(){
  166.     var rect = new Rectangle(SIDE_LENGTH, SIDE_LENGTH);
  167.     add(rect);
  168.     rect.setPosition(getWidth() / 2 - SIDE_LENGTH / 2, getHeight() / 2  - SIDE_LENGTH / 2);
  169.     var color = Randomizer.nextColor();
  170.     rect.setColor(color);
  171.    
  172. }
  173. -----------------------------------------------------------------------
  174. var STARTING_ITEMS_IN_INVENTORY = 20;
  175.  
  176. function start(){
  177.     var numItems = STARTING_ITEMS_IN_INVENTORY;
  178.     var bought = 0;
  179.    
  180.     while(numItems > 0){
  181.         println("We have " + numItems + " items in inventory");
  182.         bought = readInt("How many items are you buying? ");
  183.         println("");
  184.         if(bought > numItems){
  185.             println("All out!");
  186.         }else{
  187.             numItems -= bought;
  188.         }
  189.     }
  190.     println("All out!");
  191.    
  192. }
  193. -----------------------------------------------------------------------
  194. function start(){
  195.     var firstNum = 1;
  196.     println(firstNum);
  197.     var secondNum = 1;
  198.     println(secondNum);
  199.     var answer = firstNum + secondNum;
  200.     println(answer);
  201.     while (answer < 987) {
  202.         firstNum = secondNum;
  203.         secondNum = answer;
  204.         answer = firstNum + secondNum;
  205.         println(answer);
  206.    
  207.     }
  208. }    
  209. -----------------------------------------------------------------------
  210. function start(){
  211. var numRolls = 0;
  212. while (true) {
  213. numRolls++;
  214. var rollOne = Randomizer.nextInt (1,6);
  215. var rollTwo = Randomizer.nextInt (1,6);
  216.     println ("Rolled: " + rollOne + "," + rollTwo);
  217.  
  218. if (rollOne == 1 && rollTwo == 1) {
  219.  
  220. break;
  221.  
  222.         }
  223.  
  224.     }
  225.  
  226. println ("It took you " + numRolls + " rolls to get snake eyes. ");
  227.  
  228. }
  229. -----------------------------------------------------------------------
  230. var SECRET = "abc123";
  231.  
  232. function start(){
  233.     var password = SECRET;
  234.     while (true) {
  235.         var potential = readLine ("What is le password ");
  236.         if(potential == password) {
  237.             println("You got it!");
  238.             break;
  239.        
  240.         }else{
  241.             println("your answer is incorrect ");
  242.         }
  243.     }
  244. }
RAW Paste Data