admin管理员组文章数量:1414902
I am creating a JavaScript application where I want to generate an ArrayIndexOutOfBound Exception. I have created an array of size 5
and I am trying to insert elements from the Fibonacci series into it. Ideally, it should throw an exception after inserting 6th element, but it is not throwing any exception. Please check the code and let me know your views.
Attached code and screenshots of the output.
<!DOCTYPE html>
<html>
<head>
<title>Error Handling Demo</title>
<script type="text/javascript">
var i = 1;
var fibona = [];
fibona.length=5;
function generateNext()
{
if(i>1)
{
number1.value = number2.value;
number2.value = number3.value;
}
number3.value = parseInt(number1.value) + parseInt(number2.value);
i++;
fibona.push({'num1':number1.value, 'num2':number2.value, 'num3':number3.value});
}
</script>
</head>
<body>
<h1>Fibonacci Series Game</h1>
<p>
Number 1 :
<input type = "number" name = "number1" value = 0 id = "num1">
</p>
<p>
Number 2 :
<input type = "number" name = "number2" value = 1 id = "num2">
</p>
<p>
Next Number :
<input type = "number" name = "number3" id = "num3">
</p>
<p>
<input type = "button" name = "generatenext" value = "Generate Next Number" onclick = "generateNext()">
</p>
</body>
</html>
I am creating a JavaScript application where I want to generate an ArrayIndexOutOfBound Exception. I have created an array of size 5
and I am trying to insert elements from the Fibonacci series into it. Ideally, it should throw an exception after inserting 6th element, but it is not throwing any exception. Please check the code and let me know your views.
Attached code and screenshots of the output.
<!DOCTYPE html>
<html>
<head>
<title>Error Handling Demo</title>
<script type="text/javascript">
var i = 1;
var fibona = [];
fibona.length=5;
function generateNext()
{
if(i>1)
{
number1.value = number2.value;
number2.value = number3.value;
}
number3.value = parseInt(number1.value) + parseInt(number2.value);
i++;
fibona.push({'num1':number1.value, 'num2':number2.value, 'num3':number3.value});
}
</script>
</head>
<body>
<h1>Fibonacci Series Game</h1>
<p>
Number 1 :
<input type = "number" name = "number1" value = 0 id = "num1">
</p>
<p>
Number 2 :
<input type = "number" name = "number2" value = 1 id = "num2">
</p>
<p>
Next Number :
<input type = "number" name = "number3" id = "num3">
</p>
<p>
<input type = "button" name = "generatenext" value = "Generate Next Number" onclick = "generateNext()">
</p>
</body>
</html>
Share
Improve this question
edited Aug 29, 2016 at 6:42
Kristijan Iliev
4,99710 gold badges30 silver badges51 bronze badges
asked Aug 29, 2016 at 6:35
pavan shahpavan shah
1292 silver badges13 bronze badges
2
-
In JS, setting an array's
.length
property does not set the maximum number of elements that the array can contain. I'm not aware of any way to restrict the number of elements. – nnnnnn Commented Aug 29, 2016 at 6:39 -
@TravisRodman - No, that will just return
undefined
(if reading the value) or happily create the element at that index (if writing the value). – nnnnnn Commented Aug 29, 2016 at 6:41
3 Answers
Reset to default 2What you are experiencing is the normal behavior of the push
method.
The push() method adds new items to the end of an array, and returns the new length.
Note: The new item(s) will be added at the end of the array.
Note: This method changes the length of the array.
Please read more here http://www.w3schools./jsref/jsref_push.asp
UPDATE:
If you want to be able to constraint the max length of the array, the simplest way would be with a constant variable setting holding the length of the array and checking this value with the array's length. If you still wanna throw an exception when/if the index is greater than this max value, then you can do it by throwing your exception like this throw 'your exception message'
Javascript Array push would increase the length anyway.
Eg. if you declare
var arr = [];
andarr.length = 2
then push somethingarr.push("4")
would give the final length as 3 which adds to your initial length. The only way to check if the array exceeds length is by traversing the array and paring if index is greater than length of the array. The other way is to prevent the push by validating through a variable with predefined length.You can prevent the push by paring it with the predefined length and in else raise your custom error like this
throw new Error("outOfBoundException")
Learn more about custom exception in javascript here Custom Exceptions in JavaScript
Regarding your ment: You can just store the length in some variable, e.g.
var data = []; var length = 5; // user defined length for(var i = 0; i < length; i++) { data.push(createSomeObject()); }
In your case your doing the same as above but your not looping through the length.
- You might also think of using
new Array()
like below
new Array(4);
creates an empty array of length 4. But
new Array('4');
creates an array containing the value '4'.
But not suggested as jsLint does not like new Array()
because the constructer is ambiguous.
本文标签: htmlArray index doesn39t go out of bound in JavascriptStack Overflow
版权声明:本文标题:html - Array index doesn't go out of bound in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745176093a2646246.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论