SlideShare uma empresa Scribd logo
1 de 23
Speaking in Code




Intro to JavaScript
Loops!



Brian Lee

Professor Liel Leibovitz
Speaking in Code


Review – Functions

• Easy way to reuse code

• Define a set of statements through one variable

  var divideByThree = function (number) {
       var val = number / 3;
       console.log(val);
  };
Speaking in Code


Review – Functions: Name

• The name of the function that will execute
  var divideByThree = function (number) {
       var val = number / 3;
       console.log(val);
  };

• Run the function by calling its name
  divideByThree(9);
Speaking in Code


Review – Functions: Parameters

• Parameters are passed in for the function to use

• Similar to a variable in math
   f(x) = x2 + 2


• Calling function with x = 3

   f(3) = 32 + 2
   >> 11
Speaking in Code


Review – Functions: Parameters

• In JavaScript passing in 3:          myFunc(3);

   var myFunc = function (number) {
       var val = Math.pow(number, 2) + 2;
       console.log(val);
   };

   var myFunc = function (3) {
       var val = Math.pow(3, 2) + 2;
       console.log(val);
   };
Speaking in Code


Review – Functions: Return Values

• What good are functions if all you do is print

• A functions return value is similar to a variable

   var addThree = function (number) {
        return number + 3;
   };

   var count = addThree(4);
   console.log(count)
   >> 7
Speaking in Code


Review – Functions: Return Values

• You can assign to a variable or use it right away


   if(addThree(4) > 10) {
        console.log(“We’re over 10!”);
   } else {
        console.log(addThree(4) + “ is not over 10”);
   }
   >> “7 is not over 10”
Speaking in Code


Review – Functions: Scope (Local)

• Where you define your variables matter

• Variables defined inside a function ONLY exist there

  var multiplyThree = function (number) {
      var multiplier = 3;
      return multiplier * number;
  };

  console.log(multiplier)
  >> multiplier is not defined
Speaking in Code


Review – Functions: Scope (Global)

• Where you define your variables matter

• Variables defined outside a function exist globally
   var multiplier = 3;
   var multiplyThree = function (number) {
       return multiplier * number;
   };

   console.log(multiplier)
   >> 3
Speaking in Code


Functions in other languages

• JavaScript
  var square = function (number) {
       return number * number;
  };


• Ruby (method)
  def square(number)
       return number * number
  end
Speaking in Code


Intro: Loops

• Execute same line(s) of code over and over

• Fundamental concept in programming

• Can be trickier with JavaScript
Speaking in Code


Intro: For Loops
• Basic Syntax
   for(var i = 0; i < 10; i++) {
        console.log(i);
   }

• Initialization: define variable useful to loop
• Conditional: keep looping while this is true
   – is “i” currently less than 10?

• Increment: executed at the end of the loop
Speaking in Code


Intro: Loop Mechanics
• In what order does this loop think?
   for(var i = 0; i < 10; i++) {
        console.log(i);
   }

1. Initialization
2. Check Conditional: Stop loop if false
3. Run Code
4. Run Increment: i++              i=i+1
5. Step 2
Speaking in Code


Infinite Loops
• Loops with no exit strategy

• Will continue to execute until crashing
Speaking in Code


Infinite Loops

• Why would this loop not work?

  for(var i = 0; i < 10; i--) {
       console.log(i);
  }`
Speaking in Code


Infinite Loops

• Why would this loop not work?

  for(var i = 0; i < 10; i--) {
       console.log(i);
  }`

  >> -1
  >> -2
  >> -3
  >> -4
  …
  >> -467389146129
Speaking in Code


Arrays
Speaking in Code


Arrays

• Collection of items

• Like a box (even looks like it)
   []


• Each item has a designated spot

   var doughnuts= [   ,   ,   ,     ]
Speaking in Code


Arrays: Accessing Elements

• Elements: items in arrays

• Index: location of element in array
   – Starts from 0 not 1

  var doughnuts= *‘Boston Creme’, ‘Glazed’, ‘Old Fashioned’ +

• How to access the value “Boston Creme”
Speaking in Code


Arrays: Accessing Elements

• Elements: items in arrays

• Index: location of element in array
   – Starts from 0 not 1

  var doughnuts= *‘Boston Creme’, ‘Glazed’, ‘Old Fashioned’ +
                          0           1            2
• How to access the value “Boston Creme”
  doughnuts[0]
Speaking in Code


Loops and Arrays

