0

this question is quite basic, Here is a sample code what I want to do in javascrtipt:

(function getdata(){
  gettrue( true=true );
  gettrue( 1==2 );
  gettrue( 1==1 );
  // write something here to get all three expressions result ? 
  // console.log( [ all three result ] )
})()

gettrue(){}

update:

This question can be used for a test framework:

test("several test result:" , function(){
   assert("test1" , true );
   assert("test2" , false )
 })

function test(title , fn){
   console.log("running: " + title)
   fn()
   // write something here to get all result of assert
   //  not more than 2 lines
  }

function assert(str, test){
   if(test){
     console.log(str +" :  PASS ")
   }else{
     console.log(str +" :  Fail ")
   }
   return test
}
 
7
  • it is spelling mistake , should be true==true . The rule is not allowed to use return from gettrue(), and not allowed to change three lines include gettrue
    – twindai
    Commented Jun 15, 2015 at 15:53
  • gettrue return undefined , and I want to get 3 equal expressions directly
    – twindai
    Commented Jun 15, 2015 at 16:00
  • please help us with some more words. who should communicate? and with whom? and what? when gettrue is just a dummy, why not name it dummy? and - at least - what do you mean with scope in this contect? Commented Jun 16, 2015 at 9:41
  • I need some code at end of getdata() to communicate with 3 arguments of gettrue() . it could be used for test all result of previous gettrue.
    – twindai
    Commented Jun 16, 2015 at 10:52
  • you could also try: test( function(){ assert( true) ; assert(false) } );
    – twindai
    Commented Jun 16, 2015 at 10:53

4 Answers 4

2

You need to store the results somewhere. As it is, you're throwing all three away.

An array seems simplest:

(function getdata() {
  var results = [
    gettrue( true==true ),
    gettrue( 1==2 ),
    gettrue( 1==1 )
  ];

  console.log(results);
})()
6
  • Plus1, but I'd go for results.push() myself. I know it's pedantic, but I'd find that easier to read a year later. Commented Jun 15, 2015 at 15:40
  • I like this answer, but gettrue is return nothing, I want to find a way to get 3 equal expressions directly.
    – twindai
    Commented Jun 15, 2015 at 15:59
  • 1
    If gettrue() returns nothing, what is it you want to log?
    – Paul Roub
    Commented Jun 15, 2015 at 16:04
  • gettrue, will do log , report, but not return test result, the main issue here is , I want to find a way to find all expressions in getdata scope.
    – twindai
    Commented Jun 15, 2015 at 21:09
  • this could be used for another purpose , in a scope you have 2 assert: test( function(){ assert( true ) ; assert(false) }) function test(fn){ // how to get both assert expresion , without using return }
    – twindai
    Commented Jun 15, 2015 at 21:13
0

assignment true=true is not valid. gettrue() is not returning something and missing a parameter.

function gettrue(b) { return !!b; }
(function getdata() {
    console.log([
        gettrue(true == true),
        gettrue(1 == 2),
        gettrue(1 == 1),
    ]);
})();
1
  • the gettrue() is just a dummy, In purpose to show that the question has no relation with what gettrue do
    – twindai
    Commented Jun 16, 2015 at 7:37
0

I am not sure I understand the question but are you looking for something like this? Also you cannot set true to anything did you mean true == true?

var values = [];

(function getdata(){
  values.push(gettrue( true==true ));
  values.push(gettrue( 1==2 ));
  values.push(gettrue( 1==1 ));

  console.log(values);
})();

function gettrue(val){ return val; }
1
  • actually , not allowed return from gettrue() and not allowed to change 3 lines with gettrue
    – twindai
    Commented Jun 15, 2015 at 15:55
0

as a conclusion , discussed with several experienced programmer . it is not possible to get all evaluation result without touch getture or assert function.

var count;
(function getdata(){
  count=0 

  gettrue( true );
  gettrue( 1==2 );
  gettrue( 1==1 );

  gettrue( count==3 )
})()

function gettrue(test ){
  console.log( test ) 
  count++
}

for test framework, it could be :

    var count;

    test("several test result:" , function(){
       assert("test1" , true );
       assert("test2" , false )
     })

    function test(title , fn){
       console.log("running: " + title)
       count = 0 ;
       fn()
       var expectTotal = fn.toString().match(/assert/)

       if(expectTotal)
        assert( title , expectTotal.length == count )
      }

    function assert(str, test){
       if(test){
         console.log(str +" :  PASS ")
       }else{
         console.log(str +" :  FAIL ")
       }
       count ++
       return test
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.