Square at Alternate Indices
Given an array
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 */
}
<!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>
Comments
Sir, for second output this logic was not working.
Leave a comment