for (let i = 0 ; i<user.length;i++)
{
if (this.username === user[i].username && this.password === user[i].password){
this.title="Successfully login your account";
this.router.navigate(['/home']);
return;
} else {
this.title = "Please check your username and password (or) create New Account in Register";
return;
}
}
return user[i];
}
your else statement should not be in the loop. you else statement will execute as soon as you hit the first user. set a userVerifiedFlg = false; before the loop, and then check to see if it is still false after checking against all users.
Related
How can I toggle between false/true a variable's value with a command
my code
var tagdetect = true;
const command = args.shift().toLowerCase();
if (command === 'tagdetect'){
if (args[1] === 'true'){
}
}
if (tagdetect){
if (message.mentions.users.has('id')){
client.TagİDAlgılayıcı.get('nick').execute(message, args);
}
i am using a command handler, i want to do something like {prefix}tagdetect true/false
Assuming tagdetect is the variable you want to switch, you can use two if statements to switch the boolean's state based on user input.
if (args[1] === 'true'){
tagdetect = true;
} else if (args[1] === 'false'){
tagdetect = false;
} else {
// User has given an invalid setting, let them know by sending an error message.
}
The comment is just there to let you know what you should add.
I'm having trouble updating this code from angularjs ad angular 2, thank you for any help
if (settings != null && settings.checkSubscriptionForVip === true) {
return this.user.hasVipAccess().then(function(hasVipAccess) {
hasVipAccess ? deferred.resolve() : deferred.reject("NO_VIP");
return;
});
} else {
deferred.resolve();
return;
}
You neeed to have a boolean variable defined in your component and assign the result in to that,
hasVipAccess : boolean = false;
if (settings != null && settings.checkSubscriptionForVip === true) {
this.user.hasVipAccess().then(function(access) {
this.hasVipAccess = access;
});
} else {
this.hasVipAccess = access;
}
However if you do not need to set boolean variable, just return the result
I have written some code in the AngularJS controller.
The scene is as follow, I have a form which accepts all the details of the Organization including its name, its admin etc. I have written manual validations to validate the data. I have written a separate function to validate the data. Which validates all the data and depending upon the validations function return "true/false", I want to check the returned value and if the value is 'true', I want to make a call to service to enter the data into the database, BUT, If the result is false I am not supposed to call the service and show the proper alert message. BUT THE PROBLEM IS, The place from where I am calling validation function, it does not wait for function to return the value, instead It gets executed first and then the value is returned to the calling function, as a result it always shows the status as "undefined".
Validation function is as follows:
$scope.validateOrganizationForm = function(){
var phoneNumber = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if($scope.organization.orgName == "" || $scope.organization.orgName.length > 16){
$scope.failureAlertMessage("Please enter valid organization name of length between 1 to 16");
return false;
}else if($scope.organization.orgAddress == "" || $scope.organization.orgAddress.length > 512){
$scope.failureAlertMessage("Please enter valid organization address of length between 1 to 512");
return false;
}else if($scope.organization.orgPhoneNumber == "" || $scope.organization.orgPhoneNumber.length>13 || (!($scope.organization.orgPhoneNumber.match(phoneNumber)))){
$scope.failureAlertMessage("Please enter valid organization phone number of length between 10 to 13");
return false;
}else if($scope.organization.orgAlternatePhoneNumber != "" && ($scope.organization.orgAlternatePhoneNumber.length>13 || (!($scope.organization.orgAlternatePhoneNumber.match(phoneNumber)))) ){
$scope.failureAlertMessage("Please enter valid organization alternate phone number of length between 10 to 13");
return false;
}else if($scope.organization.orgContactPersonName == "" || $scope.organization.orgContactPersonName.length > 64){
$scope.failureAlertMessage("Please enter valid organization contact person name of length between 1 to 64");
return false;
}else if($scope.organization.orgContactPersonPhoneNumber == "" || (!($scope.organization.orgContactPersonPhoneNumber.match(phoneNumber)))){
$scope.failureAlertMessage("Please enter valid organization contact person phone number of length between 10 to 13");
return false;
}else if($scope.organization.orgContactPersonEmailId == "" || $scope.organization.orgContactPersonEmailId == undefined || $scope.organization.orgContactPersonEmailId.length > 64){
$scope.failureAlertMessage("Please enter valid organization contact person email id of length between 1 to 64");
return false;
}else if($scope.organization.orgDescription == "" || $scope.organization.orgDescription.length > 256){
$scope.failureAlertMessage("Please enter valid organization description of length between 1 to 256");
return false;
}else if($scope.organization.userVo.username == "" || $scope.organization.userVo.username.length > 64){
$scope.failureAlertMessage("Please enter valid organization admin username of length between 1 to 64");
return false;
}else if($scope.organization.userVo.emailId == "" || $scope.organization.userVo.emailId.length > 128){
$scope.failureAlertMessage("Please enter valid organization admin email Id of length between 1 to 128");
return false;
}else{
console.log("All the fields are validated except validating duplications, lets check it now");
$scope.checkOrgName().then(
function(orgNameStatus){
console.log("can we use this org name :"+orgNameStatus);
if(orgNameStatus==false){
console.log("Organziation name is already present in the database");
return false;
}else if(orgNameStatus){
$scope.isOrgAdminUsernameAvailable = $scope.checkOrgAdminUsername().then(
function(orgAdminUsernameStatus){
console.log("Can we use this username for admin: "+orgAdminUsernameStatus);
if(orgAdminUsernameStatus == false){
console.log("org admin username is already present in the database");
return false;
}else if(orgAdminUsernameStatus){
$scope.isAdminEmailIdAvailable = $scope.checkOrgAdminEmailId().then(
function(orgAdminemailIdStatus){
console.log("Can we use this admin email id: "+orgAdminemailIdStatus);
if(orgAdminemailIdStatus == false){
console.log("Org admin email id is already present in the database");
return false;
}else if(orgAdminemailIdStatus){
console.log("All the three fields(orgName,orgAdminUsername,orgAdminEmailId) are checked for duplication, everything is validated sucessfully");
return true;
}
});
}
});
}
});
}
return true;
}
And here is the calling function:
$scope.addOrganization = function(){
console.log("Command in add Org");
var validationStatus = $scope.validateOrganizationForm(); (Here I am calling validation function)
console.log("Add form validation status: "+validationStatus); (This always gives me undefined...)
if(validationStatus){
console.log("Form validated successfully, We can add organization safely");
OrganizationService.addOrganization($scope.organization).then(
function(data){
$scope.successAlertMessage("Organition Added successfully");
$scope.resetAddOrgForm();
},
function(errResponse){
$scope.successAlertMessage("There was an error adding organization");
}
);
}else{
console.log("Add form validation failed");
}
}
Can I use .then() for this, I tried that too, but it says I am not returning any promise instead I am just returning a plain boolean value, How can I send the promise if I have to use .then()..??
// Check voted
var votes = res.data.votes;
if(votes.length == 0){$scope.like = true;}
votes.forEach(function(vote){
if(vote.userId === auth.profile.user_id) {
$scope.liked = true;
} else {
$scope.like = true;
}
});
I have written a code to check if the user has voted or not, but I'm having a small issue with the else statement:
using the following code, the $scope.liked works correctly, but the else statement only goes for the first.
How can I edit this, so he goes through all the votes, and if nothing is found, he displays the $scope.like
What about just do it outside the loop?
votes.forEach(function(vote){
if(vote.userId === auth.profile.user_id) {
$scope.liked = true;
}
});
$scope.like = !$scope.liked;
I need some help. I have 2 loops to traverse two lists of records . The thing is that at some point should put a break or something, because the last loop crushes the value I'm looking for.
This is my code
for (var i in $scope.users) {
for (var j in $scope.states) {
if ($scope.users[i].id === $scope.states[j].user) {
if ($scope.states[j].estado === 'totallyBussy') {
$scope.users[i].estado= 'Not available';
} else if ($scope.states[j].estado === 'partlyBussy') {
$scope.users[i].estado= 'Maybe available';
}
}
else {
$scope.users[i].estado= 'Available';
}
}
}
Where user 4 and user 5 should be 'Maybe available' and 'Not available', but with this code, I'm getting 'Available for user 0, 1, 2, 3, and the last one. And the last one is crushes with else.
I hope I explained well.
Thanks so much
You can use the break keyword, which will end the loop when executed.
One side note: you don't want to use for (var i in array) {} since you may iterate through Array properties (and have i take unexpected values).
I updated your code with a more reliable iteration.
for (var i = 0, il = $scope.users.length; i < il; i++) {
var user = $scope.users[i];
for (var j = 0, jl = $scope.states.length; j < jl; j ++) {
var state = $scope.states[j];
if (user.id === state.user) {
if (state.estado === 'totallyBussy') {
user.estado = 'Not available';
} else if (state.estado === 'partlyBussy') {
user.estado = 'Maybe available';
}
// Go to the next user (break the "states" loop)
break;
} else {
user.estado = 'Available';
}
}
}
EDIT: If $scope.users and $scope.states are not Arrays but actually Objects (which would seem strange to me but anyway), you can keep for (var i in object) {} but you have to add a check: if (object.hasOwnProperty(i)) {} in the loop.
you can add a flag which will become true when you got your desired value.
something like
var gotValue = false;
for (var i in $scope.users) {
if(!gotValue)
{
for (var j in $scope.states) {
if ($scope.users[i].id === $scope.states[j].user) {
if ($scope.states[j].estado === 'totallyBussy') {
$scope.users[i].estado= 'Not available';
gotValue = true;
} else if ($scope.states[j].estado === 'partlyBussy') {
$scope.users[i].estado= 'Maybe available';
gotValue = true;
}
}
else {
$scope.users[i].estado= 'Available';
gotValue = true;
}
}
}
}
In AngularJS, there is no break for loops,etc., so one has to go with logic manually.
A similar case has been demonstrated here.
Here, I have created a variable f, which has been set to false initially. On forEach loop, based on a condition, it is made to true, further which it works as break.
Please find the code below:
HTML:
<div ng-app="app" ng-controller="test"></div>
JS:
var app = angular.module('app',[]);
app.controller ('test', function ($scope){
var f = false;
angular.forEach([1,2,3,4,5], function(v,k){
if(k > 2){
f = true;
}
if(!f) {
document.write('value = ' + v + ' <br>');
}
});
});