NEW
Font size
WorksheetsJavaScript Basics Quiz
Total questions: 69
Worksheet time: 35mins
What keyword is used to create a variable in JavaScript?
define
let
var
make
Which code correctly assigns the value 5 to the variable score?
score = let 5;
var score = 5;
set score = 5;
score <- 5;
Which block allows a user to type input in a Design tab element?
label
dropdown
text input
button
What is the correct syntax to declare a variable in JavaScript?
create variable x = 5;
int x = 5;
var x = 5;
x := 5;
Which keyword is used to store a number or string in JavaScript?
define
function
var
input
What is the function of setText(id, text)?
Stores text in a variable
Gets text from the screen
Sets text of a UI element
Deletes a text input
Which event type is used to detect a button click in onEvent()?
hover
input
submit
click
What is the purpose of getText("inputId")?
Sets a label's text
Retrieves user input from a text field
Hides a button
Resets the app
What does the onEvent function do?
Sets text in an element
Handles user interaction with a UI element
Defines a variable
Changes a variable's value
In the line
setText("messageLabel", "Hello!");
what does "messageLabel" refer to?
The ID of a screen
A string variable
The text to display
The ID of a UI element
What will happen after this code is run?
var name = getText("nameInput");
setText("greetingLabel", "Hello " + name);
It changes the screen
It adds numbers
It creates a popup
It displays a greeting using the user's input
Why is setProperty("image1", "image", "cat.png"); used?
To make a new image
To change the image displayed in an element
To store a variable
To rename an image
What does getNumber("inputBox"); do?
Creates a number
Stores a string
Retrieves a numeric value from the UI
Creates an alert
What happens when a variable is declared inside a function?
It becomes global
It is stored permanently
It can only be used within that function
It can be used in any file
Which code reads input from a text field and displays it?
inputText("myInput", "displayLabel");
getProperty("myInput");
var name = getText("myInput"); setText("displayLabel", name);
setText("displayLabel", getProperty("myInput"));
What is the result of this code?
var age = getNumber("ageInput");
setText("outputLabel", age + 5);
Adds 5 to the age and displays it
Displays "age + 5" as text
Throws an error
Converts age to text
To change the background color of a screen named "mainScreen", use:
setText("mainScreen", "blue");
setProperty("mainScreen", "background-color", "blue");
setScreen("blue");
getProperty("mainScreen", "color");
How would you hide an element using code.org JavaScript?
hide("elementId");
setProperty("elementId", "visible", false);
setProperty("elementId", "hidden", true);
setProperty("elementId", "hidden", "true");
Given a variable score = 10, what does score = score + 5; do?
Sets score to 5
Adds 5 to score
Resets score to 0
Multiplies score by 5
What will getText("answerInput") = "yes" evaluate to if the user types "NO"?
true
false
undefined
null
What is the error in this code?
onEvent("submitBtn", "click", function() {
var age = getNumber("ageInput");
setText("output", age + " years old");
});
getNumber is used wrong
Text is being added to a number
There is no error
setText cannot display variables
Why does this code not update the label correctly?
var userName;
onEvent("button1", "click", function() {
getText("input1");
setText("output", userName);
});
userName is never set
button1 doesn't exist
getText is used incorrectly
Output is not connected
What will be displayed?
var a = 2;
var b = 3;
setText("output", a + b + " is the total");
5
"2 + 3 is the total"
"5 is the total"
"23 is the total"
What will happen if you call setText("label1", greeting); before greeting is declared?
Nothing
Runtime error
greeting will be undefined
label1 disappears
Identify the error:
var name = getText("nameInput");
onEvent("greetBtn", "click", function() {
setText("greetingLabel", "Hi " + name);
});
Variable name used too early
onEvent should be first
getText should be inside the onEvent
greetingLabel is undefined
Why might setText("scoreLabel", total); not display anything?
total is undefined
Label is not visible
setText cannot take numbers
Incorrect label ID
Which is a better way to combine a number and string?
setText("label", 10 + " points");
setText("label", "points" + 10);
Both work
Neither works
Select the best debugging strategy for the following:
onEvent("btn", "click", function() {
var x = getText("box");
setText("output", x + 5);
});
Check if "box" has a number
Remove var
Use setProperty instead
Change box to getNumber
Why is this incorrect?
setText("label", getText("input"));
var input = "name";
getText has no value
variable input is misused
Variable declared too late
setText cannot use variables
Which of these code blocks is easiest to maintain?
Using hardcoded values
Using variables and clear IDs
Using global variables only
Using same name for all elements
What makes a program easier to debug?
No comments
Using many global variables
Using clear variable names
Keeping all code in one block
Which is the correct syntax for an onEvent click function?
onClick("button", function() {});
onEvent("button", "click", function() {});
event("button", "click", function);
on("click", "button", function() {});
What is the purpose of getText("inputBox")?
Set text
Retrieve user input
Make a new box
Add numbers
Which line sets the background color of a button to red?
setText("btn", "red");
setProperty("btn", "background-color", "red");
getProperty("btn", "color", "red");
setStyle("btn", "red");
Which event does NOT work with onEvent?
click
change
drag
random
Which function displays the message "Try again"?
getText("Try again");
showText("Try again");
setText("messageLabel", "Try again");
print("Try again");
Choose the correct way to update the text of a label to "Done"
setText("label1", Done);
setText(label1, "Done");
setText("label1", "Done");
label1.setText("Done");
What does setProperty("box", "text-color", "blue"); do?
Changes the color of text inside a box
Changes border color
Changes screen color
Changes font
What happens if you use setText("label1", getNumber("box1")); and box1 is empty?
Shows 0
Shows undefined
Shows empty label
Causes error
How would you connect an event to a button that shows a hidden image?
A. onEvent("btn", "click", function() { show("img1"); });
B. onClick("btn", function() { show("img1"); });
C. btn.onclick = function() { show("img1"); };
D. img1.show();
Which combination of functions retrieves input and updates text?
getText() and setText()
setText() and showText()
setProperty() and alert()
inputText() and outputText()
What does the following code do?
setText("titleLabel", "Welcome!");
Changes the label's color
Changes the label's text
Hides the label
Changes the screen
Which code will change the text color of a label to green?
setProperty("label1", "text-color", "green");
setText("label1", "green");
setTextColor("label1", "green");
textColor("label1", "green");
What type of element does getText("userNameInput") work with?
Image
Button
Text input field
Dropdown
Which line of code sets a button's text to "Submit"?
getText("submitBtn", "Submit");
setText("submitBtn", "Submit");
submitBtn.text = "Submit";
buttonText("submitBtn", "Submit");
What happens in the following code?
onEvent("btnSend", "click", function() {
var msg = getText("messageInput");
setText("outputLabel", msg);
});
It stores a message permanently
It sends a message to another app
It displays user input from a text field in a label
It creates a new variable
Which code sets a dropdown's selected item to "High"?
setText("dropdown1", "High");
setProperty("dropdown1", "value", "High");
getText("dropdown1");
dropdown1.value = "High";
Which code shows how to respond to a user clicking a button named "submitBtn"?
submitBtn.on("click", function() {})
onEvent("submitBtn", "click", function() { });
submitBtn.addEvent("click", function() {});
onClick("submitBtn", function() {});
How can you use input from a text field to customize a greeting message?
onEvent("greetBtn", "click", function() {
var name = getText("nameInput");
setText("greetingLabel", "Hello " + name);
});
It alerts the user
It changes the nameInput
It displays a greeting using input
It sets a variable to the label name
Which statement correctly changes the font size of a label?
setText("label1", "font-size", "20px");
setProperty("label1", "font-size", "20px");
label1.style.fontSize = "20px";
setStyle("label1", "font-size", "20px");
What is the purpose of using onEvent in an app?
To declare a variable
To draw shapes on screen
To detect and respond to user actions
To store user data permanently
Which combination of commands would let you design a calculator that multiplies two user-entered numbers and shows the result in a label?
getText, setProperty, onEvent
getNumber, setText, onEvent
onEvent, drawRect, console.log
var, setScreen, getText
You want to make a quiz app. Which function lets you update the label after a user submits their name?
getProperty
getText + setText inside onEvent
setImageURL
setProperty
You're designing a form that accepts user age and prints a message. Which code block is best?
onEvent → getNumber → setText
setText → getNumber → getText
onEvent → getText → setScreen
getText → onEvent → setNumber
Which of these is necessary to create a login screen?
getText and setText only
onEvent and getText
setProperty only
getNumber only
You want to collect feedback from users. Which would you use?
setImageURL and onEvent
getText, onEvent, and setText
getNumber only
setScreen only
What is the best way to create an app that displays different messages depending on user input?
Use a fixed label
Use getText and if-else
Use setScreen
Use setProperty only
If a user types a number in a field and presses a button to show the double of that number, which logic works?
setText("label", getNumber("input") + 2);
setText("label", getText("input") * 2);
setText("label", getNumber("input") * 2);
getNumber("input");
In a shopping cart app, how can you display the final price based on quantity and price per item?
Use getText only
getNumber + multiplication + setText
getNumber + subtraction + getText
setText only
To reset all fields in a form, which actions are needed?
Reload the screen
setText("", "") for each field
getText("")
onEvent("", "change")
What will be the output of this code?
var total = 10 + 20;
setText("resultLabel", total);
10
10 + 20
30
20
What will occur if you try to setText("label1", null);?
It sets the label to an empty string
It causes a runtime error
It sets the label to 'null'
It does nothing
What is the result of calling getText("inputField"); if the field is empty?
Causes an error
Returns undefined
Returns an empty string
Returns null
Which function is used to change the visibility of a UI element?
setProperty()
showElement()
setVisible()
toggleVisibility()
What is the purpose of the getText function in code.org JavaScript?
To delete a text element
To create a new text element
To set the text of a UI element
To retrieve the value from a UI element
What will happen if you try to use a variable that has not been declared?
It will throw a reference error
It will return null
It will display undefined
It will cause a syntax error
What will be the output of the following code?
var result = getNumber("inputField");
setText("outputLabel", result * 2);
It displays nothing
It shows an error
It shows the input as a string
It shows the input multiplied by 2
Which code correctly retrieves the value from a checkbox?
var isChecked = getProperty("checkbox1", "checked");
var isChecked = getText("checkbox1");
var isChecked = getValue("checkbox1");
var isChecked = checkbox1.value;
What does the following code do?
onEvent("btnReset", "click", function() { setText("outputLabel", ""); });
Changes the button text
Sets outputLabel to 'btnReset'
Clears the text of outputLabel
Displays an error
