Question

In: Computer Science

Write a java script function that accepts integer array as input, and display a new array...

Write a java script function that accepts integer array as input, and display a new array by

performing fol

lowing modifications,

The values in odd index positions must be incremented by 1

The values in even index positions must be decremented by 1.

Assume the array index starts from 0.

Input 1: arr = [1,2,3,4]

Output 1: arr = [0,3,2,5

it done html and javascript and plz do it in simple way

Solutions

Expert Solution

I have created oddEven.html file which contains below function:

function oddEven(myArray) :- This function incerement value of odd index and decrement value of even index of myArray which is passed as an argument.

Here, we just need to find odd index and even index and do the increment and decrement array value. So, we can find odd and even index using modulo by 2.

example: index = 1 (1 % 2 == 0) so here index is odd because 1 % 2 is not equal to zero. It is 1.

oddEven.html :-

<html>
    <head>
        <title>Odd Even</title>
    </head>
    
    <body>
        
        <script type="text/javascript">
            
            // This function incerement value of odd index and decrement value of even index of myArray which is passed as an argument.
            function oddEven(){
                
                for(var i=0; i<myArray.length; i++){
                    
                    // now get the odd index using modulo by 2 == 0
                    if(i % 2 != 0){
                        
                        // increment value of odd index
                        myArray[i]++;
                    }else{
                        
                        // decrement value of even index
                        myArray[i]--;
                    }
                        
                        
                }
                
                // display the modified array
                document.write("<br>Output 1: array = [ "+myArray+" ]");
            }
            
            
            // intialize array with values
            var myArray = [10, 20, 30, 40, 50];
            
            // display the modified array
            document.write("Input 1: array = [ "+myArray+" ]");
            
            // call the oddEven(myArray) which returns the modified array
            oddEven(myArray);
            
        </script>
        
    </body>
</html>

Output:-

According to output, array values are modified based on index positions.

I hope you will understand above program.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

Software Decode: Write a function that accepts an in-order array of unsigned integer values. The function...
Software Decode: Write a function that accepts an in-order array of unsigned integer values. The function shall then scan the array for a specific pattern: Three values contained within the array equally spaced 20 units apart. The function shall return the index position within the original array where the pattern begins or -1 if not present. Given the input array: data[] = {10,20,31,40,55,60,65525} The function shall return: 1 IN JAVA PLEASE
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT