Jump to content

hisoka

Members
  • Posts

    569
  • Joined

  • Last visited

Posts posted by hisoka

  1. thank you . Could you please suggest me a Mozilla Firefox  program that runs JavaScript code ,  that is easy to use and far more  flexible and comfortable than the web console , where I can copy paste the content i need without any problem . May be an ad don that I do not know that is compatible with Firefox  58 .  I have Mozilla Firefox 58 and , unfortunately , an add on like , for example ,   Execute JS is not compatible with my version but with the old versions of Firefox

    best regards

  2. Here is my problem :

    for example when I wrote this little piece of code in scratchpad and run it then I got the result in   the web console of Mozilla Firefox  ,

    var s = "hello w3schools how are you doing";
    var m ;
    for(i=0 ; i <s.length ; i++)
      {
       m = s.charCodeAt(i);
       console.log(m);
      }

    I got a result of many numbers . Now when I tried to copy the numbers I got in the web console , it did not let me copy only the numbers but other things so that I could not paste only the numbers but the other things too which are not necessary for me . The same happened when I run the code directly in the web console :


     

    Quote

     

    104
    Scratchpad/4:6:4
    101
    Scratchpad/4:6:4
    108
    Scratchpad/4:6:4
    111
    Scratchpad/4:6:4
    32
    Scratchpad/4:6:4
    119
    Scratchpad/4:6:4
    51
    Scratchpad/4:6:4
    115
    Scratchpad/4:6:4
    99
    Scratchpad/4:6:4
    104
    Scratchpad/4:6:4
    111
    Scratchpad/4:6:4
    108
    Scratchpad/4:6:4
    115
    Scratchpad/4:6:4
    32
    Scratchpad/4:6:4
    104
    Scratchpad/4:6:4
    111
    Scratchpad/4:6:4
    119
    Scratchpad/4:6:4
    32
    Scratchpad/4:6:4
    97
    Scratchpad/4:6:4
    114
    Scratchpad/4:6:4
    101
    Scratchpad/4:6:4
    32
    Scratchpad/4:6:4
    121
    Scratchpad/4:6:4
    111
    Scratchpad/4:6:4
    117
    Scratchpad/4:6:4
    32
    Scratchpad/4:6:4
    100
    Scratchpad/4:6:4
    111
    Scratchpad/4:6:4
    105
    Scratchpad/4:6:4
    110
    Scratchpad/4:6:4
    103

     

     

     

    The same happened when I run the code directly in the web console :

    Quote

    104 debugger eval code:6:4
    101 debugger eval code:6:4
    108
    debugger eval code:6:4
    111 debugger eval code:6:4
    32 debugger eval code:6:4
    119 debugger eval code:6:4
    51 debugger eval code:6:4
    115 debugger eval code:6:4
    99 debugger eval code:6:4
    104 debugger eval code:6:4
    111
    debugger eval code:6:4
    108 debugger eval code:6:4
    115 debugger eval code:6:4
    32 debugger eval code:6:4
    104 debugger eval code:6:4
    111 debugger eval code:6:4
    119 debugger eval code:6:4
    32 debugger eval code:6:4
    97 debugger eval code:6:4
    114 debugger eval code:6:4
    101 debugger eval code:6:4
    32 debugger eval code:6:4
    121 debugger eval code:6:4
    111 debugger eval code:6:4
    117 debugger eval code:6:4
    32 debugger eval code:6:4
    100 debugger eval code:6:4
    111 debugger eval code:6:4
    105 debugger eval code:6:4
    110 debugger eval code:6:4
    103

     

     

     

    I need only to copy paste the numbers from the web console  not with the  Scratchpad.... or debugger eval.... Please could you help me by  showing me how to do it or whether there is a solution to the problem to copy paste numbers without the unnecessary rest .

  3. I have trouble understanding this long javascript code :

    var p = 0;
    var r = '';
    var b = new Array();
    for(var i=0; i<32768; i++) b[i] = 0;
    
    function main() {
    	incPtr(1);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(8);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(5);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(6);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(5);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(2);
    	while(b[p]>0) {
    		decPtr(1);
    		decVal(7);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(3);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(4);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(1);
    	echo(String.fromCharCode(b[p]));
    	decVal(4);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(3);
    	while(b[p]>0) {
    		decPtr(1);
    		decVal(4);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	decVal(1);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(3);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(4);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(2);
    	while(b[p]>0) {
    		decPtr(1);
    		decVal(4);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(4);
    	while(b[p]>0) {
    		decPtr(1);
    		decVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	decVal(3);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	incVal(4);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(2);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(8);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(1);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(5);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(3);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	decVal(7);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(3);
    	incPtr(1);
    	b[p] = prompt('', '').charCodeAt(0);
    	echo(String.fromCharCode(b[p]));
    	incVal(2);
    	incPtr(1);
    	b[p] = prompt('', '').charCodeAt(0);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	b[p] = prompt('', '').charCodeAt(0);
    	echo(String.fromCharCode(b[p]));
    	incPtr(1);
    	b[p] = prompt('', '').charCodeAt(0);
    	echo(String.fromCharCode(b[p]));
    	decVal(4);
    	incPtr(1);
    	b[p] = prompt('', '').charCodeAt(0);
    	echo(String.fromCharCode(b[p]));
    	incPtr(3);
    	incVal(8);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(4);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(9);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	decVal(3);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(4);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(1);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(3);
    	incPtr(2);
    	incVal(6);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(5);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(3);
    	incPtr(1);
    	incVal(1);
    	incPtr(2);
    	incVal(8);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(4);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(7);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	decVal(3);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(1);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(4);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(4);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(1);
    	incPtr(2);
    	incVal(10);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	decVal(1);
    	incPtr(2);
    	incVal(11);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(10);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(6);
    	incPtr(2);
    	incVal(6);
    	while(b[p]>0) {
    		decPtr(1);
    		incVal(5);
    		incPtr(1);
    		decVal(1);
    	}
    	decPtr(1);
    	incVal(3);
    	decPtr(22);
    	while(b[p]>0) {
    		decPtr(5);
    		decVal(1);
    		incPtr(5);
    		decVal(1);
    	}
    	decPtr(5);
    	while(b[p]>0) {
    		incPtr(10);
    		incVal(1);
    		decPtr(10);
    		while(b[p]>0) {
    			decVal(1);
    		}
    	}
    	incPtr(6);
    	decVal(1);
    	while(b[p]>0) {
    		decPtr(5);
    		decVal(1);
    		incPtr(5);
    		decVal(1);
    	}
    	decPtr(5);
    	while(b[p]>0) {
    		incPtr(9);
    		incVal(1);
    		decPtr(9);
    		while(b[p]>0) {
    			decVal(1);
    		}
    	}
    	incPtr(6);
    	incVal(2);
    	while(b[p]>0) {
    		decPtr(5);
    		decVal(1);
    		incPtr(5);
    		decVal(1);
    	}
    	decPtr(5);
    	while(b[p]>0) {
    		incPtr(8);
    		incVal(1);
    		decPtr(8);
    		while(b[p]>0) {
    			decVal(1);
    		}
    	}
    	incPtr(6);
    	while(b[p]>0) {
    		decPtr(5);
    		decVal(1);
    		incPtr(5);
    		decVal(1);
    	}
    	decPtr(5);
    	while(b[p]>0) {
    		incPtr(7);
    		incVal(1);
    		decPtr(7);
    		while(b[p]>0) {
    			decVal(1);
    		}
    	}
    	incPtr(6);
    	incVal(5);
    	while(b[p]>0) {
    		decPtr(5);
    		decVal(1);
    		incPtr(5);
    		decVal(1);
    	}
    	decPtr(5);
    	while(b[p]>0) {
    		incPtr(6);
    		incVal(1);
    		decPtr(6);
    		while(b[p]>0) {
    			decVal(1);
    		}
    	}
    	incPtr(6);
    	while(b[p]>0) {
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		decVal(1);
    		decPtr(8);
    		while(b[p]>0) {
    			decVal(1);
    		}
    	}
    	incPtr(8);
    	while(b[p]>0) {
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		incPtr(1);
    		echo(String.fromCharCode(b[p]));
    		decPtr(9);
    		while(b[p]>0) {
    			decVal(1);
    		}
    	}
    }
    
    function incPtr(n) {
    	if(p < 32676 - n) p += n; else p = 0;
    }
    
    function decPtr(n) {
    	if(p > n - 1) p -= n; else p = 255;
    }
    
    function incVal(n) {
    	if(b[p] < 255 - n) b[p] += n; else b[p] = 0;
    }
    
    function decVal(n) {
    	if(b[p] > n - 1) b[p] -= n; else b[p] = 255;
    }
    
    function echo(c) {
    	r += c;
    }
    var isWS = (typeof(alert)=='undefined');
    if(isWS) prompt = function() {WScript.Echo("Unable to run this program.\n'prompt' is not supported under Windows Script Host!");return "";};
    main();
    if(r!='') if(isWS) WScript.Echo(r); else alert(r);

     

    is it obfuscated ? if yes how to get the original code ? so that i could understand what it does

    any idea?

     

  4. Now I am trying to take a number as input in box and once I press ok all the primes that are less than it are generated I did like this :
     

    function calculate(x)
    {
    var x = prompt("enter a number");
    
    var results = "" ;
    var y=parseInt(x);
      
     
    
    for(i=0 ; i<n ; i++)
    {
     var isprime = false;
     for(j=2 ; j<n ; j++)
     {
     if(i%j===0 && i!==j)
       {
        isprime = true;
       }
     }
     if(isprime===false)
       {
     
         results += i + "\n";
       }
    
    }
    return results; 
    }
    
    console.log(calculate(x));

    but it gives no result . I know that something I added in the code is wrong but I do not know how to correct it .

  5. I changed that code with another code :
     

    function calculate(n)
    {
    for(i=0 ; i<=n ; i++)
    {
     var isprime = false;
     for(j=2 ; j<=n ; j++)
     {
     if(i%j===0 && i!==j)
       {
        isprime = true;
       }
     }
     if(isprime===false)
       {
         console.log(i+"\n");
         
       }
     
    }
    
    }
    
    console.log(calculate(17));

    then it gives me :

    1
     
    2
     
    3
     
    5
     
    7
     
    11
     
    13
     
    17

    undefined

    I do not know why get undefined and how to correct the code so that I do not get undefined

  6. That is why I put the code here and asked for help because I do not know how  that function will return a value , how it will print it in the computer screen so that I can see it . What to do so that the function is well written and how to do it !!

    Quote

    how are you supposed to know if the number is prime or not?

    Generally I know that an integer is a prime if it is not divisible by any prime  less or equal to its square root hence we have in that code  the

      var q = Math.floor(Math.sqrt( n));

    it gives the square root of a number combined with math.floor and the for loop it takes n % i which is n % all numbers that are less or equal to the square root of n and dismisses or ignore all the numbers that are divisible by n as they are not prime .

     

    Moreover the role of the function is not to look if n is a prime or not otherwise to give all the primes between 2 and n

    My problem is I cannot make it print the values . You wrote that is it not well written , it does not return a value ....... I know all that . So what do you suggest to make it well writeen , return a value and print it in the screen . What is the substitution ? any suggest from you on what to do ?

  7. I have this javascript code and when I run it it does not print the result :
     

    function isprime(n)
    {
    
      if (n<2)
      {
       return false;
      }
     
      var q = Math.floor(Math.sqrt( n));
       
      for( i = 2 ; i<= q ; i++)
      {
        var result = n%i;
        
        if(result==0)
           
          
         {
          
         return false;
         
          }
        
       else
         
        {
         document.write(result);
        }
        
      }
     
    }
    
    
    var prime = isprime(100);
    
    

    I do not know why ? any help on what is the problem is and how to solve it

  8. Quote

    The second parameter indicates the size of the subsets to search for, not the size of the initial set.

    That is it thank you . Sorry , I did not read the comments . Next time I will read the comments too and look al all possible corners of the page :)

    Quote

    You do realise that the indexing of array starts from 0 not 1, so if you use array length (3) for last item in array (which will be index 2), it will come up as undefined, unless you use array.length -1 for the last record.

    Yes I realise . However if the computer begins to calculate from 0 not from 1 like humans so why the output  here :

    var array = [1, 2, 3] ; var x = array.length ; console.log(x) ;

    is 3 and not 2 ?

     

  9. now the things become more clear : the shift operator is used to change the position of the numbers in the set and the and operator is used to concatenate them together so that they form all possible subset . What I understood ,  too , is that the number of subsets in a set of x elements is 2 exponent x . The code above is taken from this page :

    http://www.w3resource.com/javascript-exercises/javascript-function-exercise-21.php

    In the end of the code , there is this instruction :

    console.log(subset([1, 2, 3], 2));

    and the expected output from it is  , as mentioned ,

     

    Expected output : [[2, 1], [3, 1], [3, 2], [3, 2, 1]] 

    which are 4 subsets

    however if we take a look at the array in the instruction above , we will see that it contains 3 elements . So 2 exponent 3 is 8 . So normally we need to have 8 subsets and not 4 subsets . So why we got 4 subsets ?

  10. Quote

    Hold on, are you running different versions of that code with only 1 of the console.log statements?

     

    that what I , unfortunately , did . However , I run it again with all the console.log statements inside it and , now the logic of the code becomes far more clear to me than before . Moreover , now I understand very well the logic beyond using the Math.pow function . However even with this :

     

     

    I still cannot grasp very well  the use of bitwise operators in the code

    Quote

    That's how it determines which elements to pull from the source array for each subset

    and this :

     

    (1 << i) is 1
    x & (1 << i) is 0
    result:
    Array [ 2 ]
    x is 3
    i is 2
    (1 << i) is 4
    x & (1 << i) is 0
    (1 << i) is 2
    x & (1 << i) is 2
    (1 << i) is 1
    x & (1 << i) is 1
    result:
    Array [ 2, 1 ]
    x is 4
    i is 2
    (1 << i) is 4
    x & (1 << i) is 4
    (1 << i) is 2
    x & (1 << i) is 0
    (1 << i) is 1
    x & (1 << i) is 0
    result:
    Array [ 3 ]
    x is 5
    i is 2
    (1 << i) is 4
    x & (1 << i) is 4
    (1 << i) is 2
    x & (1 << i) is 0
    (1 << i) is 1
    x & (1 << i) is 1
    result:
    Array [ 3, 1 ]
    x is 6
    i is 2
    (1 << i) is 4
    x & (1 << i) is 4
    (1 << i) is 2
    x & (1 << i) is 2
    (1 << i) is 1
    x & (1 << i) is 0
    result:
    Array [ 3, 2 ]
    x is 7
    i is 2
    (1 << i) is 4
    x & (1 << i) is 4
    (1 << i) is 2
    x & (1 << i) is 2
    (1 << i) is 1
    x & (1 << i) is 1
    result:
    Array [ 3, 2, 1 ]

    I am still unable to understand how and why  the bitwise operators are used to determine which element is pulled from the set and pushed in the subset ??

    Could you please make it more clear to me

     

  11. Quote

     If you run the version I posted with all of the output it makes it more obvious.

    with all due respect , after running the version you posted to me , things become more obscure :

    Here is the whole code :
     

    function subset(arra, arra_size) {
        var result_set = [],
            result;
        for (var x = 0; x < Math.pow(2, arra.length); x++) {
            console.log('x is ' + x);
            result = [];
            i = arra.length - 1;
            console.log('i is ' + i);
            do {
                console.log('(1 << i) is ' + (1 << i));
                console.log('x & (1 << i) is ' + (x & (1 << i)));
                if ((x & (1 << i)) !== 0) {
                    result.push(arra[i]);
                }
            } while (i--);
            console.log('result:');
            console.log(result);
            if (result.length >= arra_size) {
                result_set.push(result);
            }
        }
        return result_set;
    }
    
    var matrix = subset([2, 5, 6], 2);

    I run it line by line :

    console.log('x is ' + x);

    gives

    x is 0x is 1x is 2x is 3x is 4x is 5x is 6x is 7

    console.log('i is ' + i);

    gives

    i is 2i is 2i is 2i is 2i is 2i is 2i is 2i is 2

    console.log('(1 << i) is ' + (1 << i));
                console.log('x & (1 << i) is ' + (x & (1 << i)));

    give

    (1 << i) is 4(1 << i) is 2(1 << i) is 1(1 << i) is 4(1 << i) is 2(1 << i) is 1(1 << i) is 4(1 << i) is 2(1 << i) is 1(1 << i) is 4(1 << i) is 2(1 << i) is 1(1 << i) is 4(1 << i) is 2(1 << i) is 1(1 << i) is 4(1 << i) is 2(1 << i) is 1(1 << i) is 4(1 << i) is 2(1 << i) is 1(1 << i) is 4(1 << i) is 2(1 << i) is 1

    x & (1 << i) is 0x & (1 << i) is 0x & (1 << i) is 0x & (1 << i) is 0x & (1 << i) is 0x & (1 << i) is 1x & (1 << i) is 0x & (1 << i) is 2x & (1 << i) is 0x & (1 << i) is 0x & (1 << i) is 2x & (1 << i) is 1x & (1 << i) is 4x & (1 << i) is 0x & (1 << i) is 0x & (1 << i) is 4x & (1 << i) is 0x & (1 << i) is 1x & (1 << i) is 4x & (1 << i) is 2x & (1 << i) is 0x & (1 << i) is 4x & (1 << i) is 2x & (1 << i) is 1
     

     console.log('result:');
            console.log(result);
    

    give

    result:result:result:result:result:result:result:result:

    255,266,26,56,5,2

    except the last result all the results of console log  the version you gave me  are obscure and made me more confused 

    Moreover I still do not understand how math.pow gives the number of all possible subset ?!!!

    I read the tutorials about javascript in w3school site which were very useful and helped me a lot but even so I could not get a clear understanding about the code above . Justsomeguy could you please tell me what should I do in order to be able to understand very well the code above ?

     

  12. Syntactic , this what I could understand so far
    f

    unction subset(arra, arra_size) {
        var result_set = [],
            result;
    
    
        for (var x = 0; x < Math.pow(2, arra.length); x++) {
            result = [];
            i = arra.length - 1;
            do {
                if ((x & (1 << i)) !== 0) {
                    result.push(arra[i]);
                }
            } while (i--);
    
            if (result.length >= arra_size) {
                result_set.push(result);
            }
        }
    
        return result_set;
    }
    
    console.log(subset([1, 2, 3], 2));

    the for loop , loops

     Math.pow(2, arra.length)

    time . For example here it loops 2 exponent 3 time that is 8 times

     

     i = arra.length - 1; 

    is , of course ,  array length -1

    if( (x & (1 << i)) !== 0)

    each time it loops it looks if

    x & (1 << i)) !== 0

    put the result in the empty array result []

    result.push(arra[i])
    while(i--);

    is a reverse loop . It reverse the numbers .

     if( result.length >= arra_size)

    put a condition so that while does not loop infinitely

     

     result_set.push(result);

    push the sub arrays in the array

     result_set

    I cannot understand  the code semantically :

    1) math.pow is an exponent mathematical function . What has it to do with array length and for loop in the code above . What is the logical beyond using it ?

    2) what is the logic beyond  using bitwise operators in the if condition ?

    if( (x & (1 << i)) !== 0)
    3)
    
     i = arra.length - 1; 

      what is the logic beyond substracting the  array length from 1 ?

    4) in the function parameters I understand why the arra is used but why  arra-size is used ?

     

    Quote

     

    Do you understand the purpose of that code, do you understand why it returns what it does for the input?


     

    here is the purpose of it :
    
    http://www.w3resource.com/javascript-exercises/javascript-function-exercise-21.php
    

    no I do not understand why it returns what it does for the input because i as I mentioned above i do not get what the code line by line , semantically , does ?

     

     

  13. There are things I cannot understand in this code :

    function subset(arra, arra_size) { var result_set = [], result; for(var x = 0; x < Math.pow(2, arra.length); x++) { result = []; i = arra.length - 1; do { if( (x & (1 << i)) !== 0) { result.push(arra[i]); } } while(i--); if( result.length >= arra_size) { result_set.push(result); } } return result_set; } console.log(subset([1, 2, 3], 2));

     The Math.pow() function returns the base to the exponent power, that is, baseexponent.

    But I do not understand why it is used in the code above ? what is the purpose of it ? and why it is used in combination with

    arr.length

    what does this mean :

    i = arra.length - 1; 

    why bitwise and shift operators are used here in this code ? for what purpose ?

    (x & (1 << i)) !== 0)

    what is the purpose of making an empty array ?

    result = [];

     

  14. it is a good idea but in a text editor , I do not see my errors  when I run the code in the browser because they are not shown like in scratchpad or web console . if there is an error I got only a blank page .  that , of course , makes it  very hard - at least for a beginner like me-  to notice my errors and therefore correct them . Moreover I need , every time , to save my code  as HTML and then launch it in the browser . No good thing

  15. you said Number convert the string into numbers so why here in this code I got NAN
     

     function sum()
    {
    var total = 0;
    var letters = ["A" , "B"];
    for(x = 0 ; x < letters.length ; x++)
        {
         total += Number(letters[x]);
        }
        document.write(total);
    }
    
    
    var x = sum();

    if you run it in the browser you will get NOT A NUMBER OR NAN

  16. function sum(input){ if (toString.call(input) !== "[object Array]") return false; var total = 0; for(var i=0;i<input.length;i++) { if(isNaN(input[i])){ continue; } total += Number(input[i]); } return total; } console.log(sum([1,2,3])); console.log(sum([100,-200,3])); console.log(sum([1,2,'a',3]));

    In this code I do not understand what is

     total += Number(input[i]);

    Number ????

  17. I looked for a tool  inside mozilla - by clicking Menu then developer -  in which I can write a code composed of both javascript and html . Unfortunately , I could not find in the developer any tool that accepts both html and javascript together . web console and scratchpad do not accept html . Is there  - may be - a tool inside Mozilla firefox or an extension that I overlooked and that could help me write a code of both html and javascript because I do not want to download a javascript developer tool from google

×
×
  • Create New...