Programming and AI
Your guide to
JavaScript
Chapter 1
Review
1. Select the correct program to output "Hello" to the console.
a) [Link]("Hello");
b) [Link]("Hello");
c) consolelog("Hello")
d) [Link](Hello);
2. Create a program to output "Good morning" to the console.
…………………………………………………………………………………………….
3. Select the correct way to write your comment.
a) /Write your comments here./
b) //Write your comments here.
c) /Write your comments here.
d) Write your comments here.//
Chapter 2
Calculations and Strings
Revision
1. Select the correct program to output the calculation results of "50+100" to the
console.
a) [Link]("50" + "100");
b) [Link](50" + "100);
c) [Link](50 + 100);
d) [Link]("50 + 100");
2. Select the correct symbol for the division used in the program.
a) *
b) /
c) #
d) ÷
3. Let the computer calculate "19+40" and output the calculation results to the console.
………………………………………………………………………………………………………
4. Let the computer calculate "7-2" and output the calculation results to the console.
………………………………………………………………………………………………………
5. Let the computer calculate "15 divided by 5" and output the calculation results to the
console.
………………………………………………………………………………………………………
6. Select the correct program to output "Today is the 17th.7 days ago was the 10th." to the
console.
a) [Link]("Today is the 17th.7 days ago was the + 17 - 7 + th.");
b) [Link]("Today is the 17th.7 days ago was the " + "17 - 7" + "th.");
c) [Link]("Today is the 17th.7 days ago was the " + (17 - 7) + "th.");
d) [Link]("Today is the 17th.7 days ago was the (17 - 7)th.");
7. Let the computer calculate "2*5" and output "The answer is 10." to the console.
………………………………………………………………………………………………………
Chapter 3
Variables
Revision
1. Create a variable number, Assign 100 to the variable number. then output the
value of number to the console.
………………………………………………………………………………
………………………………………………………………………………
………………………………………………………………………………
2. In the previous program, (1) Assign the value of the variable number plus 10
(2) Output the value of variable number to the console
………………………………………………………………………………
………………………………………………………………………………
3. What will be the output of the following code ?
………………………………………………………………………………
Chapter 4
Mid-Term Exam
1. Select the correct program to output "Hello" to the console.
a) [Link]("Hello"); b) consolelog("Hello")
c) [Link]; d) [Link]("Hello");
2. Output "Hello" to the console.
………………………………………………………………………………………………………….
3. Create your program as follows:
-Change "Leave this text." to a comment.
-Execute the program and it will output
"Make the first line a comment.”
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
4. Select the correct program that will let the computer calculate "5+7" and output the
calculation results to the console.
a) [Link](5" + "7); b)[Link]("5 + 7");
c) [Link]("5" + "7"); d) [Link](5 + 7);
5. Select the correct symbol for multiplication used in the program.
a) / b) + c) - d) *
6. Let the computer calculate "3*5" and output "The answer is 15." to the console.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
7. Select the correct declaration for the variable number.
a) let = number; b) number let;
c) number; d) let number;
8. Create your program as follows
(1) Declare the variable num.
(2) Assign 5 to num
(3) Output the value of num to the console
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
Chapter 5
If statement
Case1: Test 1 condition
Solution
Revision
1. Create your program as follows.
(1) Declare the variable num and assign 5 to it.
(2) Output the value of variable num to the console.
However, the entire program should be written on two lines (the declaration and initial
value assignment should be written on one line).
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
2. Select a program that says, "If ~, then ________."
a) (conditional) if {processing} b) (conditional){processing} if
c) if {processing}(conditional) d) if (conditional){processing}
3. Select the conditional expression "the value of the variable num is equal to 50".
a) 50 >= num b) num = 50
c) num == 50 d) 50 = num
4. Create your program as follows
(1) Declare the variable num and assign 100.
(2) If variable num is equal to 100, output "Correct".
5. Please select the output result of the following program.
a) true b) num
c) 100 d) false
Comprehensive revision on chapters (1-5)
1. Which line has an error?
2. Write a JavaScript program to calculate 19 + 40 and output the result.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
3. Select the correct way to declare a variable:
a) let = number; b) number let;
c) number; d) let number;
4. Write a JavaScript program that calculates the total cost of three shopping items and
gives a message based on the total amount.
Instructions:
1. Add the following comment at the top of your program :” This program calculates
total shopping cost”
2. Create three variables named item1, item2, and item3.
3. Assign any numeric values to them (25, 15, 10).
4. Calculate the total cost and store it in a variable named total.
5. Output the price of each item and the total cost.
6. Calculate and output the average price per item.
7. Use an if statement:
o If total is 50 or more, output "Get a discount!".
o Otherwise, output "Add more items to get a discount!".
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
Chapter 6
if-else statement
Case2
Solution
Review
1. Select a program that says if ~ then do A, otherwise do B.
a) if (conditional){A}
else{B}
b) if (conditional) else{B}
{A}
c) else (conditional){A}
if {B}
d) else{B}
if (conditional){A}
2. Create your program as follows.
(1) Declare the variable num and assign 5 to it.
(2) If variable num is the same as 10, output "Win".
(3) Otherwise, output "Lose".
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
3. Select a program with the conditional expression "the value of the variable num is less
than or equal to 80".
a) num < 80 b) num == 80
c) num <= 80 d) num > 80
4. Select a program with the condition "the value of the variable num is greater than 30".
a) num < 30 b) num == 30
c) num <= 30 d) num > 30
5. Create your program as follows
(1) If the variable score is greater than or equal to 60, “Pass" is output;
otherwise, “Fail" is output.
Please do not delete the program that has already been written and input the
conditional expression part.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
Chapter 7
else if statements
Case3: Test multiple conditions
Solution
Review
1. Choose a program that says, "If ~, do A; if not, do B."
a) if (conditional){A} b) if (conditional){A}{B}
else if {B} else if (conditional)
c) if (conditional){A} d) if (conditional) (conditional)
else if (conditional){B} else if {A}{B}
2. select the output result of the following program.
a) So close b) Perfect score
b) Pass d) No output
3. Create your program as follows
(1) Declare the variable "egg" and assign 5 to it.
(2) If the variable egg is 0, output "No stock".
(3) If not, and the variable egg is less than or equal to 3, output "Low stock".
(4) If not, output "In stock".
Chapter 8
Revision (Mid-term)
1. Create your program as follows
(1) Declare the variable num and assign 7.
(2) If variable num is equal to 7, output "Correct".
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
2. Please select the output result of the following program.
a) 10 b) true
c) false d) 100
3. Select a program with the conditional expression "the value of the variable num is less than
20".
a) num >= 20 b) num <= 20
c) num == 20 d) num < 20
4. Create your program as follows.
(1) Declare the variable num and assign 100 to it.
(2) If variable num is equal to 100, output "correct". (3) If not, "miss" is output.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
5. Please select the output result of the following program.
a) So close b) Perfect score
c) No output d) Pass
Chapter 9
Logical operator
Symbol Name meaning Example
&& AND Both conditions should be true if (age > 18 && score >= 50){ }
|| OR Only one condition should be true if (age >= 18 || age < 18 ){ }
Review
1) Select a program that means "A and B"
a) A && B b) A // B
c) A & B d) A || B
2) Select the program that means "A or B".
a) A && B b) A // B
c) A & B d) A || B
3) Please select the output result when executing the following program.
a) Error b) No output
c) Regular Price d) 20% OFF
4) Please create a program as follows.
If variable a is equal to 5 or b is equal to 10, output "Condition met".
5) Please create a program as follows.
If variable a is greater than or equal to 2 and b is greater than or equal to 3, output
"Condition met.
Chapter 10
Iterative operation
For statement (Loops)
for (start; condition; update){ }
NOTES: - count = count + 1 can be written as count++
- in the programming world, in for statement ( ) , the variable is often named i
NOTE: 1- Break a line after the left curly bracket “ { ” and before the right curly bracket “}”.
2- Use indentation (space at line start) to make code easier to read
Revision
1) Select the first thing you would write in a program that represents "outputting the word "apple"
to the console 10 times over.
a) let b) for
c) num d) if
2) Select the correct program to add 1 to variable i.
a) i+ b) i++
c) 1+1 d) i++1
3) Please select the correct answer for the program that repeats the specified process 10 times.
a) for (i<10; let i=0; i++) { b) for {Specified process} (
Specified process let i=0; i<10; i++
} )
c) for (let i=0; i<10; i++) { d)
Specified process for (let i=0; i++; i<10) {
} Specified process
}
4) Create your program less than or equal to
Output "Hello" to the console 5 times.
*Variable name is i and assign 0 at the beginning.
*Use the increment operator.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
5) Create your program less than or equal to
Output "Hello" to the console 7 times.
*Variable name is i and assign 0 at the beginning.
Use the increment operator.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
Chapter 11
Mid Term Test
1) Select the program that means "A or B"
a) A && B b) A < B
c) A == B d) A || B
2) Please select the output result of running the program less than or equal to
3) Let num = 9;
4) if (num <= 3 || num >= 7) {[Link]("A");}
else{[Link]("B");}
a) B b) 9
c) No output d) A
3) Select the correct program to add 1 to variable i.
a) 1+1 b) i+
c) i++1 d) i++
4) Select the correct answer for the program that repeats the specified processing 10 times.
a) for { Specified process } ( b) for (let i=0; i++; i<10) {
let i=0; i<10; i++ Specified process
) }
c) for (i<10; let i=0; i++) { d) for (let i=0; i<10; i++) {
Specified process Specified process
} }
5) Please select the output result of running the program less than or equal to
6) for (let i = 0; i < 2; i++) {
7) [Link]("Hello");
};
a) Hello b) No output
Hello
Hello
c) Hello d) Hello
Hello
6) Create your program less than or equal to
(1) Output "Good morning" 10 times to the console.
*Variable name is i and assign 0 at the beginning.
Use the increment operator.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
7) Create your program less than or equal to
If variable a is greater than or equal to 2 and b is greater than or equal to 3, output "Condition
met.
*Only the conditional expression should be entered.
8) Create your program less than or equal to
(1) Output "Hello" three times to the console.
*Variable name is i and assign 0 at the beginning.
Use the increment operator.
Chapter 12
Functions (1)
• Functions: allow you to perform a predefined process quickly.
• Functions are named according to the processing they can perform.
• A function takes some data(arguments), performs a specified processing,
and returns the result.
Types of Functions
1- [Link](9.6)
• The function round off the value.
2- [Link](num1,num 2, …..)
• The function tells the maximum value
3- [Link](num1,num 2, …..)
• The function tells the minimum value
Problem1: Create a program to Round "9.7" using the function [Link] and output to the
console
Answer:
NOTE:
1-The value given to the function is called the "argument" (such as 9.8, 10.3)
2-The value returned as a result of the calculation is called the "return value" (such as 10)
NOTE:
1. M in [Link]( ), [Link]( ),…. Should be in capital letter.
2. you cannot put "." (dot) in variable and function names.
How to create your own function ?
• To create a function:
function function_Name ( argument ) { process }
Problem1:
Create a function with the function name split5 and the argument name total
Declare the variable result and assign the calculation formula "total / 5"
Pass the argument "12,500" to the function split5 and output to the console
Answer:
Problem2:
Revision
1. Select the correct program to create a function whose function name is calc and argument
name is num.
a) (num)|calc function { b) function (num) calc {
processing processing
} }
c) (num)function calc{ d) function calc(num) {
processing processing
} }
2. Select the correct function to round off.
a) [Link](1.8) b) round(1.8)
c) [Link](1.8) d) [Link](1.8)
3. Please select the output results of the following program that you have executed.
a) 120 b) 60
c) 20 d) 40
4. Please select the correct answer to be displayed in the console as a result of executing the
following program:
5. function calc(num) {
6. let result = num * 3;
7. return result;
8. }
[Link](calc(5));
a) 15 b) 0
c) 3 d) 5
5. Please add a program that calls the function calc.
Please put "3" as an argument.
6. Please add a program that returns the value of the variable result to the described program.
Chapter 13
Functions (2)
[Link]( )
it is a function that truncates decimals (rounds a number down) to the nearest integer!
[Link](5.5)
Problem 1:
Create your program as follows.
(1) Pass the argument 7.8 to the function [Link] and output to the console
(2) Pass the argument 7.8 to the function [Link] and output to the console
5- [Link]()
It is a function that returns a random "numerical value greater than or equal to 0".
NOTE: Functions can be combined with other functions.
Problem 2:
Output the minimum values of "5" and "50" to the console using the function [Link].
Answer:
Problem 3:
Create a function that adds two numerical values.
(1) Create a function with the function name sum and the argument names num1 and num2
(2) Assign "num1 + num2" to the variable result
(3) Return the variable result
(4) Pass the arguments 10 and 20 to the function sum and call it to output to the console
Answer:
Revision
1. Select the correct program to output random numerical values.
a) [Link]([Link]()); b) [Link](random());
c) [Link]([Link]()); d) [Link]([Link]());
2. Select the correct function to truncate decimals.
a) [Link]([Link](10.5)); b) [Link]([Link](10.5));
c) [Link]([Link](10.5)); d) [Link]([Link](10.5));
3. Select the output results of the following program.
[Link]([Link](7.7));
a) No output b) 7
c) 6 d) 8
4. Enter the program as follows.
(1) Use the function to round off "5.5" and output to the console.
(2) Use the function to round down “8.8" to the nearest whole number and output to the
console.
Use the functions "[Link]" and "[Link]".
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
5. Create a program that outputs “Excellent luck" if the random numerical value is greater than
or equal to 0.5, and "Bad luck" otherwise.
Use the function "[Link]()" and enter the conditional expression "if the random value is
greater than or equal to 0.5".
6. Create a program that returns a random integer as follows.
(1) Use the function "[Link]" to return a random numerical value.
(2) Multiply the randomly returned numerical value by 10.
(3) Use the function "[Link]" to return an integer.
*The program is one line.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
Chapter 14
Mid-term Test
1. Select the correct function to round off.
a) round(1.8) b) [Link](1.8)
c) [Link](1.8) d) [Link](1.8)
2. Select the output result of running the following program.
[Link]([Link](10,50,100));
a) 100 b) 50
c) 160 d) 10
3. Select the correct program to output random numerical values.
a) [Link]([Link]()); b) [Link]([Link]());
c) [Link](random()); d) [Link]([Link]());
4. Select the output results of the following program.
[Link]([Link](12.8));
a) 13 b) 12
c) No output d) 11
5. Create a program that returns a random integer as follows.
(1) Use the function [Link] to return a random numerical value.
(2) Multiply the randomly returned numerical value by 10.
(3) Use the function [Link] to return an integer.
*The program is one line.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
6. Select the correct program to create a function whose function name is sum and argument
name is num.
a) function sum(num) { b) (num)sum function {
processing processing
} }
c) (num)function sum{ d) function (num) sum {
processing processing
} }
7. Add a program that returns the value of the variable result to the described program.
8. Add a program that calls the function calc.
Enter 7 as an argument
Chapter 15
• To output the type of variables:
• To compare two values:
• == : It return “True” ,If only the values are the same
• ===: It return “True” ,If the values and the type are the same
• To change the type of a variable:
1. [Link](Number("100")); >>>>>Output>>>>>>> 100 (Type: number)
2. [Link](String(100)); >>>>> Output >>>>>> 100 (Type: string)
3. [Link](Number("Hello")); >>>>> Output >>>>>> NaN (Not a Number)
Review
1. Select the correct data type below.
[Link]("50+30");
a) No particular data type b) Numerical value
c) String d) None of them
2. Select the correct program that outputs "string".
a) [Link](string "1"); b) [Link](typeof "1");
c) [Link](Number "1"); d) [Link](typeof 1);
3. Select the output results of the following program.
a) numerical value b) true
c) false d) string
4. Select the output results of the following program.
a) number b) true
c) false d) NaN
5. Create your program as follows.
(1) Determine the data type of "7" in the computer and output to the console.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
Chapter 16
Array
• An array: It is a collection of multiple values that can be added together.
• Values are separated by , in [ ]
• Each value in the array has location index starts from 0 .
• To retrieve the value of an array:
variable name[ location number ] >>>>>> tempList [4]
• To see all values of an array:
[Link](tempList);
• To calculate the length of an array(number of values):
[Link]([Link]);
Revision
1- Select the output result of executing the following program.
2- let num = [5, 10, 15, 20, 25];
3- [Link](num[2]);
4-
a) 20 b) 2
c) 10 d) 15
5- Output all the values in the array to the console.
*Do not enter values directly.
*Do not delete the written program, but add the program in [Link].
6- Select the output result of executing the following program.
7- let num = [1, 2, 3];
8- let sum = 0;
9- for (let i = 0; i < 3; i++) {
10- sum = sum + num[i];
11-}
[Link](sum);
a) 3 b) 6
c) 1 d) 2
7- Output the length of the array to the console.
*Do not enter the numerical value of the length directly.
Chapter 17
Midterm Test
1. Select the correct data type below.
[Link]("11");
a) string b)No particular type
c) Numerical value d) boolean
2. Select the correct program that outputs "string".
a) [Link](String "50"); b) [Link](typeof "50");
c) [Link](Number "50"); d) [Link](typeof 50);
3. Select the output results of the following program.
[Link](200 === 20);
a) true b) string
c) number d) false
4. Select the output results of the following program.
[Link](Number("value"));
a) true b) NaN
c) number d) false
5. Create your program as follows.
(1) Computer determines the data type 100 and outputs it to the console
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
6. Put the following values in the array.
10, 50, 100, 150
*Nothing will be output when executed.
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….
7. 7-Select the output result of executing the following program.
let num = [5, 10, 15, 20, 25];
[Link](num[3]);
a) 10 b) 20
c) 5 d) 15
8. Output all the values in the array to the console.
*Do not enter values directly.
*Do not delete the written program, but add the program in [Link].
9. Select the output result of executing the following program.
10. let num = [2, 4, 6];
11. let sum = 0;
12. for (let i = 0; i < 3; i++) {
13. sum = sum + num[i];
14. }
[Link](sum);
a) 12 b) 2
c) 6 d) 4
10. Output the length of the array to the console.
*Do not enter the numerical value of the length directly
………………………………………………………………………………………………………….
………………………………………………………………………………………………………….