• Use loops to write less code
  var doughnuts = *‘Boston Creme’, ‘Glazed’, ‘Old Fashioned’ ]
  for(var i = 0; i < doughnuts.length; i++) {
       console.log(“This box has “ + doughnuts*i])
  }


  >> “This box has Boston Crème”
  >> “This box has Glazed”
  >> “This box has Old Fashioned”
Speaking in Code


Try it yourself! 

http://bit.ly/jsloops

http://jsbin.com
Speaking in Code


Real World Example

• Facebook:
  – Ever wanted to select all friends when sending out invites?

  var elms = document.getElementsByName("checkableitems[]");
  for (i = 0; i < elms.length; i++) {
       if (elms[i].type === "checkbox”) ,
             elms[i].click();
       }
  }

Mais conteúdo relacionado

Mais procurados

Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasserySHAMJITH KM
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories Intro C# Book
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processingIntro C# Book
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
Functional Programming and Ruby
Functional Programming and RubyFunctional Programming and Ruby
Functional Programming and RubyPat Shaughnessy
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode CompilerDonal Fellows
 

Mais procurados (20)

Day3
Day3Day3
Day3
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - Kalamassery
 
Python basics
Python basicsPython basics
Python basics
 
Build Your Own Monads
Build Your Own MonadsBuild Your Own Monads
Build Your Own Monads
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python
PythonPython
Python
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
Java introduction
Java introductionJava introduction
Java introduction
 
Python
PythonPython
Python
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Functional Programming and Ruby
Functional Programming and RubyFunctional Programming and Ruby
Functional Programming and Ruby
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode Compiler
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 

Semelhante a Intro to JavaScript Loops

C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)Eduard Tomàs
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016Codemotion
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chhom Karath
 
Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Jared Roesch
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
07 control+structures
07 control+structures07 control+structures
07 control+structuresbaran19901990
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++kinan keshkeh
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointersTAlha MAlik
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)Thinkful
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 developmentFisnik Doko
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developersJim Roepcke
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8Christian Nagel
 
Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscriptBill Buchan
 

Semelhante a Intro to JavaScript Loops (20)

Week 7 html css js
Week 7   html css jsWeek 7   html css js
Week 7 html css js
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Who go Types in my Systems Programing!
Who go Types in my Systems Programing!
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointers
 
Intro to javascript (6:19)
Intro to javascript (6:19)Intro to javascript (6:19)
Intro to javascript (6:19)
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscript
 

Mais de brianjihoonlee

Week 8 intro to python
Week 8   intro to pythonWeek 8   intro to python
Week 8 intro to pythonbrianjihoonlee
 
Week 5 java script functions
Week 5  java script functionsWeek 5  java script functions
Week 5 java script functionsbrianjihoonlee
 
Week 4 css recap and js
Week 4   css recap and jsWeek 4   css recap and js
Week 4 css recap and jsbrianjihoonlee
 
Week 3 html recap and css
Week 3   html recap and cssWeek 3   html recap and css
Week 3 html recap and cssbrianjihoonlee
 
Week 1 - Intro to the Internet
Week 1 - Intro to the InternetWeek 1 - Intro to the Internet
Week 1 - Intro to the Internetbrianjihoonlee
 

Mais de brianjihoonlee (6)

Week 8 intro to python
Week 8   intro to pythonWeek 8   intro to python
Week 8 intro to python
 
Week 5 java script functions
Week 5  java script functionsWeek 5  java script functions
Week 5 java script functions
 
Week 4 css recap and js
Week 4   css recap and jsWeek 4   css recap and js
Week 4 css recap and js
 
Week 3 html recap and css
Week 3   html recap and cssWeek 3   html recap and css
Week 3 html recap and css
 
Week 2 html
Week 2   htmlWeek 2   html
Week 2 html
 
Week 1 - Intro to the Internet
Week 1 - Intro to the InternetWeek 1 - Intro to the Internet
Week 1 - Intro to the Internet
 

