Skip to content

Commit 6e935bc

Browse files
committed
Added Sectin 10
1 parent d710484 commit 6e935bc

File tree

13 files changed

+943
-0
lines changed

13 files changed

+943
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "C:\\Users\\DELL\\anaconda3\\python.exe"
3+
}
189 KB
Binary file not shown.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// FUNCTIONS
2+
3+
// Functions will be one of our main building blocks when we construct
4+
// larger and larger amounts of code to solve problems.
5+
//
6+
// So what is a function?
7+
//
8+
// Formally, a function is a useful device that groups together a set of statements
9+
// so they can be run more than once. They can also let us specify parameters that
10+
// can serve as inputs to the functions.
11+
// On a more fundamental level, functions allow us to not have to repeatedly write
12+
// the same code again and again.
13+
14+
// Functions are one of the most critical parts of being able to use JavaScript
15+
// effectively, so once you are done learning how to create them you will have
16+
// an extensive exercise testing your ability to use them to solve problems!
17+
18+
// In JavaScript, functions follow this general form:
19+
// (parameters are also commonly called arguments)
20+
21+
function name(parameter1, parameter2, parameter3) {
22+
//code to be executed
23+
}
24+
25+
// Now let's see several examples!
26+
27+
// EXAMPLE
28+
// Simple function with no input parameters
29+
function hello(){
30+
console.log("hello world!");
31+
}
32+
33+
// This will just return the function:
34+
hello
35+
36+
// You need to add parenthesis (and any parameters you want) to actually call
37+
// the function into action!
38+
hello()
39+
40+
41+
// EXAMPLE
42+
function helloYou(name){
43+
console.log("hello "+name);
44+
}
45+
helloYou("Jose")
46+
47+
// EXAMPLE
48+
function addNum(num1,num2){
49+
console.log(num1+num2);
50+
}
51+
addNum(30,20)
52+
53+
/////////////////////
54+
// Default values //
55+
///////////////////
56+
57+
58+
//
59+
// Notice that so far we've had to define every single argument in the function
60+
// when using it, but we can also have default values by using an equals sign,
61+
// for example:
62+
63+
// EXAMPLE
64+
function helloSomeone(name="Frankie"){
65+
console.log("Hello "+name);
66+
}
67+
68+
helloSomeone()
69+
///////////////////////
70+
// Returning Values //
71+
/////////////////////
72+
73+
// So far we've only been printing out results, but what if we wanted to return
74+
// the results so that we could assign them to a variable, we can use the return
75+
// keyword for this task in the following manner:
76+
77+
// EXAMPLE
78+
79+
// Without Return
80+
function formal(name="Sam",title="Sir"){
81+
console.log(title+" "+name)
82+
}
83+
84+
//
85+
"Welcome " + formal()
86+
// Welcome undefined
87+
88+
89+
// With a return
90+
function formal(name="Sam",title="Sir"){
91+
return title+" "+name;
92+
}
93+
94+
//
95+
"Welcome "+formal()
96+
//
97+
98+
var result = formal()
99+
100+
////////////////////
101+
// SCOPE //////////
102+
//////////////////
103+
104+
// Scope is the term we use to describe how objects and variable get defined
105+
// within JavaScript. When discussing scope with functions, as a general rule we
106+
// can say that if a variable is defined only inside a function than its scope is
107+
// limited to that function. For example, consider the following function:
108+
109+
// EXAMPLE
110+
111+
// Multiplies input by 5
112+
function times5(numInput) {
113+
var result = numInput * 5
114+
return result
115+
}
116+
117+
times5(5)
118+
119+
// Now after running the function try calling:
120+
numInput // Error
121+
result // Error
122+
123+
// Not defined outside the scope of the function!
124+
125+
// These errors indicate that these variables are only defined inside the scope of
126+
// the function. So variables defined inside of a function are only defined
127+
// (or redefined) inside of that function. However, variables assigned outside of
128+
// the function are global variables, and the function will have access to them
129+
// due to their scope. For example:
130+
131+
var v = "I'm global v"
132+
var stuff = "I'm global stuff"
133+
134+
function fun(stuff){
135+
console.log(v);
136+
stuff = "Reassign stuff inside func";
137+
console.log(stuff);
138+
}
139+
140+
// Now see what happens!
141+
fun()
142+
143+
// So what is happening above? The following happens
144+
// console.log(v); will check for the global variable v, the outer scope
145+
// console.log(stuff); will also check for the global variable stuff
146+
// fun(stuff) will accept an argument stuff, print out v, and then reassign stuff
147+
// (in the scope of the function) and print out stuff.
148+
//
149+
// Notice two things:
150+
//
151+
// The reassignment of stuff only effects the scope
152+
// of the stuff variable inside the function
153+
//
154+
// The fun function first checks to see if v is defined
155+
// at the function scope, if not (which was the case) it
156+
// will then search the global scope for a variable names v,
157+
// leading to it printing out "I'm global v".
158+
159+
// Okay! Now you are ready to tackle some actual exercise problems!
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// FUNCTION EXERCISES - SOLUTIONS
2+
3+
// Functions are an extremely important part of understanding how to program
4+
// and they also allow for an ideal way of testing out your general JavaScript
5+
// knowledge.
6+
7+
// These problem statements are sourced from codingbat.com,
8+
// a great website to practice your code!
9+
10+
// The problems will gradually get harder and harder.
11+
12+
//
13+
// PROBLEM 1: SLEEPING IN
14+
//
15+
// Write a function called sleepIn that takes in two boolean parameters: weekday
16+
// and vacation.
17+
//
18+
// The parameter weekday is true if it is a weekday, and the parameter vacation is
19+
// true if we are on vacation. We sleep in if it is not a weekday or
20+
// we're on vacation. Return true if we sleep in. So some example input and output:
21+
//
22+
// sleepIn(false, false) → true
23+
// sleepIn(true, false) → false
24+
// sleepIn(false, true) → true
25+
26+
function sleepIn(weekday, vacation){
27+
return (!weekday || vacation)
28+
}
29+
30+
31+
32+
//
33+
// PROBLEM 2: MONKEY TROUBLE
34+
//
35+
// We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if
36+
// each is smiling. We are in trouble if they are both smiling or if neither of
37+
// them is smiling. Return true if we are in trouble.
38+
//
39+
// Example Input and Output
40+
//
41+
// monkeyTrouble(true, true) → true
42+
// monkeyTrouble(false, false) → true
43+
// monkeyTrouble(true, false) → false
44+
45+
function monkeyTrouble(aSmile, bSmile) {
46+
return ((aSmile && bSmile) || (!aSmile && !bSmile))
47+
}
48+
49+
50+
//
51+
// PROBLEM 3: STRING TIMES
52+
//
53+
// Given a string and a non-negative int n, return a larger
54+
// string that is n copies of the original string.
55+
//
56+
// Example input and output:
57+
//
58+
// stringTimes("Hi", 2) → "HiHi"
59+
// stringTimes("Hi", 3) → "HiHiHi"
60+
// stringTimes("Hi", 1) → "Hi"
61+
62+
function stringTimes(str, n) {
63+
//Code Goes Here
64+
var i = 0
65+
stringg = ''
66+
while (i<n){
67+
stringg += str
68+
i ++
69+
}
70+
return stringg
71+
}
72+
73+
// PROBLEM 4: LUCKY SUM
74+
75+
// Given 3 numerical values, a b c, return their sum. However, if one of the values is
76+
// 13 then it does not count towards the sum and values to its right do not count.
77+
// So for example, if b is 13, then both b and c do not count.
78+
//
79+
// Hint (Explore using multiple return statements inside a single function!)
80+
//
81+
// Example input and output
82+
//
83+
// luckySum(1, 2, 3) → 6
84+
// luckySum(1, 2, 13) → 3
85+
// luckySum(1, 13, 3) → 1
86+
87+
function luckySum(a, b, c){
88+
if ((a!==13) && (b!==13) && (c!==13)){
89+
return a+b+c
90+
}else if(a===13){
91+
return 0
92+
}else if(b===13){
93+
return a
94+
}else if(c===13){
95+
return a+b
96+
}
97+
//Code Goes Here
98+
}
99+
100+
// PROBLEM 5:
101+
//
102+
// You are driving a little too fast, and a police officer stops you. Write code to
103+
// compute the result, encoded as an int value: 0=no ticket, 1=small ticket,
104+
// 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61
105+
// and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2.
106+
// Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
107+
//
108+
// Here are some example inputs and outputs:
109+
//
110+
// caught_speeding(60, false) → 0
111+
// caught_speeding(65, false) → 1
112+
// caught_speeding(65, true) → 0
113+
114+
function caught_speeding(speed, is_birthday){
115+
if (is_birthday){
116+
if (speed <= 60+5){
117+
return 0
118+
}
119+
else if (61<=speed<=80+5){
120+
return 1
121+
}
122+
return 2
123+
124+
}
125+
else{
126+
if (speed <= 60){
127+
return 0
128+
}
129+
else if (61<=speed<=80){
130+
return 1
131+
}
132+
return 2
133+
134+
}
135+
//Code Goes Here
136+
}
137+
138+
139+
// BONUS: MAKE BRICKS
140+
//
141+
// We want to make a row of bricks that is goal inches long. We have a number of
142+
// small bricks (1 inch each) and big bricks (5 inches each). Return true if it
143+
// is possible to make the goal by choosing from the given bricks. This is a
144+
// little harder than it looks and can be done without any loops in a single line!
145+
//
146+
// If you can't figue this one out, don't worry, that's why its a bonus!
147+
//
148+
// makeBricks(3, 1, 8) → true
149+
// makeBricks(3, 1, 9) → false
150+
// makeBricks(3, 2, 10) → true
151+
152+
function makeBricks(small, big, goal){
153+
return goal%5 >= 0 && goal%5 - small <= 0 && small + 5*big >= goal
154+
}
155+
156+
//Code Goes Here

0 commit comments

Comments
 (0)