In: Computer Science
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
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.