Intro to JavaScript Loops

  • 1. Speaking in Code Intro to JavaScript Loops! Brian Lee Professor Liel Leibovitz
  • 2. Speaking in Code Review – Functions • Easy way to reuse code • Define a set of statements through one variable var divideByThree = function (number) { var val = number / 3; console.log(val); };
  • 3. Speaking in Code Review – Functions: Name • The name of the function that will execute var divideByThree = function (number) { var val = number / 3; console.log(val); }; • Run the function by calling its name divideByThree(9);
  • 4. Speaking in Code Review – Functions: Parameters • Parameters are passed in for the function to use • Similar to a variable in math f(x) = x2 + 2 • Calling function with x = 3 f(3) = 32 + 2 >> 11
  • 5. Speaking in Code Review – Functions: Parameters • In JavaScript passing in 3: myFunc(3); var myFunc = function (number) { var val = Math.pow(number, 2) + 2; console.log(val); }; var myFunc = function (3) { var val = Math.pow(3, 2) + 2; console.log(val); };
  • 6. Speaking in Code Review – Functions: Return Values • What good are functions if all you do is print • A functions return value is similar to a variable var addThree = function (number) { return number + 3; }; var count = addThree(4); console.log(count) >> 7
  • 7. Speaking in Code Review – Functions: Return Values • You can assign to a variable or use it right away if(addThree(4) > 10) { console.log(“We’re over 10!”); } else { console.log(addThree(4) + “ is not over 10”); } >> “7 is not over 10”
  • 8. Speaking in Code Review – Functions: Scope (Local) • Where you define your variables matter • Variables defined inside a function ONLY exist there var multiplyThree = function (number) { var multiplier = 3; return multiplier * number; }; console.log(multiplier) >> multiplier is not defined
  • 9. Speaking in Code Review – Functions: Scope (Global) • Where you define your variables matter • Variables defined outside a function exist globally var multiplier = 3; var multiplyThree = function (number) { return multiplier * number; }; console.log(multiplier) >> 3
  • 10. Speaking in Code Functions in other languages • JavaScript var square = function (number) { return number * number; }; • Ruby (method) def square(number) return number * number end
  • 11. Speaking in Code Intro: Loops • Execute same line(s) of code over and over • Fundamental concept in programming • Can be trickier with JavaScript
  • 12. Speaking in Code Intro: For Loops • Basic Syntax for(var i = 0; i < 10; i++) { console.log(i); } • Initialization: define variable useful to loop • Conditional: keep looping while this is true – is “i” currently less than 10? • Increment: executed at the end of the loop
  • 13. Speaking in Code Intro: Loop Mechanics • In what order does this loop think? for(var i = 0; i < 10; i++) { console.log(i); } 1. Initialization 2. Check Conditional: Stop loop if false 3. Run Code 4. Run Increment: i++ i=i+1 5. Step 2
  • 14. Speaking in Code Infinite Loops • Loops with no exit strategy • Will continue to execute until crashing
  • 15. Speaking in Code Infinite Loops • Why would this loop not work? for(var i = 0; i < 10; i--) { console.log(i); }`
  • 16. Speaking in Code Infinite Loops • Why would this loop not work? for(var i = 0; i < 10; i--) { console.log(i); }` >> -1 >> -2 >> -3 >> -4 … >> -467389146129
  • 18. Speaking in Code Arrays • Collection of items • Like a box (even looks like it) [] • Each item has a designated spot var doughnuts= [ , , , ]
  • 19. Speaking in Code Arrays: Accessing Elements • Elements: items in arrays • Index: location of element in array – Starts from 0 not 1 var doughnuts= *‘Boston Creme’, ‘Glazed’, ‘Old Fashioned’ + • How to access the value “Boston Creme”
  • 20. Speaking in Code Arrays: Accessing Elements • Elements: items in arrays • Index: location of element in array – Starts from 0 not 1 var doughnuts= *‘Boston Creme’, ‘Glazed’, ‘Old Fashioned’ + 0 1 2 • How to access the value “Boston Creme” doughnuts[0]
  • 21. Speaking in Code Loops and Arrays • Use loops to write less code var doughnuts = *‘Boston Creme’, ‘Glazed’, ‘Old Fashioned’ ] for(var i = 0; i < doughnuts.length; i++) { console.log(“This box has “ + doughnuts*i]) } >> “This box has Boston Crème” >> “This box has Glazed” >> “This box has Old Fashioned”
  • 22. Speaking in Code Try it yourself!  http://bit.ly/jsloops http://jsbin.com
  • 23. Speaking in Code Real World Example • Facebook: – Ever wanted to select all friends when sending out invites? var elms = document.getElementsByName("checkableitems[]"); for (i = 0; i < elms.length; i++) { if (elms[i].type === "checkbox”) , elms[i].click(); } }

Notas do Editor

  1. Example: brush teethDon’t have to list out what you do everytime
  2. var elms = document.getElementsByName(&quot;checkableitems[]&quot;); for (i = 0; i &lt; elms.length; i++) { if (elms[i].type === &quot;checkbox&quot;) { elms[i].click(); } }