Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Can anyone explain to me a bit of JS code?


  • Please log in to reply
7 replies to this topic

#1 Alvas Rawuther

Alvas Rawuther

  • Members
  • 356 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Mumbai, India.
  • Local time:04:02 PM

Posted 02 May 2012 - 03:55 AM

I'm learning JS basics from codecademy. It'd be great if some of you could explain to me a little code clearly and in a way that I understand.
var power = function(base,exponent){
    if(exponent === 0){
        return 1;
    }
    else{
        return base * power(base, exponent - 1);
    }
};

power(2,2);

What does exponent - 1 do in the sixth line of the code?
And what does this mean?

To compute the power of a number base, we multiply it by itself raised to the exponent - 1.


Also, how does this bit of code work in finding the power and how is it different from the method used above?
var power = function (base, exponent) {
  var result = 1;
  for ( i = 0; i < exponent; i++) {
    result = result * base;
  }
  return result;
};

power(2, 4);

What does the for loop do here? Please explain to me.

I'd love help. So please do help me. Any help would be appreciated a lot. Thank you. :mellow:
SYSTEM SPECS.
Windows 7 Ultimate SP1 | Intel Core 2 Duo E7500 @ 2.93GHz | 4.00 GB Dual-Channel DDR2 @ 333MHz RAM | 488 GB WD SATA HDD | 1024MB ATI Radeon HD 4350 | No real-time antivirus | MBAM on-demand | Windows 7's Built-in Firewall |

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:32 AM

Posted 02 May 2012 - 08:02 AM

Sure. In both examples (starting at the bottom), the 'power' function is called, and provided with two arguments. The arguments are the numbers inside the parenthesis. So in the first example you have 'power(2,4)'. In Javascript, functions can be assigned to variables. In these examples, you can ignore the part that says 'var power =' because it has nothing to do with what the function does.

Going back to the first example, 'base' equals 2, and 'exponent' equals 4. The first bit of code says "If the variable called exponent is equal to 0, return a 1 to whatever statement called us". 'Exponent' equals 4, so the first part of the code is ignored. The second line of code is called, and it says "multiple 'base', which is two, by whatever the return value is from power(base, exponent-1). If 'exponent' = 4, then 'exponent' minus 1 = 3. So if you substitute values for variable names, that line of code is saying "return 2 * power(2,3)". This is called a recursive function because it calls itself until some condition has been reached. In this case, until 'exponent' equals 0.

As for the second example, you should figure it out on your own since for loops are trivial to understand. You can use a pencil and paper to trace through how the values change pretty easily. If you are unable to understand that, then you need to stop and take some time to learn some of the basics otherwise you won't learn anything.

#3 Alvas Rawuther

Alvas Rawuther
  • Topic Starter

  • Members
  • 356 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Mumbai, India.
  • Local time:04:02 PM

Posted 31 March 2013 - 11:08 AM

Thanks a lot for your help. I'm late(very), but I guess it's still worth something. :)


SYSTEM SPECS.
Windows 7 Ultimate SP1 | Intel Core 2 Duo E7500 @ 2.93GHz | 4.00 GB Dual-Channel DDR2 @ 333MHz RAM | 488 GB WD SATA HDD | 1024MB ATI Radeon HD 4350 | No real-time antivirus | MBAM on-demand | Windows 7's Built-in Firewall |

#4 DarkSnake-Kobra

DarkSnake-Kobra

  • Members
  • 633 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Iowa, USA
  • Local time:05:32 AM

Posted 01 April 2013 - 05:18 PM

I didn't see this earlier, but I am familiar with a bit of programming.
 
var power = function (base, exponent) {
var result = 1;
for ( i = 0; i < exponent; i++) {
result = result * base;
}
return result;
};

power(2, 4);
The first line indicates that a variable power is receiving the contents of the function with the parameters base and exponent.

Second line assigns result with the value 1.

The 3rd line is a bit tricky. The for loop is made of 3 parts a start, stop and pause. i usually refers to index meaning it's a basic starting point to count from. i < exponent means that on every count it will check if i is greater than the value of exponent and if so it stops else it continues incrementing the value by 1. The i++ part means that it will increment the value of i by 1 on each call.

Edited by DarkSnake-Kobra, 01 April 2013 - 05:19 PM.


#5 Alvas Rawuther

Alvas Rawuther
  • Topic Starter

  • Members
  • 356 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Mumbai, India.
  • Local time:04:02 PM

Posted 02 April 2013 - 03:16 AM

I didn't see this earlier, but I am familiar with a bit of programming.
 

var power = function (base, exponent) {
var result = 1;
for ( i = 0; i < exponent; i++) {
result = result * base;
}
return result;
};

power(2, 4);
The first line indicates that a variable power is receiving the contents of the function with the parameters base and exponent.

Second line assigns result with the value 1.

The 3rd line is a bit tricky. The for loop is made of 3 parts a start, stop and pause. i usually refers to index meaning it's a basic starting point to count from. i < exponent means that on every count it will check if i is greater than the value of exponent and if so it stops else it continues incrementing the value by 1. The i++ part means that it will increment the value of i by 1 on each call.

 

Thanks for your help. But I guess I figured it out in a year, :P . I replied only so I could thank the other guy. Anyway, how's stuff going on?


SYSTEM SPECS.
Windows 7 Ultimate SP1 | Intel Core 2 Duo E7500 @ 2.93GHz | 4.00 GB Dual-Channel DDR2 @ 333MHz RAM | 488 GB WD SATA HDD | 1024MB ATI Radeon HD 4350 | No real-time antivirus | MBAM on-demand | Windows 7's Built-in Firewall |

#6 DarkSnake-Kobra

DarkSnake-Kobra

  • Members
  • 633 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Iowa, USA
  • Local time:05:32 AM

Posted 02 April 2013 - 09:43 PM

No problem! Wow I didn't even notice this was over a year old. :blush: Least he got this thank you although much later. :P Good good. :) Been busy with world of warcraft and python lately. You?



#7 Alvas Rawuther

Alvas Rawuther
  • Topic Starter

  • Members
  • 356 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Mumbai, India.
  • Local time:04:02 PM

Posted 10 April 2013 - 05:49 AM

No problem! Wow I didn't even notice this was over a year old. :blush: Least he got this thank you although much later. :P Good good. :) Been busy with world of warcraft and python lately. You?

Been busy studying a lot of stuff about network protocols and security(and cracking one's way inside them). :P


Edited by Alvas Rawuther, 10 April 2013 - 05:49 AM.

SYSTEM SPECS.
Windows 7 Ultimate SP1 | Intel Core 2 Duo E7500 @ 2.93GHz | 4.00 GB Dual-Channel DDR2 @ 333MHz RAM | 488 GB WD SATA HDD | 1024MB ATI Radeon HD 4350 | No real-time antivirus | MBAM on-demand | Windows 7's Built-in Firewall |

#8 DarkSnake-Kobra

DarkSnake-Kobra

  • Members
  • 633 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Iowa, USA
  • Local time:05:32 AM

Posted 11 April 2013 - 03:22 PM

Cool!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users