Jump to content

hisoka

Members
  • Content count

    563
  • Joined

  • Last visited

Everything posted by hisoka

  1. 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
  2. hisoka

    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 .
  3. hisoka

    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
  4. hisoka

    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 ?
  5. 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 ????
  6. 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 ?
  7. 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
  8. 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 ?
  9. 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
  10. 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 ?
  11. yes i know what is a subset but i do not know the answer for my 4 questions . That is why I asked them
  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 ? 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. 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]; ?
  15. hisoka

    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
  16. 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
  17. 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
  18. Hello glad to be again in w3schools forum when I opened a zip file in hex editor , I noticed that after the end of the file , there are additional hex values . For example in a zip file I opened in a hex editor there was like this : 50 4B 05 06 00 00 00 00 02 00 02 00 6F 00 00 00 84 09 00 00 00 00 50 4B 05 06 hex values represent the end of the zip file . However , I do not understand what represent these additional hex values after the end 00 00 00 00 02 00 02 00 6F 00 00 00 84 09 00 00 00 00 ??
  19. Thank you , I will try to put more efforts before asking the question
  20. could you please tell me what do some of these above mean ? what does Number of this disk mean ? what does Disk where central directory starts mean ? what are central directory records ?
  21. I already did . However I still do not understand what are the additional hex values after 50 4B 05 06
  22. and what represent these additional hex values after 50 4B 05 06 00 00 00 00 02 00 02 00 6F 00 00 00 84 09 00 00 00 00 ??
  23. I decided to post a new thread concerning this link case I do not understand something I write it here as I think it will be too long . Continuation of this already opened thread : http://w3schools.invisionzone.com/index.php?showtopic=55183&hl= Foxy Mod the second method is document.getElementsByTagName(name) This is a sample of a w3schools source code : <a target="_top" href="default.asp">JS HOME</a> <a target="_top" href="js_intro.asp">JS Introduction</a> <a target="_top" href="js_whereto.asp">JS Where To</a> <a target="_top" href="js_output.asp">JS Output</a> <a target="_top" href="js_syntax.asp">JS Syntax</a> When I put document.getElementsByTagName('a') I got this : HTMLCollection [ <a.attribute-value>, <a.attribute-value>, <a.attribute-value>, <a.attribute-value>, <a.attribute-value>, <a.attribute-value>, <a.attribute-value>, <a.attribute-value>, <a.attribute-value>, <a.attribute-value> I wonder why I did not get the element as it as namely like this <a target="_top" href="default.asp">JS HOME</a> <a target="_top" href="js_intro.asp">JS Introduction</a> <a target="_top" href="js_whereto.asp">JS Where To</a> <a target="_top" href="js_output.asp">JS Output</a> <a target="_top" href="js_syntax.asp">JS Syntax</a> <a target="_top" href="js_whereto.asp">JS Where To</a> <a target="_top" href="js_output.asp">JS Output</a> <a target="_top" href="js_syntax.asp">JS Syntax</a> <a target="_top" href="js_statements.asp">JS Statements</a> <a target="_top" href="js_comments.asp">JS Comments</a> <a target="_top" href="js_variables.asp">JS Variables</a> <a target="_top" href="js_operators.asp">JS Operators</a> <a target="_top" href="js_arithmetic.asp">JS Arithmetic</a> <a target="_top" href="js_assignment.asp">JS Assignment</a> <a target="_top" href="js_datatypes.asp">JS Data Types</a> ..... ???
  24. hisoka

    missile launch code

    Could you please tell me how does exactly a missile launch code work ? I have a website here which is of course fake https://whitehouse.gov1.info/launch/launch.html and I saw codes like this : pyHAvqTp3Mxye8vscUGr+92wvuhK6eyL9QBseQ4gsX3Fcnr2xO 9cMJHoSdEv98hVymwhLsOgLzigJHPTfxA0Ani7EWNaZx1hLaRm pyHAv2KZ9gRLgLtwb4spOh0Xb1cFjsZ3tcbo6CnuUT+wOy74p7 mQINBE+UYy0BEADeUhQF1JVfDU9/OAvWD3FajAyQPjAeiOoVRfukM0L8qFQCieCN pyHAvqTp3Mxye8vscUGr+92wvuhK6eyL9QBseQ4gsX3Fcnr2xO 9cMJHoSdEv98hVymwhLsOgLzigJHPTfxA0Ani7EWNaZx1hLaRm pyHAv2KZ9gRLgLtwb4spOh0Xb1cFjsZ3tcbo6CnuUT+wOy74p7 mQINBE+UYy0 how does the whole process work?
  25. var x = document.anchors ; for ( var i = 0 ; i < 5 ; i++) { console.log(x[i]); } gives as a result and even var x = document.anchors ; for ( var i = 0 ; i < 5 ; i++) { alert(x[i]); } gives undefined I do not know how to get the result . Any help
×