Question

Instructions from your teacher. Medium Question #4 Write a function called addNeighbors that takes an array of numbers and adWrite a function called addNeighbors that takes an array of numbers and adds each number in the array to the number next to it. The array will always have an even number of numbers in it. In the end you should return an array of neighbors added up. Please use JavaScript

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<!doctype html>
<html>
<head>
<script>
function addNeighbors(arrr)
{
var lenn=arrr.length,i;
var array=new Array(lenn-1);
if(lenn%2==0)
{
for(i=0;i<lenn-1;i++)
array[i]=arrr[i]+arrr[i+1];
return array;
}
else
{
document.write("<br>"+"Enter array with only even length"+"<br>");
}
}
function printresult(result)
{
document.write("<br>");
for(var j=0;j<result.length;j++)
   document.write(result[j]+" ");
}
document.write("<br>"+"for array 1");
var result1=addNeighbors([1,2,3,4,5,6,7,8]);
printresult(result1)
document.write("<br>"+"for array 2");
var result2=addNeighbors([12,32,35,28,10,18,55]);
printresult(result2)

</script>
</head>
<body>
</body>
</html>

output:

Add a comment
Know the answer?
Add Answer to:
Write a function called addNeighbors that takes an array of numbers and adds each number in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Write a function called fillArray that will fill an array of any constantly declared...

    In C++ Write a function called fillArray that will fill an array of any constantly declared array size variable with random numbers in the range of 1 - 100. Write a function called printArray that will print an array of any size. Write a third function called countEvens which will count and return the number of even numbers in the array. In main create an array that holds 25 ints. Use your functions to fill, print, and count the number...

  • create a javascript function named sumBeyond that takes in an array of numbers and returns the...

    create a javascript function named sumBeyond that takes in an array of numbers and returns the summation of the values in the array that are greater than (but not including) 42. If there are no values greater than 42, the function should return 0. example- sumBeyond([2,85,932,0,7,-5,0,32]) must return 1017

  • write an algorithm function called balance() in javascript that takes in a string and returns a...

    write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • 1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines...

    1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines whether two distinct elements of the array add up to some specified value, "key". If so, return 1. Otherwise, return 0. The function signature is: int canMakeSum(int *array, int n, int key);

  • Write a function with two input parameters: an array of numbers and the size of the...

    Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • Write a method that takes an array of ints and that returns true if there are...

    Write a method that takes an array of ints and that returns true if there are more odd numbers than even numbers. However, as for a casino, 0 is not counted as an odd or an even number. If the array is empty or contains only 0's the method should return false.

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • Write a function in the C++called summary that takes as its parameters an input and output...

    Write a function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values. Please write program in C++.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT