Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions pset1.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ if invalid input given, return -1
******************/

function myAge( ageNow, numYears ) {

howOld = (ageNow + numYears);
if ((howOld) > 100) {
return NaN
} else if (typeof (howOld) != typeof(1)) {
return -1
} else {
return howOld
}
}


console.log(myAge(28, 5))
console.log(myAge(50, 51))
console.log(myAge("text", 2))

/******************
Concatenate Strings
Expand All @@ -30,10 +39,11 @@ Concatenating string variables
******************/

function myConcatenate( firstStr, secondStr, thirdStr ) {

let mySentence = (firstStr + " " + secondStr + " " + thirdStr);
return mySentence;
}

myConcatenate('I', 'am', 'iron man'); // 'I am iron man'
console.log(myConcatenate('I', 'am', 'iron man')); // 'I am iron man'

/******************
Subtract Function
Expand All @@ -45,9 +55,15 @@ Ensure that both of the inputs are numbers
******************/

function subtract(a,b) {

if (typeof(a) === "number" && typeof(b) === "number") {
return (a - b);
}
}

console.log(typeof(typeof(1)))
console.log(subtract(50,45))
console.log(subtract(45,50))

/******************
Area of A Circle

Expand All @@ -61,10 +77,12 @@ A = π * r2, where is π is Pi and r is the radius squared


function areaOfaCircle(radius){


let areaCircle = (Math.PI * (radius ** 2));
return areaCircle;
}

console.log(areaOfaCircle(5));

/******************
Temperature Converter Fahrenheit to Celsius

Expand All @@ -75,6 +93,11 @@ Now store a fahrenheit temperature into a variable.
Convert it to celsius and output "NN°F is NN°C."
******************/

function fahrenheitToCelsius(fahrenheitTemp) {
let celsiusTemp = ((fahrenheitTemp - 32) / (9/5));
return (`${fahrenheitTemp}°F is ${celsiusTemp}°C.`);
}
console.log(fahrenheitToCelsius(65));

/******************
Temperature Converter Celsius to Fahrenheit
Expand All @@ -86,6 +109,11 @@ Store a celsius temperature into a variable.
Convert it to fahrenheit and output "NN°C is NN°F".
******************/

function celsiusToFahrenheit(celsiusTemp) {
let fahrenheitTemp = ((celsiusTemp * (9/5)) + 32);
return (`${celsiusTemp}°C is ${fahrenheitTemp}°F.`);
}
console.log(celsiusToFahrenheit(18.33));

/******************
Is it the weekend?
Expand Down Expand Up @@ -116,6 +144,17 @@ The absolute value of a negative number is the positive version of that same num
and the absolute value of a positive number (or zero) is that number itself.
******************/

function absolute(aNumber){
if (aNumber >= 0) {
return aNumber;
} else {
return (aNumber * -1);
}
}

console.log(absolute(321));
console.log(absolute(-321));
console.log(absolute(0));

/******************
Create a function that counts the number of characters in your name
Expand All @@ -128,4 +167,15 @@ return the number of characters in the string
call function 'countChars'
******************/

function countChars(nameString) {
// return toString(nameString)
if (typeof(nameString) != "string") {
return ((String(nameString)).length);
} else {
return ((nameString).length);
}
}

console.log(countChars("Vincent"));
console.log(countChars(123456));
console.log(typeof(String))
102 changes: 78 additions & 24 deletions pset4_scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@
const foo = 1;
function run() {
const foo = 2;
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 2 because it is declared in a new context
}

console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 1 because foo is declared as 1 in the global environment
run();
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is still 1 because the 'foo' in the 'run' function environment is destroyed
*/


Expand All @@ -92,7 +92,7 @@
}

run();
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is undefined as it was only declared in the run() function environment
*/

/*
Expand All @@ -102,15 +102,15 @@

const foo = 1;
function run() {
const foo = 2;
const foo = 2;
function _inner() {
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 2. function _inner() looks at the environment above it for a reference to 'foo'
}
_inner();
}

run();
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 1. the declared "foo" in the function run() environment is destroyed
*/

/*
Expand All @@ -122,13 +122,13 @@
function run() {
foo = 2;
function _inner() {
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 2. foo is declared in the global environment and subsequently redefined in the run() function environment. function _inner is able to check the value of foo in the global context
}
_inner();
}

run();
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? foo is 2. see above
*/

