Skip to content

Commit

Permalink
Chapter 2 Fundamentals
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsosborne committed Aug 14, 2017
1 parent 6dc1083 commit 2fa9aae
Show file tree
Hide file tree
Showing 28 changed files with 97 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions Chapter 2 - Fundamentals/factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Just the Facts, ma'am. Factoria;s, this is. Write a function factorial(num) that, given a number, returns the product (multiplication) of all positive integers from 1 up to number (inclusive). FOr example, facotiral(3) = 6(or 1 * 2 * 3); factorial(5) = 120 (or 1 * 2 * 3 * 4 * 5).

function factorial(num){
var val = 1;
var i = 2;
while(i <= num){
val *= i
i++;
}
console.log(val);
}

factorial(1);
factorial(2);
factorial(3);
factorial(5);
13 changes: 13 additions & 0 deletions Chapter 2 - Fundamentals/sigma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Implement function sigma(num) that given a number, returns the sum of all positive integers up to number (inclusive). Ex.: signma(3) = 6 (or 1 + 2 + 3); sigma(5) = 15 (or 1 + 2 + 3 + 4 + 5).

function sigma(num){
var value = 0;
var i = 1;
while(i <= num){
value += i
i++;
}
console.log(value);
}

sigma(3);
68 changes: 68 additions & 0 deletions Chapter 2 - Fundamentals/starArt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Assume that you have a text field that is exactly 75 characters long. You want to fill it with spaces and astericks ('*'), sometimes called stars. You should print the given numbers of astericks consecutively. Depending on which function is called, those stars should be left-justified (first star would be very first char in the text field), or right-justified (last star would be very last char in the text field, with potentially some number of spaces at beginning of text field before the block of stars start), or centered in the 75-character text field (with same number of spaces on either side of the block of stars, plus/minus one).
* write a function drawLeftStars(num) that accepts a number and prints that many astericks.
* write a function drawRightStars(num) that prints 75 characters total. Stars should build from right side. That num characters should be astericks, the other 75 should be spaces.
* write a function drawCenteredStars(num) that prints 75 characters total. Stars should be centered in the 75. The middle num characters should be astericks; the rest of the 75 spaces.
*/

function drawLeftStars(num, char){
if(num <= char){
var diff = char - num;
var symbols = "";
for ( var i = 1; i <= num; i++){
symbols += "*";
}
for (var idx = 1; idx <= diff; idx++){
symbols += " ";
}
console.log(symbols);
}
else{
console.log("num exceeds char")
}

}
drawLeftStars(4, 5);

function drawRightStars(num, char){
if(num <= char){
var diff = char - num;
var symbols = "";
for ( var i = 1; i <= num; i++){
symbols += "-";
}
for (var idx = 1; idx <= diff; idx++){
symbols += "*";
}
console.log(symbols);
}
else{
console.log("num exceeds char")
}

}
drawRightStars(4,5);

function drawCenteredStars(num, char){
if(num <= char){
var symbols = "";
var side = ((char - num)/2);
for (var i = 1; i <= char; i++){
if(i <= side){
symbols += "-";
}
else if(i > side && i <= num+side){
symbols += "*";
}
else if(i > num+side){
symbols += "-";
}
}
console.log(symbols);
}
}

drawCenteredStars(5, 75);

0 comments on commit 2fa9aae

Please sign in to comment.