Alert (!(car)) ; // false

! not
a=3;
b=2;
a>b = true
!(a>b) = !(true) = false
I want to write a function without using !
I want to write a function instead of !
How can I do this?
is this right?

<script type="text/javascript">

function not(bu){ 
if(bu == true){return false;}
if(bu == false){return true;}
return false;
}

var c = true;
alert( not(c) ); // false
alert (!(c)) ; // false


var car = "araba";
alert (!(car)) ; // false
alert( not(car) ); // false
alert (!!(car)) ; // true
alert(not(not(car))); // true

</script>

I’d say that’s easy enough to find out. ;-) The line

if(bu == false){return true;}

is redundant though. An even more concise way would be

function not(bu){ 
    return bu === false;
}

Haha cheers @James_Hibbard, x-edit here! :-)

You can make it more concise;

function not(bool){ 
 return bool === false;
}

console.log(not(true)); // false
console.log(not(false)); // true
console.log(not(3===3)); // false
console.log(not(1 > 2)); // true
etc ...

Ninja’d :slight_smile:

1 Like

Thanks

Actually, it just occurred to me that it is not. For one, you’d have to use the equality operator, not identity – as in your OP indeed. :-$ Also, you’d have to account for null / undefined and NaN. Thus the function should be

function not(bu) { 
    return bu == false || 
        bu == null ||
        isNaN(bu);
}

not('');        // returns true
not(null);      // returns true
not(undefined); // returns true
not(0/0);       // returns true

Thanks.
I tried your code.
alert(!(‘’)); // returns true
alert(not(‘’)); // return true
alert(!(“clean”));// false
alert(not(“clean”)); // true

alert(typeof ‘’);// string
alert(typeof ‘clean’); // string

I add this
function not(bu){
if(bu.length >0 && typeof bu==“string”){return false;}
…

alert(not(“clean”)); //false

Ah yes of course, a string isNaN also… didn’t think of that. Dealing with NaN is notoriously difficult; yes you cold check if typeof the value in question is a String instead, but then you’d have to check if it’s an Object, a Boolean etc. as well.

I think it would be more elegant to to take advantage of the fact that NaN !== NaN, so the result of every invalid Number operaration will be unequal to itself. Thus you could do this:

function not(bu) { 
    return bu == false || 
        bu == null ||
        bu !== bu;
}

not('');    // returns true
not('foo'); // returns false
not(0);     // returns true
not(1);     // returns false
not(0/0);   // returns true
not({});    // returns false

That’s more of a brain-teaser than I initially thought anyway. :-)

But that doesn’t get rid of the need for ! - if you are going to use ! inside the function you may as well just use ! and get rid of the function

1 Like

!#§$%&

function not(bu) { 
    return bu == false || 
        bu == null ||
        (bu === bu) === false;
}

:-DDD

2 Likes

I tried this

<script type="text/javascript">

//http://www.w3schools.com/js/js_type_conversion.asp
//http://www.w3schools.com/js/js_comparisons.asp
//http://www.w3schools.com/js/tryit.asp?filename=tryjs_comparison_or
//http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_isnan
//http://www.w3schools.com/jsref/jsref_isnan.asp


function not(bu) { 
    return bu == false || 
        bu == null ||
        (bu === bu) === false;
}

//alert(typeof 0/0); //  NaN
//alert(typeof null);// object
//alert(typeof undefined); // undefined



var A =[/a.?/,"","metin",3.14, NaN, 0/0,false, [1,2,3,4], {ad:"john", age:34}, new Date(), function(){}, null, undefined];
var t="";
for(var i=0; i<A.length; i++){

if(!(A[i]) == not(A[i])){ t +=  A[i]+":  "+!(A[i]) +"<span style='color:red'>=</span>"+ not(A[i])+"<br>"; }
if(!(A[i]) != not(A[i])){ t +=  A[i]+":  "+!(A[i]) +"!="+ not(A[i])+"<br>"; }
}

document.write(t);

// I can write

var a = ((4==5)==false ); // true
alert("a= "+a);
alert(((4==5)==false ) ); // true

//instead of

alert(4 != 5 ); // true


</script>

!(…) = not(…)

I can write
(4==5)==false
instead of
4 != 5

Thanks

get rid of the document.write call - that command has been obsolete for over 12 years.

console.log is better for debugging than alert as it doesn’t stop the script from continuing to run.

the entire discussion has been about how it is a lot more complicated than that - that’s why there are three tests in the not function instead of one

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.