Jump to content

hisoka

Recommended Posts

I wrote this to know what happens when a for loop is inside another

 

<script>var letter = "ab";for(var i=0; i<letter.length; i++){var sum = 0;for(var k=0; k<letter.length; k++){document.write("<br />");document.write(sum += (letter.charCodeAt(i+k%4) & 15 ) << 1);}}</script>

 

it gives

 

2

 

6

 

4

 

4

 

I understand the why I got 2 and 6 but I cannot understand why I got 4 then 4 after 2 and 6

 

so Why ?

Link to comment
Share on other sites

Seriously, why are you using modulus, bitwise and, and bit shifting when you're trying to figure out how loops work?

var letter = "ab";for(var i=0; i<letter.length; i++){  for(var k=0; k<letter.length; k++)  {    console.log("i is " + i + ", k is " + k);    console.log("character i is " + letter.charAt(i) + ", character k is " + letter.charAt(k));  }}
Link to comment
Share on other sites

it is thanks to this

 

"Seriously, why are you using modulus, bitwise and, and bit shifting when you're trying to figure out how loops work?"

 

that I got the inspiration to understand , even for a little degree, how both for loop interact together

 

In this script :

 

var pass = "abc";for(var i=0; i<3; i++){for(j=0 ; j<4 ; j++){var tmp = 0;document.write("<br />");document.write(pass.charCodeAt(i+j%4)+10);}}</script>

 

I noticed the following

 

the first for loop , loops through the string "abc" // it gives 107 108 109

 

the second loop inside the for loop , loops the result of the first loop a number of times specified in the second statement of the second loop . I also noticed that the number of times the second loop lopps through the result of the first loop is computed by multiplying the number in the second statement of the for loop by the number of the second statement in the for loop 3*4 = 12 . What followed is that I got this as an ouput

107108109NaNNaN109NaNNaNNaN

 

now coming to the result I got , I tried to understand it :

 

first loop of the second loop gives 107 , 108 , 109

second loop of the second loop gives NaN , 108 , 109

third loop of the second loop gives NaN , NaN , 109

fourth loop of the second loop gives NaN , NaN , NaN

 

why it gives NaN because in the first loop it is mentioned that the scan must be i<3 number of times

 

0<4 = 107 true

1<4 = 108 true

3<4 = 109 true

 

4=4 false and the loop stops , the second loop when it loops more than three times it surpass the limit of the string and does not find any number so it gives NaN

 

0<4 = 107 true

1<4 = 108 true

3<4 = 109 true

4 = 4 = false (NaN)

 

I noticed another thing : namely the manner by which the pointer of the second for loop works

 

the for loop loops like this

 

-> begins by zero 0,1,2,3 (107 , 108 ,109 ,NaN)

-> turn back and start from one 1,2,3,4 (108,109,NaN , NaN )

-> turn back and start from two 2,3,4,5 (109,NaN , NaN , NaN)

 

now based on that I understand that when I change the number in the second statement of the second for loop I get

 

<script>var pass = "abc";for(var i=0; i<3; i++){for(j=0 ; j<1 ; j++){var tmp = 0;document.write("<br />");document.write(pass.charCodeAt(i+j%4)+10);}}</script>

 

output

 

107

 

108

 

109

 

I would appreciate it if you correct me in every step and show me my errors

Link to comment
Share on other sites

The problem is that (i + j % 4) can be greater than 2, which is the highest index that a string with three characters can have.

 

charCodeAt(0) will give the code for the first letter

charCodeAt(1) will give the code for the second letter

charCodeAt(2) will give the code for the third letter

charCodeAt(3) will give NaN because there is no fourth letter

  • Like 1
Link to comment
Share on other sites

this script here

var letter = "ab";for(var i=0; i<letter.length; i++){for(var k=0; k<letter.length; k++){var sum = 0;document.write("<br />");document.write(sum += (letter.charCodeAt(i+k%4) & 15 ) << 1);}}</script>

 

gives

 

2

 

4

 

4

 

0

 

(97 & 15)<<1= 2 //a

 

(98 & 15)<<1 = 4 //b

 

(98 & 15)<<1 = 4 //b

 

(NaN & 15)<<1 = 0//NaN

 

the second loop , loops like this

 

0,1 ( a,b)/2,4)

 

1,2 (b,NaN( 4,0)

 

it is all up to the pointer of the second for loop from where it starts and where to end :rofl: :rofl:

 

"The problem is that (i + j % 4) can be greater than 2, which is the highest index that a string with three characters can have." that is

 

97 & 15)<<1= 2 //a

 

(98 & 15)<<1 = 4 //b

 

(98 & 15)<<1 = 4 //b

 

(NaN & 15)<<1 = 0//NaN

 

the second loop , loops like this

 

0,1 ( a,b)/2,4)

 

1,2 (b,NaN( 4,0)

Edited by hisoka
Link to comment
Share on other sites

They are operators used by the computers CPU and Registers to calculate and deal with binaries 0 and 1

 

and for encoding and decoding , they are used in cryptography and many other fields

 

please if I am wrong correct me

Edited by hisoka
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...