JavaScript Loop to Display Every Other Element in an Array
4 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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.

Signup and view all the answers

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.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

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.

More Like This

Use Quizgecko on...
Browser
Browser