/*
Expand All @@ -141,13 +141,13 @@
const foo = 2;
function _inner() {
const foo = 3;
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? foo is 3 because foo has been defined in the _inner() function environment as 3, and then console.log() function is called in the same environment
}
_inner();
}

run();
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 1. it is defined in the global environment as 1 and any declarations within inner functions have no bearing on it's value
*/

/*
Expand All @@ -158,14 +158,14 @@
let foo = 1;
function run() {
function _inner() {
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 1 because it was declared in the global environment as 1. Another "foo" variable was not declared elsewhere
foo = 10;
}
_inner();
}

run();
console.log(foo); // what is foo? why?
console.log(foo); // what is foo? why? ### foo is 10 because a value of 10 was reassigned to it in function _inner(). foo was not declared in any other environments
*/

/*
Expand All @@ -190,19 +190,40 @@

Given the following code:

let foo = 1;
function run( anotherFunctionToCall ) {
const foo = 9;
anotherFunctionToCall();
console.log(foo)
}

run(function() {
foo = 2;
});
console.log(foo); // what is foo? why?
// let foo = 1;
// function run( anotherFunctionToCall ) {

// anotherFunctionToCall();
// console.log(foo)
// }

// run(function() {
// foo = 2;
// });
// console.log(foo); // what is foo? why?
*/
// let foo = 1;
// function run( anotherFunctionToCall ) {
// const foo = 9;
// anotherFunctionToCall();
// console.log(foo)
// }

// run(function() {
// foo = 2;
// });
// console.log(foo); // what is foo? why?

// let b = 3
// function whatever(){
// let a = 1
// function hey(){
// b = a * b
// }
// }

// whatever()
// console.log(b);
/*
PROBLEM 12:

Expand All @@ -213,15 +234,48 @@
foo = 9;
anotherFunctionToCall();
console.log(foo)
}ROBLEM 11:

Given the following code:

let foo = 1;
function run( anotherFunctionToCall ) {
const foo = 9;
anotherFunctionToCall();
console.log(foo)
}



run(function() {
foo = 2;
});
console.log(foo); // what is foo? why?
*/

// let foo = 1;
// function run( anotherFunctionToCall ) {
// foo = 9;
// anotherFunctionToCall();
// console.log(foo)
// }

// run(function() {
// foo = 2;
// });
// console.log(foo); // what is foo? why?

let foo = 1;
function run( anotherFunctionToCall ) {
const foo = 9
anotherFunctionToCall();
console.log(foo)
}

run(function() {
foo = 2;
});
console.log(foo); // what is foo? why?



Expand Down
54 changes: 54 additions & 0 deletions pset5_declarations_vs_expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
return the remainder of the sum of a,b,c divided by 3
*/

function prob1_decl(a,b,c){
return (a + b + c) % 3;
}
console.log(prob1_decl(1,2,4));


const prob1_named = function prob1_named(a,b,c){
return (a + b + c) % 3;
}
console.log(prob1_named(1,2,4));


const prob1_fat = (a,b,c) => {
return (a + b + c) % 3;
}
console.log(prob1_fat(1,2,4));

/*
PROBLEM 2:
Expand All @@ -24,9 +40,47 @@
then function will return 'one-two-three'
*/

function prob2_decl(a,b,c){
// return (a+"-"+b+"-"+c);
return (`${a}-${b}-${c}`)
}
console.log(prob2_decl('one', 'two', 'three'));


const prob2_named = function prob2_named(a,b,c){
return (`${a}-${b}-${c}`)
}
console.log(prob2_named('one', 'two', 'three'));


const prob2_fat = (a,b,c) =>{
return (`${a}-${b}-${c}`)
}
console.log(prob2_fat('one', 'two', 'three'));

/*
PROBLEM 3:
Write a function that takes NO params
return a random number between 0 and 10
(doesn't have to be a whole number tho)
*/

function prob3_decl(){
randomNum = Math.random();
return randomNum * 10;
}
console.log(prob3_decl());


const prob3_named = function prob3_named(){
randomNum = Math.random();
return randomNum * 10;
}
console.log(prob3_named());


const prob3_fat = () => {
randomNum = Math.random();
return randomNum * 10;
}
console.log(prob3_fat());