CodeHS JavaScript And Graphics Answers

We thoroughly check each answer to a question to provide you with the most correct answers. Found a mistake? Tell us about it through the REPORT button at the bottom of the page. Ctrl+F (Cmd+F) will help you a lot when searching through such a large set of questions.

These are the answers for Unit 2: Video Game Design from CodeHS.

Read Also: CodeHS Karel answers

ExerciseSolution
All Dice Valuesvar SIDES_ON_DICE = 6;
function start() {
for(var i = 1; i < 7; i++){
println(“1,”+i)
}
for(var i = 1; i < 7; i++){
println(“2,”+i)
}
for(var i = 1; i < 7; i++){
println(“3,”+i)
}
for(var i = 1; i < 7; i++){
println(“4,”+i)
}
for(var i = 1; i < 7; i++){
println(“5,”+i)
}
for(var i = 1; i < 7; i++){
println(“6,”+i)
}
}
All Starfunction start(){
var pointsPerGame = readInt(“Points per game: “);
var reboundsPerGame = readInt(“Rebounds per game: “);
var assistsPerGame = readInt(“Assists per game: “);
var star = pointsPerGame + reboundsPerGame + assistsPerGame;
///
if(pointsPerGame >= 10 && reboundsPerGame >= 10 && assistsPerGame >=10){ println("true"); } ///If 10-10-10 if(pointsPerGame >= 25){ println("true"); } ///If ONLY 25 points else{ println("false"); } ///Invalid.
}
Apples and Orangesfunction start()
{
var numApples = 20;
var numOranges = 15;
println(numApples);
println(numOranges);
numApples = 0;
numApples = 0;
println(numApples);
println(numOranges);
}
Area of Trianglefunction start(){
triangleArea(3, 5);
triangleArea(15, 10);
}
function triangleArea(base, height){
var area = 1/2 * base * height;
println(area);
}
Better Password Promptvar SECRET = “abc123”;
function start()
{
var guess;
while(guess != SECRET){
var guess = readLine(“What is the password? “);
if(guess == SECRET){ println("Password correct"); break; } else{ println("Sorry, that password does not match."); } }
}
Better Sumfunction start(){
var numOne = readInt(“Enter a number: “);
var numTwo = readInt(“Enter another number: “);
var sum = 0; for(var i = numOne; i <= numTwo; i++){ sum += i; } println("The sum was " + sum);
}
CanYou Graduatefunction start()
{
var credits = readInt(“How many credits do you have: “);
var metRequirements = readBoolean(“Have you met requirements: “);
var canGraduate = credits && metRequirements;
if(canGraduate){ println("true"); } if(!canGraduate){ println("false"); }
}
Caterpillarvar NUM_CIRCLES = 15;
// This graphics program should draw a worm.
// A worm is made up of NUM_CIRCLES circles.
// Use a for loop to draw the worm,
// centered vertically in the screen.
// Also, be sure that the worm is still drawn across
// the whole canvas, even if the value of NUM_CIRCLES is changed.
function start(){
var diameter = getWidth()/NUM_CIRCLES;
var radius = getWidth()/NUM_CIRCLES/2;
var xpos = radius;
for(var i = 0; i < NUM_CIRCLES; i++){ var circle = new Circle(radius); circle.setPosition(xpos, getHeight()/2); if(i % 2 == 0){ circle.setColor(Color.red); } else{ circle.setColor(Color.green); } add(circle); xpos = xpos + (2*radius); }
}
Chalkboardfunction start(){
for(var i = 0; i < 100; i++){
println(“I will not come late to school”);
}
}
Count By Sevensfunction start(){
for(var i = 0; i < 500; i += 7){
println(i);
}
}
Do You Have a Dog/* This program should declare a boolean that describes whether or
not you have a dog. Then you should print out an informative
message to the user. */
function start()
{
var dog = true;
println(“Do you have a dog? ” + dog);
}
Factorialvar N = 5;
function start(){
for(var i = 4; i >= 1; i–){
N *= i;
}
println(“The sum was ” + N);
}
Fibonaccivar MAX = 1000;
function start(){
var x = 0;
var y = 1;
while (x + y < MAX){ println(y); x += y; println(x); y += x; }
}
French Flagfunction start()
{
var newRect = new Rectangle(getWidth() / 3, getHeight());
newRect.setPosition(0, 0); //Setting the rectangle’s position
newRect.setColor(Color.blue); //Setting the colour to blue
add(newRect); //Placing the rectangle
var newRect = new Rectangle(getWidth() / 3, getHeight()); newRect.setPosition(267, 0); //Setting the rectangle's position newRect.setColor(Color.red); //Setting the rectangle's colour to red add(newRect); //Placing the rectangle
}
Graphics Stop Lightvar LIGHT_RADIUS = 35;
var STOPLIGHT_WIDTH = 120;
var STOPLIGHT_HEIGHT = 350;
var BUFFER = 100;
var GRAY_COLOR = “#737071”;
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
var DIST_BETWEEN_LIGHTS = 100;
function start(){
var rect = new Rectangle(STOPLIGHT_WIDTH, STOPLIGHT_HEIGHT);
rect.setPosition(centerX – STOPLIGHT_WIDTH / 2, centerY – STOPLIGHT_HEIGHT / 2);
rect.setColor(GRAY_COLOR);
add(rect);
drawCircle(Color.red, centerY – DIST_BETWEEN_LIGHTS);
drawCircle(Color.yellow, centerY);
drawCircle(Color.green, centerY + DIST_BETWEEN_LIGHTS);
}
function drawCircle(color, y){
var circle = new Circle(LIGHT_RADIUS);
circle.setPosition(centerX, y);
circle.setColor(color);
add(circle);
}
Grocery Storefunction start()
{
var name = readLine(“Enter name: “);
var apples = readInt(“How many apples? “);
var oranges = readInt(“How many oranges? “);
println(“Hello ” + name + “, you want ” + apples + ” apples” + ” and ” + oranges + ” oranges”);
}
Height in Metersvar INCHES_TO_CM = 2.54;
var CM_TO_METERS = 100;
var FEET_TO_INCHES = 12;
function start(){
convertHeightToMeters(15, 10);
}
function convertHeightToMeters(x, y){
var inches = ((x * FEET_TO_INCHES) + y);
var cm = (inches * INCHES_TO_CM);
var meters = (cm / CM_TO_METERS);
println(meters);
}
Horizontal Linesfunction start(){
horizontalLine(100, 200);
horizontalLine(200, 100);
horizontalLine(300, 20);
}
function horizontalLine(y, length){
var line = new Line(0, y, length, y);
line.setLineWidth(1);
add(line);
}
Inventoryvar STARTING_ITEMS_IN_INVENTORY = 20;
function start(){
while(STARTING_ITEMS_IN_INVENTORY >= 1){ println("We have " + STARTING_ITEMS_IN_INVENTORY + " items in inventory."); var buy = readInt("How many would you like to buy? "); if(buy <= STARTING_ITEMS_IN_INVENTORY) { STARTING_ITEMS_IN_INVENTORY = STARTING_ITEMS_IN_INVENTORY - buy; println("Now we have " + STARTING_ITEMS_IN_INVENTORY + " left."); } if(buy > 20) { println("There is not enough in inventory for that purchase."); } if(STARTING_ITEMS_IN_INVENTORY == 0) { println("All out!"); } }
}
Is It Evenvar SENTINEL = 0;
// Prints whether the entered number is even or odd
function start(){
while(true){
var input = readInt(“Enter a number: “);
if(input == SENTINEL){
println(“Done!”);
break;
}
if(isEven(input)){ println("Even"); } else{ println("Odd"); } }
}
function isEven(x){
return(x % 2 == 0);
}
Local Variables/* All you have to do in this exercise is write a good comment explaining the local variables. You should also give an example of a function
and what the local variables are, and what the scope is of the variable.
*/
/* A local variable is literally just explaining how you cannot use a
variable from a certain function outside of it 😛
*/
Lots of Dicefunction start(){
for(var i = 0; i < 100; i++){
var roll = Randomizer.nextInt(1, 6);
println(“You rolled ” + roll);
}
}
Maxfunction start(){
max(10, 14);
max(7, 3);
}
function max(x, y){
if(x > y){
return x;
}
if(x < y){
return y;
}
if (x == y){
return x;
}
}
Pool Tablevar POOL_BALL_RADIUS = 40;
var POOL_BALL_FONT = “30pt Arial”;
function start(){
drawPoolBall(Color.orange, 5, 100, 100);
drawPoolBall(Color.green, 6, 50, 200);
drawPoolBall(Color.red, 3, 150, 350);
drawPoolBall(Color.blue, 2, 250, 140);
drawPoolBall(Color.black, 1, 500, 330);
// Add some more pool balls!
}
function drawPoolBall(color, num, x, y){
var ball = new Circle(POOL_BALL_RADIUS);
ball.setColor(color);
ball.setPosition(x, y);
add(ball);
var number = new Text(num);
number.setFont(“POOL_BALL_FONT”);
number.setColor(Color.white);
number.setPosition(x – (ball.getWidth() / 8), y + (ball.getHeight() / 8));
add(number);
}
Powers of Two.jsfunction start(){
for(var i = 1; i < 1000000; i *= 2){
println(i);
}
}
Random Color Squarevar SIDE_LENGTH = 100;
function start()
{
var rect = new Rectangle(100, 100); var x = getWidth() / 2; var y = getHeight() / 2; rect.setPosition(x, y); var color = Randomizer.nextColor(); rect.setColor(color); add(rect);
}
Rolling Dicefunction start()
{
var diceOne = readInt(“Dice 1: “);
var diceTwo = readInt(“Dice 2: “);
var rolledDoubles = diceOne == diceTwo;
/// if(rolledDoubles){ println("true"); } /// if(!rolledDoubles){ println("false"); }
}
Running Speed/* Write a program that asks the user how far they ran (in miles)
and then how long it took them (in minutes), and print out
their speed in miles per hour. */
function start()
{
var miles = readInt(“How many miles did you run? “);
var minutes = readInt(“How many minutes did it take you? “);
var MINUTES_TO_HOURS= (minutes/60);
println(miles/MINUTES_TO_HOURS);
}
School’s Outfunction start(){
var weekday = readBoolean(“Is it a week day? “);
var holiday = readBoolean(“Is it a holiday? “);
var noSchoolToday = !weekday || holiday;
if(noSchoolToday){ println("There is no school today: " + noSchoolToday); } if(!noSchoolToday){ println("There is school today: " + noSchoolToday); }
}
Snake Eyesfunction start(){
while(true){
var roll1 = Randomizer.nextInt(1, 6);
var roll2 = Randomizer.nextInt(1, 6);
println(roll1 + “, ” + roll2);
if(roll1 == 1 && roll2 == 1){
break;
}
}
}
Snowman/* Constants representing the radius of the top, middle,
and bottom snowball. */
var BOTTOM_RADIUS = 100;
var MID_RADIUS = 60;
var TOP_RADIUS = 30;
function start(){
var circle = new Circle(BOTTOM_RADIUS);
circle.setPosition(200, 300);
circle.setColor(Color.gray);
add(circle);
var circle = new Circle(MID_RADIUS); circle.setPosition(200, 140); circle.setColor(Color.gray); add(circle); var circle = new Circle(TOP_RADIUS); circle.setPosition(200, 50); circle.setColor(Color.gray); add(circle);
}
Squarefunction start(){
square(5);
square(10);
}
function square(x){
var squareX = x * x;
println(squareX);
}
Square with Return Valuesfunction start(){
square(5);
}
function square(x){
return x * x;
}
Stop Lightfunction start(){
var str = readLine(“Color? “);
if(str == “red”){
println(“Red light: you shoud stop.”);
}
else if(str == “green”){
println(“Green light: Go!”);
}
else if(str == “yellow”){
println(“Slow down!”);
}
}
T-Shirt Shopfunction start()
{
var COST_OF_SHIRT = 15; //Constant
println(“How many t-shirts would you like”);
var tshirts = readInt(“Num T-shirts: “); //Ask
println(“Your total is: ” + COST_OF_SHIRT * tshirts + ” dollars.”); //Total
}
Teenagersfunction start(){
var age = readInt(“How old are you: “);
///
if(age >= 13 && age <= 19){
println(“Yes, you are a teenager.”);
}
else{
println(“No, you are not a teenager.”);
}
}
The Wormvar NUM_CIRCLES = 15;
// This graphics program should draw a worm.
// A worm is made up of NUM_CIRCLES circles.
// Use a for loop to draw the worm,
// centered vertically in the screen.
// Also, be sure that the worm is still drawn across
// the whole canvas, even if the value of NUM_CIRCLES is changed.
function start(){
var diameter = getWidth()/NUM_CIRCLES;
var radius = getWidth()/NUM_CIRCLES;
var xpos = radius;
for(var i = 0; i < NUM_CIRCLES; i++){ var circle = new Circle(radius); circle.setPosition(xpos, getHeight()/2); add(circle); xpos = xpos + (2*radius); }
}
Triplefunction start(){
triple(5);
triple(7);
}
function triple(x){
var tripleX = x * 3;
println(tripleX);
}
Triple with Return Valuesfunction start(){
triple(5);
}
function triple(x){
return x * 3;
}
Your Name And Hobby/* This program should print out your
name, and a hobby you have */
function start(){
println(“My name is Anonymous Porg”);
println(“I like to ree”);
}
// Sample output:
//
// My name is Jeremy
// I like to juggle
//

Was this helpful?

quizzma
Quizzma Team
+ posts

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.




Leave a Comment

Your email address will not be published. Required fields are marked *