quiz image

JavaScript Loop to Display Every Other Element in an Array

ReasonedWetland avatar
ReasonedWetland
·
·
Download

Start Quiz

Study Flashcards

4 Questions

Write a JS loop that will display every other element in an array, arr For example, if arr=[6,1,5,7,2,4,4,8,0,6,9,1] your code should display 6 5 2 4 0 9 Use document.write to display your results. The numbers should be separated by spaces

What does the array arr contain after the following JavaScript code is executed? var arr = new Array(10,1,3,2,4); for (var i = 1; i < arr.length; i = i + 1) { arr[i] = arr[i -1] + 1; }

Consider the following JavaScript code. Complete that one assignment statement above so that the value of taxAmount is computed as 7% of the function parameter cost

Write a JS function, sumOfHalf, the will return the sum of the first half of the elements in its array parameter. Assume the array has at least 2 elements.

Study Notes

JavaScript Loops and Arrays

  • Create a JavaScript loop to display every other element in an array arr, using document.write to display the results, with numbers separated by spaces.

Array Modification

  • The array arr contains [10, 2, 4, 5, 6] after the code var arr = new Array(10,1,3,2,4); for (var i = 1; i &lt; arr.length; i = i + 1) { arr[i] = arr[i -1] + 1; } is executed.

Tax Amount Calculation

  • Complete the assignment statement to compute the value of taxAmount as 7% of the function parameter cost, like this: taxAmount = cost * 0.07;

Sum of First Half

  • Write a JavaScript function sumOfHalf that returns the sum of the first half of the elements in its array parameter, assuming the array has at least 2 elements.
  • The function would take an array as input, calculate the middle index, and then sum up the elements from the start to the middle index.

Learn how to write a JavaScript loop that displays every other element in an array using document.write. For the given array arr=[6,1,5,7,2,4,4,8,0,6,9,1], the code should display 6 5 2 4 0 9 with numbers separated by spaces.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

JavaScript Functions and Loops Quiz
10 questions
JavaScript Loops
12 questions
While Loops in JavaScript
5 questions
Use Quizgecko on...
Browser
Browser