When calling my function checkIss(), issFullArray.indexOf(issToCheck) always returns undefined. I've run a .length, output the contents of issFullArray, I can't figure out why it's not working- the array looks fine to me. As you can see below, I've tried explicitly setting issArray as an array and copying the array returned by my getIssList()
function updateIss() {
var issArray = [];
var currService = current.u_business_service;
var currIss = current.u_is_service;
issArray = getIssList(currService).slice(); //getIssList() returns an arry
if (checkIss(issArray, currIss) === false) {
//do stuff
}
}
function checkIss(issFullArray, issToCheck) {
if (issFullArray.indexOf(issToCheck) < 0) {
return false;
} else {
return true;
}
}
Easiest to just loop through the array and compare each value and return true if there is a match otherwise return false. Not much more code and works for all browsers.
function checkIss(issFullArray, issToCheck) {
for(i=0; i<issFullArray.length; i++) {
if(issFullArray[i]==issToCheck) {
return true;
}
}
return false;
}
Related
The 1st set of code works fine and assigns non-empty objects to the var, but the 2nd code with only log the right data but will not return the data. The var just remains undefined.
Any idea on where I am going wrong? Thanks!
var filteredEmpty1 = json_data.children.filter(function(value, index, arr) {
if (value.children.length != 0) {
return value//Returns what I need
} else {
console.log("EMPTY")
};
});
json_data = filteredEmpty1;
json_data = {
"name": "RVs",
"children": json_data
};
var filteredEmpty2 = json_data.children.forEach(function(value) {
value.children.filter(function(e) {
if (e.children.length != 0) {
console.log(e)//Logs what I need to return
return(e)//Returns undefined
} else {
console.log("EMPTY")
};
});
})
Use Map instead of forEach because Map returns a value. and Filter function always returns boolean.
var filteredEmpty2 = json_data.children.map(function(value) {
return value.children.filter(function(e) {
return e.children.length != 0;
});
})
TL;DR Need help on making this script work
Hi, I've been trying to get this script working with an array structure but it doesn't work and I keep getting the error in_Array not defined. If possible can anyone point out the mistakes I am making even if it minimal. Thanks for your time -Simon
<script>
$traffictick = Array("OFF","ON","OFF");
function tick() {
if (in_Array("ON", $traffictick[1])) {
yellowon();
console.log("Yellow");
$traffictick = Array("OFF,OFF,ON;");
} else if (in_Array("ON", $traffictick[2])) {
redon();
console.log("Red");
$traffictick = Array("OFF,ON,ON;");
} else if (in_Array("ON", $traffictick[2])) {
yellowon();
console.log("Red & Yellow");
$traffictick = Array("ON,OFF,OFF;");
} else if (in_Array("ON", $traffictick[1] & $trafficktick[2])) {
greenon();
console.log("Green");
$traffictick = Array("OFF,ON,OFF;");
}
}
</script>
there is no in_array function in javascript, you have to check manually that your value exist in array or not,
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
Use this function instead of in_array and also you are passing value as a second parameter, you must pass an array.
I have a button that I want to hide when the array $scope.game.players.players contains specific value.
button(ng-click="", ng-hide="ImPlaying()") Play
The function ImPlaying() checks the condition and return a boolean
$scope.ImPlaying = function(){
$scope.game.players.playerExist($scope.user.socketID, function(exist){
console.log(exist);
return exist;
});
}
exist change value but the button is always shown
But when I replace the function $scope.game.players.playerExist() by its code everything works as expected.
$scope.ImPlaying = function(){
for (var i = 0; i < $scope.game.players.players.length; i++) {
if($scope.game.players.players[i]){
if($scope.game.players.players[i].socketID == $scope.user.socketID){
return true;
}
}
};
return false;
}
What's wrong with the first function ?
You're missing return statement in ImPlaying function
$scope.ImPlaying = function(){
return $scope.game.players.playerExist($scope.user.socketID, function(exist){
console.log(exist);
return exist;
});
}
I made a function which validates a form and works fine but I now want to break it down into 3 separate functions.
I now have a function which is called by the form being submitted which declares some arrays and runs the three functions. When it was all one big function the various if statements that found errors would return false; which would then go back to the form and stop it sending.
However now that I've got functions within a function I can't figure out how to get that message 'false' back to the form.
Below is the function called by the form submit button followed by the main function it calls.
I tried creating an empty variable which is returned instead of false which is then is assigned the value false by the validateSignup function but it didn't work.
function validateSignup()
{
// Declaring Arrays (deleted array contents)
var errorSpansArray=[whatever];
var regexErrorArray=[whatever];
var regexArray=[whatever];
validateText(0,6,errorSpansArray, regexErrorArray, regexArray);
passMatch();
genderCountryCheck()
}
function validateText(formNumber, numberElements, errorSpansArrayName, regexErrorArrayName, regexArrayName)
{
for (x = 0; x<numberElements; x++)
{
var spanName = errorSpansArrayName[x];
var textError = document.getElementById(spanName);
var y=document.forms[formNumber].elements[x];
if (!y.value)
{
errorMessage(0,spanName,x);
return false;
}
if(!regexArrayName[x].test(y.value)){
textError.innerHTML = regexErrorArrayName[x];
return false;
}
}
UPDATE:
Thanks for your responses. I have found a solution that seems to work for me.
function validateSignup()
{
// Declaring Arrays (deleted array contents)
var errorSpansArray=[whatever];
var regexErrorArray=[whatever];
var regexArray=[whatever];
var returnValidateText=validateText(0,6,errorSpansArray, regexErrorArray, regexArray);
var returnPassMatch = passMatch();
var returnGenderCountry = genderCountryCheck();
if (returnValidateText || returnPassMatch || returnGenderCountry === false)
{
return false;
}
else{
return true;
}
}
If you call the function it returns a value
var formIsValid = function validateText(....)
should do the trick.
function validateSignup()
{
// Declaring Arrays (deleted array contents)
var errorSpansArray=[whatever];
var regexErrorArray=[whatever];
var regexArray=[whatever];
var formIsValid = false;
formIsValid = validateText(0,6,errorSpansArray, regexErrorArray, regexArray);
formIsValid = passMatch();
formIsValid = genderCountryCheck()
}
One way is to just check the individual function returns directly and return based on that
if (!validateText(0,6,errorSpansArray, regexErrorArray, regexArray)) {
return false;
}
if (!passMatch()) {
return false;
}
if (!genderCountryCheck()) {
return false;
}
Although it's shorter to use a single conditional
return
validateText(0,6,errorSpansArray, regexErrorArray, regexArray) &&
passMatch() &&
genderCountryCheck();
In javascript return false means false will be returned as value where the method is called. So you need something like
If(validateText()){
return true;
}
And similarly rest of the code.
Is there any ways to short-up such chain call?
if (obj && obj.prop && obj.prop.subProp1 && obj.prop.subProp1.subPropFunc) {
obj.prop.subProp1.subPropFunc();
}
The only alternative I can imagine is try-catch. Any other ideas?
*I really tired of writing these. It's much easier in coffeescript using ?..
This should work given your sample code (haven't tested "all cases", just a copy of your sample):
function propsExist(obj) {
if (!obj) return false;
for (var i = 1; i < arguments.length; i++) {
if (!obj[arguments[i]]) return false;
obj = obj[arguments[i]];
}
return true;
}
if (propsExist(obj, "prop", "subProp1", "subPropFunc")) {
obj.prop.subProp1.subPropFunc();
}
The method propsExist() takes a variable number of arguments, the first of which being the original object you want to check properties/functions on. It will iterate through the list of properties you send to it and check them in-order. If one doesn't exist, it will return false. If it makes it through the whole loop, it validated successfully!
If you always want to call the sub-property's function if it validates, you could also just change the propsExist function to call it instead of returning true (then rename the function to something like callIfValid(obj, ...)
Same idea as the previous post, just a different solution.
function checkChain(variablePath,startingPoint){
var check = startingPoint || window,
parts = variablePath.split("."),
i;
for (i=0;i<parts.length;i++) {
check = check[parts[i]];
if (!check) {
return null;
}
}
return check;
}
var foo = { bar : { cat : { says : function(x){ alert(x); } } } };
var test1 = checkChain("foo.bar.cat.says");
if (test1) {
test1("meow");
}
var test2 = checkChain("foo.bar.cat.bark");
if (test2) {
test2("burp");
}
var test3 = checkChain("cat.says",foo.bar);
if (test3) {
test3("huh?");
}