Skip to content

Instantly share code, notes, and snippets.

@erottman
Created December 22, 2016 18:39
Show Gist options
  • Save erottman/2f47b0b9ff4c3a05ce2d04c4829672f6 to your computer and use it in GitHub Desktop.
Save erottman/2f47b0b9ff4c3a05ce2d04c4829672f6 to your computer and use it in GitHub Desktop.
Building a Function: An accumulator pattern refers to a function that follow these steps:
1. Identify the inputs.
1a. Array: [], string "", object: {}, number: 0, bollean: true/false
2. Write a function definition with those inputs.
2a. define parameters ()
2b. function call/invoking: function(arguments)
3. Identify the output, define the result variable, and return it.
3a. Declare output type: result; default , result = [] array, result = {} object, result = 0 num result = "" string
3b. return statement - return result (one picked from above)
4. Determine how to iterate.
4a. Which loop - for, while, while/do, for -in (Objects)
5. Changes the data somehow. This is the trickey part
5a. Do you need conditionals(if/elseif/else) - comparisions
5b. Modelo %: Do we need even/odd/remainder - even (num % 2 === 0) , odd(num %2 !== 0)
5b. Add: result += (array[i]) (num) (string[i]) - also consider *= /=
5c. Array: result.push(array[i]) - also consider slice
5d. Object: result[object[key] = [i]
5e. String: result = result + items[i] concatenation
// 16. Write a conditional statement to find the largest of the numbers in the array provided.
var num = [-5, -2, -6, 0, -1];
var largestNum = num[0];
for (var i = 1; i < num.length; i++) {
if (num[i] > largestNum) {
largestNum = num[i];
}
}
// 9. FIZZ BUZZ
// Write a program that uses console.log to print each number up to 100, with a couple exceptions...
// If the number is divisible by 3, print "Fizz" instead of the number.
// If the number is divisible by 5, print "Buzz" instead of the number.
// If the number is divisible by both 3 and 5, print "FizzBuzz" instead of the number.
for (var i = 0; i < 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
document.getElementById("q9").innerHTML += "<p>FizzBuzz</p>";
} else if (i % 3 === 0 && !(i % 5 === 0)) {
console.log("Fizz");
document.getElementById("q9").innerHTML += "<p>Fizz</p>";
} else if (i % 5 === 0 && !(i % 3 === 0)) {
console.log("Buzz");
document.getElementById("q9").innerHTML += "<p>Buzz</p>";
} else {
console.log(i);
document.getElementById("q9").innerHTML += "<p>" + i + "</p>";
}
// HEADS OR TAILS?
// 17. Use the following variable for your coin flip action:
var coin = Math.floor(Math.random() * 2);
if (coin === 0) {
var result = "heads";
} else {
var result = "tails";
}
console.log(result);
document.getElementById("q17").innerHTML += "<p>" + result + "</p>";
// 5. You use Amazon Subscribe & Save to have six cans of cashews automatically sent to you each month.
// Write a function that takes the price per unit and calculates the total for you each month.
// Since the price of the cashews can change anytime, pass that amount into the function to get your total price.
function totalCost(price, amount) {
return (price * amount);
}
totalCost(input1, input2));
// 1. Without using Math.min(), write a function called minimum(),
// that takes two numbers from a user,
// and outputs the smaller number into the HTML page.
function minimum (num1, num2) {
if (num1 === num2) {
return("same number");
} else if (num1 < num2) {
return(num1);
} else if (num1 > num2) {
return(num2);
} else {
return("incorrect input");
}
}
var exampleArray = [1, 2, 3, 4];
function sum(array) {
var result = 0;
for (var i = 0; i < array.length; i++) {
result += array[i];
}
return result;
}
/ 8. Write a function countBs() that takes a string as its only argument and returns
// a number that indicates how many uppercase “B” characters are in the string.
// HINT: Google charAt()
function countBs(string) {
var count = 0;
for (var i = 0; i < string.length; i++) {
if (string.charAt(i) === "B");
count++;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment