Advertisement
gnations56

CodeHS AP CSP answers

Mar 6th, 2019
2,689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ====JS Control Stuctures 6.6.4-6.8.4====
  2. ==6.6.4==
  3. function start(){
  4.     var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];
  5.     var line2 = ["Tony", "Lisa", "Laurie", "Karen"];
  6.     if(line.indexOf("Bob") != -1) {println("Bob is in the first line.");}
  7.     else{println("Bob is not in the first line.");}
  8.     if(line2.indexOf("Bob") != -1) {println("Bob is in the second line.");}
  9.     else{println("Bob is not in the second line.");}
  10.     // Your code goes here
  11. }
  12. ==6.7.4==
  13. function start(){
  14.     var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];
  15.     // Your code goes here
  16.     println(line[0] + ", " + line[1] + ", " + line[2] + ", " +line[3] + ", " +line[4]);
  17.     line.remove("Sam");
  18.     line.remove("Lisa");
  19.     println(line[0] + ", " + line[1] + ", " + line[2]);
  20. }
  21. ==6.8.4==
  22. function start() {
  23.     // Create your coin flip simulation here
  24.     var i = 0;
  25.     while(i < 100) {
  26.         if(Randomizer.nextBoolean()) {println("Heads");}
  27.         else {println("Tails");}
  28.         i++;
  29.     }
  30. }
  31. ====Digital Information====
  32. ==7.3.7==
  33. 01001000
  34. 01100101
  35. 01101100
  36. 01101100
  37. 01101111
  38. 00101100
  39. 00100000
  40. 01001011
  41. 01100001
  42. 01110010
  43. 01100101
  44. 01101100
  45. 00100001
  46. ==7.4.5==
  47. 01010101 10101010 01010101 10101010 01010101 10101010 01010101 10101010
  48. ==7.4.6==
  49. 01110
  50. 01110
  51. 00000
  52. 01110
  53. 01110
  54. 00000
  55. 01110
  56. 01110
  57. 00000
  58. 01110
  59. 01110
  60. 00000
  61. 01110
  62. 01110
  63. 00000
  64. 01110
  65. 01110
  66. 00000
  67. 01110
  68. 01110
  69. ==7.4.7==
  70. 234234e2139649283764862497
  71. ==7.6.6==
  72. FF0000
  73. 00FF00
  74. 0000FF
  75. ==7.6.7==
  76. FF0000
  77. 00FF00
  78. 0000FF
  79. FFFF00
  80. ==7.6.8==
  81. FF0000
  82. FF7700
  83. FFFF00
  84. 00FF00
  85. 0000FF
  86. FF00FF
  87. ==7.6.9==
  88. 79427549579475923759749579347593459294583458345843503450450ffdffff008508508f87f89d709f7d9f79427549579475923759749579347593459294583458345843503450450ffdffff008508508f87f89d709f7d9f79427549579475923759749579347593459294583458345843503450450ffdffff008508508f87f89d709f7d9f79427549579475923759749579347593459294583458345843503450450ffdffff008508508f87f89d709f7d9f79427549579475923759749579347593459294583458345843503450450ffdffff008508508f87f89d709f7d9f79427549579475923759749579347593459294583458345843503450450ffdffff008508508f87f89d709f7d9f79427549579475923759749579347593459294583458345843503450450ffdffff008508508f87f89d709f7d9f
  89. ==7.7.9==
  90. // Constants for the image
  91. var IMAGE_URL = "https://codehs.com/static/img/zebra.jpg";
  92. var IMAGE_WIDTH = 350;
  93. var IMAGE_HEIGHT = 250;
  94. var IMAGE_X = getWidth() / 2 - IMAGE_WIDTH / 2;
  95. var IMAGE_Y = getHeight() / 2 - IMAGE_HEIGHT / 2;
  96.  
  97. // Constants for the pixel array
  98. var RED = 0;
  99. var GREEN = 1;
  100. var BLUE = 2;
  101.  
  102. // Constants for the pixel filter
  103. var MAX_COLOR_VALUE = 255;
  104.  
  105. // We need to wait for the image to load before modifying it
  106. var IMAGE_LOAD_WAIT_TIME = 50;
  107.  
  108. /*
  109.  * Given a pixel array with 3 values [R, G, B]
  110.  * Modifies the pixel array such that each value is inverted ie:
  111.  * R = 255 - R
  112.  * G = 255 - G
  113.  * B = 255 - B
  114.  * Returns the modified pixel array
  115.  */
  116. function invertPixel(pixel) {
  117.    
  118.     // WRITE THIS FUNCTION
  119.     var red = pixel[0];
  120.     var green = pixel[1];
  121.     var blue = pixel[2];
  122.     red = 255-red;
  123.     blue = 255-blue;
  124.     green = 255-green;
  125.     var newPixel = [red,green,blue];
  126.     return newPixel;
  127.    
  128. }
  129.  
  130. // Inverts the colors of each pixel in the WebImage image
  131. function invert(image) {
  132.     for(var x = 0; x < image.getWidth(); x++) {
  133.         for (var y = 0; y < image.getHeight(); y++) {
  134.             // Get the current pixel
  135.             var pixel = image.getPixel(x, y);
  136.            
  137.             // Modify the current pixel
  138.             pixel = invertPixel(pixel);
  139.            
  140.             // Update the image with the modified pixel
  141.             image.setRed(x, y, pixel[RED]);
  142.             image.setGreen(x, y, pixel[GREEN]);
  143.             image.setBlue(x, y, pixel[BLUE]);
  144.         }
  145.     }
  146. }
  147.  
  148. function start() {
  149.     // Set up the image
  150.     var image = new WebImage(IMAGE_URL);
  151.     image.setSize(IMAGE_WIDTH, IMAGE_HEIGHT);
  152.     image.setPosition(IMAGE_X, IMAGE_Y);
  153.    
  154.     // Add it to the canvas
  155.     add(image);
  156.    
  157.     // Wait for it to load before applying the filter
  158.     setTimeout(function(){
  159.         invert(image);
  160.     }, IMAGE_LOAD_WAIT_TIME);
  161. }
  162. ==7.7.10==
  163. // Constants for the image
  164. var IMAGE_URL = "https://codehs.com/static/img/zebra.jpg";
  165. var IMAGE_WIDTH = 350;
  166. var IMAGE_HEIGHT = 250;
  167. var IMAGE_X = getWidth() / 2 - IMAGE_WIDTH / 2;
  168. var IMAGE_Y = getHeight() / 2 - IMAGE_HEIGHT / 2;
  169.  
  170. // We need to wait for the image to load before modifying it
  171. var IMAGE_LOAD_WAIT_TIME = 50;
  172.  
  173. // Filter that sets the blue value of every pixel in the image to be 0
  174. function removeBlue(image) {
  175.    
  176.     for (var x = 0; x < image.getWidth();x++) {
  177.         for (var y = 0; y < image.getHeight();y++) {
  178.             var pixel = getPixel(x,y);
  179.             pixel[2] = 0;
  180.             setPixel(x,y,pixel);
  181.         }
  182.     }
  183. }
  184.  
  185. function start() {
  186.     // Set up the image
  187.     var image = new WebImage(IMAGE_URL);
  188.     image.setSize(IMAGE_WIDTH, IMAGE_HEIGHT);
  189.     image.setPosition(IMAGE_X, IMAGE_Y);
  190.    
  191.     // Add it to the canvas
  192.     add(image);
  193.    
  194.     // Wait for it to load before applying the filter
  195.     setTimeout(function(){
  196.         removeBlue(image);
  197.     }, IMAGE_LOAD_WAIT_TIME);
  198. }
  199. ==7.7.11==
  200. // Constants for the image
  201. var IMAGE_URL = "https://codehs.com/static/img/about/goldengate.jpg";
  202. var IMAGE_WIDTH = 350;
  203. var IMAGE_HEIGHT = 250;
  204. var IMAGE_X = getWidth() / 2 - IMAGE_WIDTH / 2;
  205. var IMAGE_Y = getHeight() / 2 - IMAGE_HEIGHT / 2;
  206.  
  207. // Constants for the filter
  208. var DARKENING_FACTOR = 25;
  209. var MIN_PIXEL_VALUE = 0;
  210.  
  211. // Constants for pixel indices
  212. var RED = 0;
  213. var GREEN = 1;
  214. var BLUE = 2;
  215.  
  216. // We need to wait for the image to load before modifying it
  217. var IMAGE_LOAD_WAIT_TIME = 50;
  218.  
  219. // Filter that takes an image as a parameter
  220. // and darkens the pixels in the left half of the image
  221. function darkenFilter(image) {
  222.    
  223.     for (var x = 0; x < image.getWidth();x++) {
  224.         for (var y = 0; y < image.getHeight();y++) {
  225.             var pixel = image.getPixel(x,y);
  226.             pixel[0] = pixel[0] - DARKENING_FACTOR;
  227.             pixel[1] = pixel[1] - DARKENING_FACTOR;
  228.             pixel[2] = pixel[2] - DARKENING_FACTOR;
  229.             image.setPixel(x,y,pixel);
  230.         }    
  231.     }
  232.    
  233. }
  234.  
  235. function start() {
  236.     // Set up the image
  237.     var image = new WebImage(IMAGE_URL);
  238.     image.setSize(IMAGE_WIDTH, IMAGE_HEIGHT);
  239.     image.setPosition(IMAGE_X, IMAGE_Y);
  240.    
  241.     // Add it to the canvas
  242.     add(image);
  243.    
  244.     // Wait for it to load before applying the filter
  245.     setTimeout(function(){
  246.         darkenFilter(image);
  247.     }, IMAGE_LOAD_WAIT_TIME);
  248. }
  249. ==7.9.4==
  250. 0000000000001111110001011110100110110110011100111001110011100110110110010111101000111111000000000000
  251. ==7.10.8==
  252. /*
  253.  * Write a program that guesses every possible 4 digit passcode
  254.  * combinations until the correct passcode is guessed.
  255.  *
  256.  * The passcode is randomly generated and stored in the variable
  257.  * secretPasscode.
  258.  *
  259.  * Print out how many guesses it took to guess the correct passcode.
  260.  */
  261.  
  262.     function start() {
  263.     var secretPasscode = generateRandomPasscode();
  264.     for (var guessInt = 0; guessInt < 1000; guessInt++) {
  265.         let guess = pad(`${guessInt}`,4);
  266.         if(guess === secretPasscode){
  267.             println(`we just started counting, and found the passcode at ${guessInt} `);
  268.             break;
  269.         }
  270.     }
  271. }
  272.  
  273. function pad(str, length) {
  274.     while (str.length < length) str = '0' + str;
  275.     return str;
  276. }
  277.  
  278.  
  279. // Checks whether the given guess passcode is the correct passcode
  280. function isCorrect(guessCode, correctCode) {
  281.     return guessCode == correctCode;
  282. }
  283.  
  284. // Generates a random 4 digit passcode and returns it as a String
  285. function generateRandomPasscode() {
  286.     var randomPasscode = "";
  287.    
  288.     for(var i = 0; i < 4; i++) {
  289.         var randomDigit = Randomizer.nextInt(0, 9);
  290.         randomPasscode += randomDigit;
  291.     }
  292.    
  293.     return randomPasscode;
  294. }
  295. ==7.13.2==
  296. // DESCRIBE YOUR FILTER HERE IN THIS COMMENT!
  297. function customFilter(image) {
  298.    
  299.     for (var x = 0; x < image.getWidth();x++) {
  300.         for (var y = 0; y < image.getHeight();y++) {
  301.             var pixel = getPixel(x,y);
  302.             pixel[0] = 0;
  303.             pixel[1] = Math.random();
  304.             pixel[2] = 128;
  305.             setPixel(x,y,pixel);
  306.         }  
  307.     }
  308. }
  309.  
  310.  
  311. // WRITE ANY HELPER FUNCTIONS HERE
  312.  
  313. /*********************************************
  314.  * You do not need to write any code below this line.
  315.  * This is starter code that sets up the image on the screen
  316.  * and calls your customFilter function.
  317.  * Feel free to read this code and learn how it works!
  318.  * Be careful though, if you modify this code the program may not
  319.  * work correctly.
  320.  *********************************************/
  321.  
  322. // Constants for the image
  323. var IMAGE_URL = "https://codehs.com/static/img/zebra.jpg";
  324. var IMAGE_WIDTH = 350;
  325. var IMAGE_HEIGHT = 250;
  326. var IMAGE_X = getWidth() / 2 - IMAGE_WIDTH / 2;
  327. var IMAGE_Y = getHeight() / 2 - IMAGE_HEIGHT / 2;
  328.  
  329. // We need to wait for the image to load before modifying it
  330. var IMAGE_LOAD_WAIT_TIME = 50;
  331.  
  332. function start() {
  333.     // Set up the image
  334.     var image = new WebImage(IMAGE_URL);
  335.     image.setSize(IMAGE_WIDTH, IMAGE_HEIGHT);
  336.     image.setPosition(IMAGE_X, IMAGE_Y);
  337.    
  338.     // Add it to the canvas
  339.     add(image);
  340.    
  341.     // Wait for it to load before applying the filter
  342.     setTimeout(function(){
  343.         customFilter(image);
  344.     }, IMAGE_LOAD_WAIT_TIME);
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement