Skip to content

Instantly share code, notes, and snippets.

@mmeigooni
Created May 19, 2017 05:20
Show Gist options
  • Save mmeigooni/17d74e232ba3bb81bf914ec88651ebcb to your computer and use it in GitHub Desktop.
Save mmeigooni/17d74e232ba3bb81bf914ec88651ebcb to your computer and use it in GitHub Desktop.
/*
Last time
* Conditionals (rps mini game)
This time
* Functions
* Iteration (for and while loop)
*/
makeTurkeySandwich
One slice of bread.
One slice of turkey.
One slice of bread.
makeTurkeySandwich
makeHamSandwich
makeVeggieSandwich
function makeTurkeySandwich() {
One slice of bread;
One slice of turkey;
One slice of bread;
}
makeThisSandwich _____
One slice of bread.
One slice of _____.
One slice of bread.
function makeThisSandwich(filling) {
One slice of bread;
One slice of filling;
One slice of bread;
}
var scottSandwich = makeThisSandwich('ham');
var items = [];
items.push(scottSandwich);
function makeThisSandwich(filling, breadType) {
return sandwich;
var sandwich = breadType + filling + breadtype;
}
// there is another way to write functions. Google "function expression vs function declaration js" to learn more.
/*
--- Function Rules ---
* ALL functions either modify something or create something new.
* IF it is modifying something, you do not need a return statement in your function
* IF it is creating something new, you must have a return statement in your function
* If a function gets to a return statement, it returns that thing and stops working immediately
*/
function whichRow(studentName) {
if (studentName === 'Andrew') {
return 'Row 1';
} else if (studentName === 'Erica') {
return 'Row 3';
}
}
var result = whichRow('Erica');
console.log(result);
// Problem 1
var names = ['vincent', 'swami', 'ting'];
function addNewName(nameList, nameToAdd) {
// names.push('Erica');
nameList.push(nameToAdd);
}
// inputs: Two inputs. nameList is an Array. nameToAdd is a string.
addNewName(names, 'Erica');
console.log(names);
addNewName(names, 'Ram');
console.log(names);
var anotherName = 'Scott'
addNewName(names, anotherName);
// Problem 2
// Write a function, capitalizeName, that takes in a String as an input and returns a new string with the first letter in it capitalized.
// inputs: a single String, i.e. 'scott'
// outputs: new string that is the old string, but first letter capitalized, i.e. 'Scott'. Needs return statement.
var name = 'scott';
function capitalizeNameAssembler(name) {
// concatenate the capital letter and the remaing string
return capitalizeFirstLetter(name) + everythingButFirstLetter(name)
// return new string
}
function capitalizeFirstLetter(name) {
return name.charAt(0).toUpperCase();
}
var everythingButFirstLetter = function(name) {
return name.slice(1);
}
console.log(capitalizeNameAssembler('scott'));
// test that it works by passing in a sample value and console logging it
// Problem 3
// Write a function, capitalizeNames, that takes in an Array of Strings as an input and modifies each element of the Array so that the first letter of it is capitalized
var names = ['swami', 'ting', 'erica'];
function capitalizeNames(names) {
// your code here
}
capitalizeNames(names);
console.log(names);
/*
Last time
* Functions
* A recipe
* A set of instructions that you want a computer to do
* If your function is creating something new you MUST have a return statement
* If your function is modifying something existing you should NOT have a return function
* Once you hit your first return statement, your function will stop executing
*/
/*
repetition
* while
* for
* recursion (when a function calls itself)
*/
// Prompt: Using a while loop, console log the numbers 1 - 10
var min = 1;
// var max = 10;
while (min <= 10) {
console.log('Your number is ' + min);
min++;
}
// Prompt 1: Using a while loop, console.log all even numbers from 1 - 10 (inclusive)
var min = 1;
while (min <= 10) {
if (min % 2 === 0) {
console.log(min);
}
min++;
}
// Prompt 2: Using a while loop, console.log all odd numbers between 1 - 10 from highest to lowest
/* Prompt 3: Using a while loop, write a function 'fizzBuzz' that takes in two parameters, a start and end point, and prints the numbers, inclusively. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
Sample output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Challenge: Solve by first creating an array with the numbers 0 - 100 as individual elements, then use a forEach to change the individual elements to either Fizz, Buzz, or FizzBuzz
*/
// Prompt 4: Write a function that takes in any number as an argument and returns the sum of its digits
function sumDigits(num) {
}
console.log(sumDigits(4290)); // expect 15
// Hint: use split
// --- For loops ---
// Fundamental difference from while loops:
// * initializion of expression
// * increment operator is included in the parameters
// Prompt: console.log from 1 - 10 using a for loop
for (var min = 0; min <= 10; min++) {
console.log(min);
}
var names = ['Irwing', 'Ting', 'Jessica'];
for (var i = 0; i < names.length; i++) {
console.log(names[i]);
}
// Prompt 5
var firstNames = ['Lars', 'Pejma', 'Kenji'];
var lastNames = ['Buebke', 'Jackson', 'deBoisBlanc'];
// In as few lines of code as possible, generate an array containing all the combinations of first names and last names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment