Given the following JavaScript code: <script language=javascript type=\"text/jav
ID: 3851424 • Letter: G
Question
Given the following JavaScript code:
<script language=javascript type="text/javascript">
for (i=0;i < 10; i++) {
document.write(i);
}
</script>
• What is the output? • How would you modify the code so that it prints the numbers backwards? Please list your codes. • How would you modify the code so that it prints only the even numbers? Please list your codes. • How would you modify the code so that it sums all the values of i and prints the final sum? Please list your codes.
Explanation / Answer
OUTPUT:
0123456789
Program toprints the numbers backwards
<script language=javascript type="text/javascript">
for (i=10;i > 0; i--) {
document.write(i);
}
</script>
Output:
10987654321
Program to prints only the even numbers:
<script language=javascript type="text/javascript">
for (i=10;i > 0; i--) {
if(i%2==0){ #Checking for even numbers
document.write(i);
}
}
</script>
Output:
108642
Program to sums all the values of i and prints the final sum:
<script language=javascript type="text/javascript">
sum = 0;
for (i=10;i > 0; i--) {
sum = sum + i; #adding i values
}
document.write(sum);
</script>
Output:
55
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.