i am trying to add 5 element in array and print sum of elements in java script without function
<HTML> <body> <script type="text/javascript"> var x=new Array(5); for(i=0;i<x.length;i++){ x[i]=prompt("enter Any Value") <!-- its properly working till now --> t=0; for(t+= x[i];) for(i=0;i<x.length;i++){ document.write(x[i]); } </script> </body> </HTML>
Why dont you try
var sum=0;
var times = 5;
for(i=0;i<times;i++){
sum+=parseInt(prompt("enter Any Value"));
}
console.log(sum);
document.write(sum);
Try this:
<HTML>
<body>
<script type="text/javascript">
var x=new Array(5);
t=0;
for(i=0;i<x.length;i++){
x[i]=prompt("enter Any Value") <!-- its properly working till now -->
t+= x[i];
}
document.write(t);
</script>
</body>
</HTML>
Corrected your code:
<HTML>
<body>
<script type="text/javascript">
var x = new Array(5);
var sum = 0;
for(i=0;i < x.length;i++){
x[i] = parseInt(prompt("enter Any Value")); <!-- its properly working till now -->
sum += x[i];
}
alert("Sum is: " + sum);
</script>
</body>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.