Question

Javascript function that takes an array parameter and returns an array value. The assumption is that...

Javascript function that takes an array parameter and returns an array value. The assumption is that the array is an integer array. The returned array should be composed of the square of the elements in the array parameter for the corresponding position.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

function sayHello(arr) //function taking array as parameter and returning array result
{result=[];//array to store result

//calculating square of each element and storing in the result array
for (i=0; i<arr.length; i++){
sqr=Math.pow(arr[i],2);
result.push(sqr);
}
return result; //returning result array
}
var x = [1,2,3]; //intializing x array
var y=sayHello(x); //calling function and passing array x

//printing resulting array in alert
for (i=0; i<y.length; i++){
alert(y[i]);
}

//Please let me know in case of any doubt,Thanks.

Add a comment
Know the answer?
Add Answer to:
Javascript function that takes an array parameter and returns an array value. The assumption is that...
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
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