2週目は3つコースが用意されている。とりあえず一つ目をこなした。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Introduction | |
var hello = function () { | |
// Print hello on the console. | |
console.log("i am saying hello"); | |
}; | |
hello(); | |
hello(); | |
// 2. Function | |
var hi = function () { | |
console.log("hi"); | |
}; | |
hi(); | |
// 3. My first function | |
var myFirstFunction = function() { | |
console.log('Manabu'); | |
}; | |
myFirstFunction(); | |
// 4. Fix me ! | |
var fullName = ""; | |
var name; | |
var firstLetter; | |
/* | |
fixName function definition should go here. | |
*/ | |
var fixName = function(){ | |
firstLetter = name.substring(0, 1); | |
name = firstLetter.toUpperCase() + name.substring(1); | |
fullName = fullName + " " + name; | |
}; | |
name = prompt("Enter your first name (all in lower case):"); | |
fixName(); | |
name = prompt("Enter your second name (all in lower case):"); | |
fixName(); | |
console.log("And your full name is:" + fullName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Ahoy matey! | |
var greeting = "Ahoy"; | |
// 2. Thou shalt greet | |
var greeting = "Ahoy"; | |
var greet = function() { | |
console.log(greeting); | |
}; | |
greet(); | |
// 3. Domesticate | |
var greeting = "Ahoy"; | |
var greet = function () { | |
// Local greeting variable. | |
var greeting = "Hello"; | |
console.log(greeting); | |
}; | |
greet(); | |
// 4. Sometimes change is bad | |
var greeting = "Ahoy"; | |
var greet = function () { | |
// Local greeting variable. | |
var greeting = "Hello"; | |
console.log(greeting); | |
}; | |
greet(); | |
console.log(greeting); | |
// 5. Parameters | |
var sayHelloTo = function (name) { | |
console.log("Hello " + name); | |
}; | |
sayHelloTo("Amjad"); | |
sayHelloTo("Manabu"); | |
// 6. Recap | |
/* Start global variable `greeting` scope */ | |
var greeting = "Ahoy"; | |
var greet = function () { | |
/* Start local variable `greeting` scope */ | |
var greeting = "Hello"; | |
/* Start local variable `myName` scope */ | |
var myName = prompt("Input your name: "); | |
console.log(greeting + " " + myName); | |
/* End local variables `myName` and `greeting` scope */ | |
}; | |
greet(); | |
// This will produce a `ReferenceError` since we are out | |
// of the `myName` variable scope. | |
console.log(myName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Introduction | |
var identity = function (x) { | |
return x; | |
}; | |
identity(999); | |
// 2. Square | |
var square = function (x) { | |
return x * x; | |
}; | |
// 3. More on return | |
var identity = function (x) { | |
return x; | |
}; | |
// 4. Branch and return | |
var isEven = function (n) { | |
if (n % 2 === 0) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
console.log(isEven(1)); | |
console.log(isEven(2)); | |
console.log(isEven(999)); | |
// 5. The lost numbers | |
var lost = [4, 8, 15, 16, 23, 42]; | |
var count = lost.length; | |
var isLost = function (n) { | |
for ( var i = 0; i < count; i++ ) { | |
if ( n === lost[i]) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
if ( isLost(12) ) { | |
console.log('12 is a lost number'); | |
} | |
if ( isLost(16) ) { | |
console.log('16 is a lost number'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Function calls as values | |
// Define quarter here. | |
var quarter = function( x ) { | |
return x / 4; | |
}; | |
if (quarter(4) === 1) { | |
console.log("The statement is true."); | |
} else { | |
console.log("The statement is false."); | |
} | |
// 2. Cube | |
var cube = function( n ) { | |
return n * n * n; | |
}; | |
// 3. Squares n'cubes | |
var square = function (x) { | |
return x * x; | |
}; | |
var cube = function (x) { | |
return square(x) * x; | |
}; | |
// 4. What not | |
var isMultipleOfThree = function (x) { | |
return x % 3 === 0; | |
}; | |
var isNotMultipleOfThree = function (x) { | |
return !isMultipleOfThree(x); | |
}; | |
// 5. Even is not odd | |
var isOdd = function( x ) { | |
return x % 2 != 0; | |
}; | |
var isEven = function( x ) { | |
return !isOdd(x); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Let there be more | |
var area = function (w, l) { | |
// We should return the product of w and l. | |
return w * l; | |
}; | |
// 2. Divisibility | |
var isDivisible = function (x, y) { | |
return x % y === 0; | |
}; | |
// 3. Power | |
var power = function (base, exponent) { | |
var result = 1; | |
for (var i = 0; i < exponent; i++) { | |
result = result * base; | |
} | |
return result; | |
}; | |
power(2, 2); | |
// 4. Recursive: Part 1 | |
var power = function( base, exponent ){ | |
}; | |
// 5. Recursive: Part 2 | |
var power = function( base, exponent ){ | |
if(0 === exponent) { | |
return 1; | |
} | |
}; | |
// 6. Recursive: Part 3 | |
var power = function( base, exponent ){ | |
if(0 === exponent) { | |
return 1; | |
} | |
return base * power(base, exponent -1); | |
}; | |
// 7. Recursive: Part 4 | |
var power = function( base, exponent ){ | |
if(0 === exponent) { | |
return 1; | |
} | |
return base * power(base, exponent -1); | |
}; | |
console.log(power(2, 4) === 16); | |
console.log(power(2, 0) === 1); | |
console.log(power(3, 3) === 27); |
0 件のコメント:
コメントを投稿