I am making a function that returns true if number in outerfucntion is within innerfunction list
<script>
function hasMatch(item) {
hasMatch(2)
function inList() {
var List = [1,2,3,4];
for (i = 0; i<List.length; i++){
if (list[i] == item) {
return true;
} else {
return false;
}
}
}
inList();
}
hasMatch();
</script>
I get a "Max Stack exceeded error", how would I fix this?
hasMatch(2) is recursive call without any terminating condition.
hasMatch() is getting called infinitely and that's why you see stack exceeded error.
function hasMatch(item) {
function inList() {
var List = [1,2,3,4];
for (i = 0; i<List.length; i++){
if (List[i] == item) {
return true;
} else {
return false;
}
}
}
return inList();
}
hasMatch(2);
Related
there is no output to the code even tho it is being called at the bottom. How can i fix this?
<script>
function hasMatch(item) {
function inList() {
var List = [1,2,3,4];
for (i = 0; i<List.length; i++){
if (List[i] = item) {
return true;
} else {
return false;
}
}
}
inList();
}
hasMatch(2);
hasMatch();
</script>
You need to return the output of inList
function hasMatch(item) {
function inList() {
var List = [1,2,3,4];
for (i = 0; i<List.length; i++){
if (List[i] = item) {
return true;
} else {
return false;
}
}
}
return inList();
}
console.log(hasMatch(2));
console.log(hasMatch())
Try to do like this:
function hasMatch(item) {
function inList() {
var List = [1,2,3,4];
for (var i = 0; i<List.length; i++){
if (List[i] == item) {
return true;
}
}
return false;
}
return inList(item);
}
console.log (hasMatch(2));
I am try to call a nesting function like below:
function public(val) {
if (val == "exist") {
function runList() {
for (var i = 0; i < arguments.length; i++) {
arguments[i]();
}
}
}
public.runList = runList;
}
var publicExist = "exist";
function test() {
console.log(test)
}
function testE() {
console.log(testE)
}
public(publicExist);
public.runList(test, testE);
I want to fire function, but these codes only show the text, how can I do?
You forgot to put quotes around 'test' and 'testE', which I am assuming you wanted to log to the console as strings.
function public(val) {
if (val == "exist") {
function runList() {
for (var i = 0; i < arguments.length; i++) {
arguments[i]();
}
}
}
public.runList = runList;
}
var publicExist = "exist";
function test() {
console.log('test')
}
function testE() {
console.log('testE')
}
public(publicExist);
public.runList(test, testE);
Just add quotes in test and testE method's console -
function public(val) {
if (val == "exist") {
function runList() {
for (var i = 0; i < arguments.length; i++) {
arguments[i]();
}
}
}
public.runList = runList;
}
var publicExist = "exist";
function test(){
console.log('test')
}
function testE(){
console.log('testE')
}
public(publicExist);
public.runList(test,testE);
I have a for-loop that is terminating without finishing the loop. It seems to be related to whether or not a call to another function is made within the loop.
this.cycle = function() {
var list = this.getBreaches("Uncontained");
if (list.length > 0) {
for (i=0; i < list.length; i++) {
this.saveVsRupture(DC=11, i); //calls this.rupture() if save failed
}}
return 1;
};
this.saveVsRupture() calls a function that rolls a d20 and checks the result. If the save fails, it calls a method called this.rupture() that does some adjusting to this.
Problem
If the saving throw is successful, the loop continues, but if the saving throw fails, it runs the this.rupture() function and then breaks the for-loop. It should keep running the for-loop.
Why is this happening?
Additional Details
Here are the other functions...
savingThrow = function(DC=11) {
// DC = Difficulty Check on a d20
try {
if (0 <= DC) {
var roll = Math.floor((Math.random() * 20))+1; //roll d20
var msg = "(Rolled "+roll+" vs DC "+DC+")";
console.log(msg);
if (roll >= DC) { //Saved
return true;
}
else { //Failed save
return false;
}
}
}
catch(e) {
console.log("Exception in savingThrow: "+e);
};
};
this.saveVsRupture = function(DC=1, i=null) {
try {
if (!savingThrow(DC)) {
this.rupture(i);
return false;
}
return true;
}
catch(e) {
console.log(e);
}
};
this.rupture = function(i=null) {
if (i == null) {
i = range(1,this.damageList.length).sample();
}
var hole = this.damageList[i];
var dmg = range(1,this.harmonics()).sample();
hole.rupture(dmg);
msg = "*ALERT* " + hole + " expanded by " + dmg + "% Hull Integrity #"+this.hullIntegrity();
this.log(msg);
if (hole.size % 10 == 0) {
this.health -= 25;
msg = "The ship creaks ominously.";
this.log(msg);
}
return 1;
};
The correct syntax for the for-loop declares the counter variable.
for (var i=0; i < list.length; i++) {etc..
/// Causes the For-Loop to exit prematurely...
for (i=0; i < list.length; i++) {etc..
Once the "var i=0" is used, the for-loop operates as expected.
consider implementing a try-catch in your for loop, with your saveVsRupture function within the try. This implementation will catch errors in your function but allow the program to keep running.
Change the saveVsRupture function like this:
function saveVsRupture(a,b) {
try{
//your saveVsRupture code here...
}
catch(e){}
}
Your should catch problems that occurred in your code with try,catch block to prevent throw them to top level of your code (the browser in this example) .
Don't use return in for loop!
Change your code as following:
this.cycle = function() {
var list = this.getBreaches("Uncontained");
if (list.length > 0) {
for (i=0; i < list.length; i++) {
var temp = this.saveVsRupture(DC=11, i); //calls this.rupture() if save failed
console.log(temp);
}}
return 1;
};
I have a for loop in a function in the structure
func(var, callback) {
for(i = 0; i < len; i++) {
validate(var, function(value) {
if (!value) { callback(value) }
}
}
callback(true);
}
Where the function validate returns a boolean. I would only like to call my callback with true if it has not been called before. I tried putting a return after callback(value) but that didn't help.
Set a flag:
function func(foo, callback) {
var called = false;
for(var i = 0; i < len; i++) {
validate(foo, function(value) {
if (!value) {
called = true;
callback(value);
}
})
}
if (!called) {
callback(true);
}
}
I'm working on a Chrome extension an I've hit a wall.
function isInQueue(id) {
chrome.extension.sendRequest({getQueueItems: 1}, function(response) {
var items = response.items;
if (items) {
for (var i = 0; i < items.length; i++) {
if ((items[i].id == id) == true) return true;
}
return false;
} else { return false; }
});
}
The request returns 'items' which is an array of objects. I am trying to see if another item outside of the queue already exists inside the queue. For example, there is an item on the outside with an id equal to '198677'. I know I already have an exact copy of that same item in my queue with the exact same id, '198677', however, when I test the two for equality (items[i].id == id) == true, it returns false. I have checked the typeof both and they are both strings. I have tried using === and that hasn't worked. I tried adding zero to each of them to turn them into integers and that made the function return true when it was actually true, however, when I tested for true if (isInQueue(id) == true) the conditional returned false.
This is all very confusing and frustrating for me. They're both strings, why doesn't it work?
Help is very much appreciated.
The problem is chrome.extension.sendRequest is asynchronous - it returns immediately, but the callback function you provide to it will only be called once the request has completed.
The usual way to handle something like this is to pass a callback to your isInQueue method; the callback is called when the asynch operation is completed with the result.
function isInQueue(id, callback) {
chrome.extension.sendRequest({getQueueItems: 1}, function(response) {
var result = false;
var items = response.items;
if (items) {
for (var i = 0; i < items.length; i++) {
if ((items[i].id == id) == true) {
result = true;
break;
}
}
}
callback(result);
});
}
I figured it out:
function isInQueue(id) {
var result = false;
var queue = localStorage["queue_rss"];
if (queue != undefined) {
var items = JSON.parse(queue).items;
if (items) {
for (var i = 0; i < items.length; i++) {
if ((items[i].id == id) == true) {
result = true;
break;
}
}
}
}
return result;
}
I should have done it that way in the first place.
Thanks guys. :D