Jump to content

hisoka

Members
  • Content count

    488
  • Joined

  • Last visited

Community Reputation

2 Neutral

About hisoka

  • Rank
    Member
  1. javascript code does not show result

    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 .
  2. javascript code does not show result

    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
  3. javascript code does not show result

    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 !! 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 ?
  4. 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
  5. 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 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 ?
  6. Normally , the second parameter passed to the function is 3 not 2 because the size of [1,2,3] is 3 not 2 . I know it is 3 through this : var array = [1, 2, 3] ; var x = array.length ; console.log(x) ; the output is 3 not 2 and 2 exponent 3 is 8 not 4
  7. 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 ?
  8. 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 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
  9. 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 ?
  10. yes i know what is a subset but i do not know the answer for my 4 questions . That is why I asked them
  11. 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 ? 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 ?
  12. 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 = [];
  13. ok I understand the items inside the array are numbers so why we need to convert them into numbers ??? and therefore why do not we simply write : total += input[i]; ?
  14. mozilla web console for html + javascipt both

    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
×