Answer to Question #169159 in Java | JSP | JSF for Chandra sena reddy

Question #169159

Square at Alternate Indices

Given an array

myArray of numbers, write a function to square the alternate numbers of the myArray, starting from index 0 using the array method map.Input

  • The input will be a single line containing an array myArray

Output

  • The output should be a single line containing an array with alternate numbers squared

Constraints

  • Each value in the array must be a number


Sample Input 1

[ 1, 2, 3, 4, 5 ]


Sample Output 1

[ 1, 2, 9, 4, 25 ]


Sample Input 2

[ 2, 4 ]


Sample Output 2

[ 4, 4 ]


and the given code to fill the function is :


"use strict";


process.stdin.resume();

process.stdin.setEncoding("utf-8");


let inputString = "";

let currentLine = 0;


process.stdin.on("data", (inputStdin) => {

 inputString += inputStdin;

});


process.stdin.on("end", (_) => {

 inputString = inputString.trim().split("\n").map((str) => str.trim());

 main();

});


function readLine() {

 return inputString[currentLine++];

}


/* Please do not modify anything above this line */


function main() {

 const myArray = JSON.parse(readLine());

  

    /* fill the code in this function */


}





1
Expert's answer
2021-03-11T06:30:21-0500
<!DOCTYPE html>
<html>
<body>

<h2>The required result are given below</h2>

<p>The value of array which is at odd place will get in square.</p>

<p id="demo"></p>

<script>
var x = [ 2, 4 ];
var arr=" ";

var i;
for(i = 0;i< x.length; i++){

    if(i%2==0){
    arr +=x[i]*x[i]+",";
    }else{
    arr +=x[i]+","
    }
}
document.getElementById("demo").innerHTML ="The value of array is: " + arr;
</script>

</body>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

chandrasena reddy
10.03.21, 10:52

Sir, for second output this logic was not working.

Leave a comment

LATEST TUTORIALS
New on Blog