Question: Given the following JavaScript code: <script language=javascript type=
ID: 3851423 • Letter: Q
Question
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 is : 0123456789
Code for printing numbers in backwards:
<script language="javascript" type="text/javascript">
for(i=9;i>=0;i--){
document.write(i);
}
</script>
Code for printing only even numbers:
<script language="javascript" type="text/javascript">
for(i=0;i<10;i++){
if((i%2)==0){
document.write(i);
}
}
</script>
Code for calculating the sum of values of i:
<script language="javascript" type="text/javascript">
var finalSum=0;
for(i=0;i<10;i++){
finalSum = finalSum + i;
}
document.write(finalSum);
</script>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.