JavaScript - Listing Odd Numbers Flashcards

1
Q

What is the Code to List out Odd Numbers ?

A
<script>
let oddNumbers = []; 
for (let i = 1; i <= 20; i+= 2) { 
  oddNumbers.push(i); } 
document.write(oddNumbers);
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the point of :

?
A

It starts the Javascript code within a HTML document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the point of : let oddNumbers = []; ?

A

It starts by defining the function, letting it be blank to start off shown with the empty square brackets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the point of : for (let i = 1; i <= 20; i+= 2) { ?

A

This JavaScript code is setting up a for loop. Here’s a breakdown of what it does:
1. Initialization: let i = 1; - This declares the variable i and initializes it with a value of 1.
2. Condition: i <= 20; - This specifies that the loop will continue as long as i is less than or equal to 20.
4. Increment: i += 2 - This increases the value of i by 2 after each iteration of the loop.
This line will get all the odd numbers basing it off the increment, till the condition and starting of from let.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the point of : oddNumbers.push(i); } ?

A

Adding oddNumbers.push(i); inside the loop means that each odd number will be added to the oddNumbers array. The push adds the specified elements based on the for loop to the end of the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the point of : document.write(oddNumbers); ?

A

It will print out the variable oddNumbers based on the code above, it wont print ‘oddNumbers’ as a string as there are no quotation marks around oddNumbers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the point of : </script> ?

A

Ends the Javascript code within a HTML document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly