Here is my code:
<script>
function monthassign()
{
document.getElementById("month").selectedIndex=0;
}
function isleap()
{
var yr=document.getElementById("year").value;
if ((parseInt(yr)%4) == 0)
{
if (parseInt(yr)%100 == 0)
{
if (parseInt(yr)%400 != 0)
{
//alert("Not Leap");
return "false";
}
if (parseInt(yr)%400 == 0)
{
//alert("Leap");
return "true";
}
}
if (parseInt(yr)%100 != 0)
{
//alert("Leap");
return "true";
}
}
if ((parseInt(yr)%4) != 0)
{
//alert("Not Leap");
return "false";
}
}
function dateassign()
{
var yr=isleap();
var mth=parseInt(document.getElementById("month").selectedIndex);
var dt=document.getElementById("date")
if(yr)
{
if(mth==2)
{
//alert(yr);
dt.options.length = 0;
for(i=1; i<30; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
}
if(yr==false && mth==2)
{
//alert("Second fun");
dt.options.length = 0;
for(i=1; i<29; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
if(mth==4 || mth==6 || mth==9 || mth==11)
{
dt.options.length = 0;
for(i=1; i<31; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
else
{
dt.options.length = 0;
for(i=1; i<32; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
}
</script>
My problem is when the variable yr contains false value the first if condition gets executed in function dateassign(). When the yr contains false value it is expected to shift the program control to the code block if(yr==false && mth==2), but it's not happening. I'm fed up of this problem of execution of specific condition even if the condition is false and why the control is not going inside a specific if condition. Please help me out of this issue. Thanks in Advance.
true and false are not the same thing as "true" and "false". The first is a Boolean, but the second is a string. You should be returning Boolean values, so you'll need to replace each instance of "true" with true and "false" with false.
Related
Even after I give the conditioned truth, the answer here always looks false.
var AlloverName = ['Iqbal', 'Saidul', 'Ali Akbar']
var isOkay = false
function detailsAllOfYou(activity) {
for (i = 0; i <= activity.length; i++) {
if (activity[i] === 'Saidul') {
console.log("Name is found")
isOkay = true;
break
}
if (!isOkay) {
console.log("Name is not found")
break
}
}
}
detailsAllOfYou(AlloverName)
You need to change your logic, because the array sees the first element isnt equal then breaks, so change it to
var AlloverName = ['Iqbal', 'Saidul', 'Ali Akbar']
var isOkay = false
function detailsAllOfYou(activity) {
for (let i = 0; i <= activity.length; i++) {
if (activity[i] === 'Saidul') {
console.log("Name is found")
isOkay = true;
break
}
}
if (!isOkay) {
console.log("Name is not found")
}
}
detailsAllOfYou(AlloverName)
My code will only go through to the first if statement where it checks the value of key for headline1 etc... The first if statement works properly but it won't work with any of the following if statements when the first one isn't true. I've switched the second statement to the first where it checks for 'desc1' and then it works for that one only.
The purpose of this function is to check each key of an object and return the key when its value is over a certain length so I can add a class and show user some warning. This is in Vue JS so ads is in data and characterCheck is in computed property.
ads: [
{
headline1: '_keyword_',
headline2: 'Online',
headline3: 'Free',
desc1: 'Buy online _keyword_',
desc2: ' Vast collection of _keyword_',
finalurl: 'www.books.com',
path1: '',
path2: '',
boolean: true
}
]
characterCheck () {
for(var x = 0; x < this.ads.length; x++){
if(this.ads[x]['boolean'] == true) {
for(var key in this.ads[x]){
var length = this.ads[x][key].replace(/_keyword_/g, this.activeKeyword).length
if( key === 'headline1' || key === 'headline2' || key === 'headline3'){
if(length > 30){
return key
}
} else if( key == 'desc1' || key == 'desc2'){
if(length > 90){
return key
}
} else if( key == 'path1' || key == 'path2'){
if(length > 15){
return key
}
} else {
return false
}
}
}
}
}
When your first nested if condition fails, the code goes to next subsequent else-if. For some particular value, all the if and else-if block fails and code lands on final else block which contains a return statement.
If your code reaches even once there, the entire function execution immediately stops and false value is returned.
Since, you wish to wait as long as you have not looped through all the values, remove the else part and add a simple return statement to the end of the for loop like this:
function characterCheck () {
for(var x = 0; x < this.ads.length; x++) {
if(this.ads[x]['boolean'] == true) {
for(var key in this.ads[x]) {
var length = this.ads[x][key].replace(/_keyword_/g, this.activeKeyword).length
if( key === 'headline1' || key === 'headline2' || key === 'headline3') {
if(length > 30) {
return key
}
}
else if( key == 'desc1' || key == 'desc2') {
if(length > 90) {
return key
}
} else if( key == 'path1' || key == 'path2') {
if(length > 15) {
return key
}
}
}
}
}
return false
}
I have some problem when I check the function validation, I need when checking all the cassis is true hide the parent div * errors message *
var error_pass = false;
$('#pass').focusout(function(){
check_pass();
error_pass = false;
if(error_pass !== true){
console.log('its showing!');
}else{
$('.test').fadeOut('522');
}
});
function check_pass() {
var fpass= $('#pass').val();
switch(error_pass = true){
case(fpass.length < 6 ? $('#pass-error-message3').css('color','red'):$('#pass-error-message3').css('color','green') ):
$('#pass-error-message3').show();
case(fpass.search(/(?=.[a-z])(?=.*[A-Z])/) == -1 ? $('#pass-error-message4').css('color','red') : $('#pass-error-message4').css('color','green')):
$('#pass-error-message4').show();
case(fpass.search(/\d/) == -1 ? $('#pass-error-message2').css('color','red'):$('#pass-error-message2').css('color','green')):
$('#pass-error-message2').show();
default:break;
}
}
Use if else statements like this
function validation() {
var error = false;
if (fpass.length < 6) {
error = true;
$('#pass-error-message3').css('color', 'red').show();
} else {
$('#pass-error-message3').css('color', 'green');
}
if (fpass.search(/(?=.[a-z])(?=.*[A-Z])/) == -1) {
error = true;
$('#pass-error-message4').css('color', 'red').show();
} else {
$('#pass-error-message4').css('color', 'green')
}
if(fpass.search(/\d/) == -1){
error = true;
$('#pass-error-message2').css('color','red').show();
}else{
$('#pass-error-message2').css('color','green');
}
if(error === false){
hideParentDiv(); // Here hide the div
}
}
Much cleaner approach
I'm comparing a variable with an array: $scope.object.id and $scope.groepen.id with an if statement after using a for loop. If $scope.object.id is exactly the same as one of the IDs of $scope.groepen.id, then it should make that index of $scope.overlap true.
I'm using another if check to see if anything of $scope.overlap is true. If one element of $scope.overlap is true, it will make $scope.bestaand true. Else it should make it false.
for (var i = 0; i < $scope.groepen.length; i++) {
if ($scope.object.id === $scope.groepen[i].id) {
$scope.overlap[i] = true;
if ($scope.overlap[i]) {
$scope.bestaand = true;
}
else {
$scope.bestaand = false;
}
}
else {
$scope.overlap[i] = false;
}
}
My console log shows me that $scope.overlap does in fact show the correct values (so if nothing is the same, all indexes are false). $scope.bestaand does turn to true if something is the same, but it doesn't change back to false.
I'm using Angular form validation to show if the check is working or not shown here:
<div class="col-md-3" ng-class="{ 'has-error' : bestaand }">
<label class="control-label" for="textinput">Groepsnaam</label>
<input id="groepen" name="groepen" type="text" class="form-control input-md" ng-model="object.id" ng-minlength="4" ng-maxlength="16" ng-change="checkOverlap()" required>
<p ng-show="bestaand" class="help-block">Deze groepsnaam bestaat al!</p>
</div>
What am I doing wrong here?
Edit:
I have changed the place of my if statements. Updated code shown here:
for (var i = 0; i < $scope.groepen.length; i++) {
if ($scope.object.id === $scope.groepen[i].id) {
$scope.overlap[i] = true;
}
else {
$scope.overlap[i] = false;
}
if ($scope.overlap[i]) {
$scope.bestaand = true;
console.log("works")
}
else {
$scope.bestaand = false;
console.log("doesnt work")
}
}
The console log shows me this:
It seems that it does become true, but it gets overwritten (I have input a value that is the same as the second value of the array). If I input a value that is the same as the LAST value of the array, it does work.
Your problem is that you've enclosed if ($scope.overlap[i]) { inside if ($scope.object.id === $scope.groepen[i].id) { so $scope.overlap will always be true. This means $scope.bestaand will only ever be set to true or left as undefined. What you actually want is -
for (var i = 0; i < $scope.groepen.length; i++) {
if ($scope.object.id === $scope.groepen[i].id) {
$scope.overlap[i] = true;
}
else {
$scope.overlap[i] = false;
}
if ($scope.overlap[i]) {
$scope.bestaand = true;
}
else {
$scope.bestaand = false;
}
}
Edit
If you want to set $scope.bestaand to true if any of your $scope.overlap elements is true then you will want something a little different -
$scope.bestaand = false;
for (var i = 0; i < $scope.groepen.length; i++) {
if ($scope.object.id === $scope.groepen[i].id) {
$scope.overlap[i] = true;
}
else {
$scope.overlap[i] = false;
}
if(!$scope.bestaand) {
if ($scope.overlap[i]) {
$scope.bestaand = true;
}
}
}
This is because else statement is unreachable:
$scope.overlap[i] = true;
if ($scope.overlap[i]) {
$scope.bestaand = true;
}
else {
console.log('UNREACHABLE');
$scope.bestaand = false;
}
As for your edited question:
Your $scope.bestaand indicate an error, so I assume that if it be false once it should never be true.
So you need to rewrite your loop as follows:
$scope.bestaand = false;
for (var i = 0; i < $scope.groepen.length; i++) {
if ($scope.object.id === $scope.groepen[i].id) {
$scope.overlap[i] = true;
} else {
$scope.overlap[i] = false;
}
if ($scope.overlap[i] && !$scope.bestaand) {
$scope.bestaand = true;
}
}
because there is no case you set it to false.
if ($scope.object.id === $scope.groepen[i].id) is true, then you already set it overlap = true, if overlap is false, you don't set bastaan = false in any case
These two Javascript functions are supposed to convert serial numbers (2-9999) for example into a number , but the functions below are not working for some reason .. they were originally written in PHP ... Works in PHP but not for Javascript.
<html>
<head>
<script type="text/javascript">
function my_isnum(str, negative=false, decimal=false)
{
var has_decimal = false;
var len = strlen(str);
if (len > 0) {
var valid = true;
for (var i=0; valid && i < len; i++) {
if (!(str[i] >= "0" && str[i] <= "9")) {
if (str[i] == "-") {
if (!negative || i != 0) {
valid = false;
}
} else if (str[i] == ".") {
if (!decimal || has_decimal) {
valid = false;
}
} else {
valid = false;
}
}
}
} else {
valid = false;
}
return valid;
}
function esn_to_num(esn) {
var tmp = [];
if ((tmp = esn.split("-")) {
if (tmp.length == 2
&& my_isnum(tmp[0])
&& my_isnum(tmp[1])
) {
esn = ((tmp[0] << 23) | tmp[1]);
} else {
esn = -1;
}
} else {
esn = -1;
}
return esn;
}
alert(2-9999);
</script> </head>
</html>
Original PHP functions
<?php
function my_isnum($str, $negative=false, $decimal=false)
{
$has_decimal = false;
$len = strlen($str);
if ($len > 0) {
$valid = true;
for ($i=0; $valid && $i<$len; $i++) {
if (!($str[$i] >= '0' && $str[$i] <= '9')) {
if ($str[$i] == '-') {
if (!$negative || $i != 0) {
$valid = false;
}
} else if ($str[$i] == '.') {
if (!$decimal || $has_decimal) {
$valid = false;
}
} else {
$valid = false;
}
}
}
} else {
$valid = false;
}
return $valid;
}
function esn_to_num($esn)
{
if (($tmp = explode('-', $esn))) {
if (sizeof($tmp) == 2
&& my_isnum($tmp[0])
&& my_isnum($tmp[1])
) {
$esn = (($tmp[0] << 23) | $tmp[1]);
} else {
$esn = -1;
}
} else {
$esn = -1;
}
return $esn;
}
?>
There is no such thing as strlen in Javascript. Use str.length instead.
Also, as Jason Sperske suggested below, change this:
function my_isnum(str, negative=false, decimal=false)
To this:
function my_isnum(str, negative, decimal)
{
if (typeof negative == "undefined") negative = false;
if (typeof decimal == "undefined") decimal = false;
....
}
These two javascript functions are supposed to convert serial numbers (2-9999) for example into a number.
Why not just get rid of the - and parse as a decimal number?
function padToFourDigits(_, digits) {
return "0000".substring(digits.length) + digits;
}
function serialToNum(serialNumStr) {
return +(serialNumStr.replace(/-(\d{1,4})/g, padToFourDigits));
}
Then
serialToNum('2-9999') === 29999
serialToNum('2-999') === 20999