${content}/div>`;
+}
+console.log(addClasses('foo', 'bar', 'stuff'));
/*
@function duplicate
@param {string} start
@@ -58,4 +73,7 @@
@example duplicate('hello!'); // hello!hello!
*/
-
+const duplicate = start => {
+ return start.concat(start);
+}
+console.log(duplicate('hello!'));
\ No newline at end of file
diff --git a/pset3_default_params.js b/pset3_default_params.js
index 9342519..d4c4bc4 100644
--- a/pset3_default_params.js
+++ b/pset3_default_params.js
@@ -10,7 +10,11 @@
use a default parameter for the middleName, set it to ""
*/
-
+function getFullName(lastName, firstName, middleName = "") {
+let getFullName = `${lastName}, ${firstName} ${middleName}`;
+return getFullName;
+}
+console.log(getFullName("Igwe", "Osita"));
/*
PROBLEM 2:
@@ -21,7 +25,12 @@
Take a param that represents F. By default this should be 32
*/
-
+function fahrenheitToCelsius(F = 32){
+ const C = ((F-32) * (5/9));
+ const fahrenheitToCelsius = `${F}°F is ${C}°C.`;
+ return fahrenheitToCelsius;
+}
+console.log(fahrenheitToCelsius(70));
/*
PROBLEM 3:
@@ -33,7 +42,13 @@
should return a number that tells you how old you'll be in numYears
ageNow and numYears should have default params
*/
-
+function myAge(ageNow = 26, numYears = 32) {
+ const futureAge = `In ${numYears} years, I will be ${numYears+ageNow}.`;
+
+ return futureAge;
+ }
+ const futureAge1 = myAge();
+ console.log(futureAge1);
/*
PROBLEM 4:
Now, let's do something interesint.
@@ -60,12 +75,41 @@
*/
+ function missingParameter(paramName) {
+ console.log(`you are missing parameter ${paramName}`);
+ return 0;
+ }
+function addTwoNums (a= missingParameter('a'), b= missingParameter('b')) {
+return a+b;
+}
+console.log(addTwoNums());
+/* addtwonums runs - it figures out if there is are parameters for both imputs, if there are any missing inputs
+then it invokes the missing parameter function - the missingparam functions prints the message and returns 0 so that the addvalue function
+can return a number */
+/*function missingParameter(paramName) {
+ throw new Error(`you are missing ${paramName}.`);
+ }
+function addTwoNums (a= missingParameter('a'), b= missingParameter('b')) {
+return a+b;
+}
+console.log(addTwoNums());
-
-
-
-
+*/
+const subtract = function(a, b){
+ return a - b;
+} // this is called a function expression\
+console.log(subtract()); //
+
+const hello = 5;
+hello(3); // this does not work because hello is a number (5) - can't invoke a number
+subtract(5); //this can be invoked because subtract is a function - can invoke a function
+
+
+function countChars(nameString = "Jane Doe") {
+ return nameString.length
+}
+console.log(countChars());
\ No newline at end of file
diff --git a/pset4_scope.js b/pset4_scope.js
index adc7abb..f472f2b 100644
--- a/pset4_scope.js
+++ b/pset4_scope.js
@@ -8,218 +8,215 @@
/*
PROBLEM 1:
- Given the following code:
+ Given the following code:*/
- const foo = 1;
+ const foo = 1; //global
function run() {
- const foo = 2;
+ const foo = 2; //local
}
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - global = 1*/
/*
PROBLEM 2:
- Given the following code:
+ Given the following code:*/
function run() {
- const foo = 2;
+ const foo = 2; //local
}
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - undefined because there is not foo variable in the global scope
+
/*
PROBLEM 3:
- Given the following code:
+ Given the following code:*/
- const foo = 1;
+ const foo = 1; //global
function run() {
- console.log(foo); // what is foo? why?
+ console.log(foo); // what is foo? why? - global = 1
}
run();
-*/
+
/*
PROBLEM 4:
- Given the following code:
+ Given the following code:*/
- let foo = 1;
+ let foo = 1; //global
function run() {
- foo = 2;
+ foo = 2; //local
}
- console.log(foo); // what is foo? why?
+ console.log(foo); // what is foo? why? //global = 1
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? // local = 2
+
/*
PROBLEM 5:
- Given the following code:
+ Given the following code:*/
- const foo = 1;
+ const foo = 1; //gloabl
function run() {
- const foo = 2;
- console.log(foo); // what is foo? why?
+ const foo = 2; //local
+ console.log(foo); // what is foo? why? //local = 2
}
- console.log(foo); // what is foo? why?
+ console.log(foo); // what is foo? why? //global = 1
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? // global = 1
+
/*
PROBLEM 6:
- Given the following code:
+ Given the following code:*/
function run() {
- const foo = 1;
+ const foo = 1; //local
function _inner() {
- console.log(foo); // what is foo? why?
+ console.log(foo); // what is foo? why? //local = 1
}
_inner();
}
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - undefined
+
/*
PROBLEM 7:
- Given the following code:
+ Given the following code:*/
- const foo = 1;
+ const foo = 1; //gloabl
function run() {
- const foo = 2;
+ const foo = 2; //local
function _inner() {
- console.log(foo); // what is foo? why?
+ console.log(foo); // what is foo? why? //local - 2
}
_inner();
}
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - global 1
/*
PROBLEM 8:
- Given the following code:
+ Given the following code:*/
- let foo = 1;
+ let foo = 1; //global
function run() {
- foo = 2;
+ foo = 2; //global = 2
function _inner() {
- console.log(foo); // what is foo? why?
+ console.log(foo); // what is foo? why? // global 2
}
_inner();
}
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? //global 2
+
/*
PROBLEM 9:
- Given the following code:
+ Given the following code:*/
- const foo = 1;
+ const foo = 1; //global
function run() {
- const foo = 2;
+ const foo = 2; //local
function _inner() {
- const foo = 3;
- console.log(foo); // what is foo? why?
+ const foo = 3; //local
+ console.log(foo); // what is foo? why? //local = 3
}
_inner();
}
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - global = 1
+
/*
PROBLEM 9:
- Given the following code:
+ Given the following code:*/
- let foo = 1;
+ let foo = 1; //global
function run() {
function _inner() {
- console.log(foo); // what is foo? why?
+ console.log(foo); // what is foo? why? //local = 10
foo = 10;
}
_inner();
}
run();
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? //global = 1
+
/*
PROBLEM 10:
- Given the following code:
+ Given the following code:*/
let foo = 1;
function run( anotherFunctionToCall ) {
anotherFunctionToCall();
- console.log(foo)
+ console.log(foo) //local = 2
}
run(function() {
foo = 2;
});
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - local = 2
/*
PROBLEM 11:
- Given the following code:
+ Given the following code:*/
- let foo = 1;
+ let foo = 1; //global
function run( anotherFunctionToCall ) {
const foo = 9;
anotherFunctionToCall();
- console.log(foo)
+ console.log(foo) //local 9
}
run(function() {
foo = 2;
});
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - global 2
+
/*
PROBLEM 12:
- Given the following code:
+ Given the following code:*/
let foo = 1;
function run( anotherFunctionToCall ) {
foo = 9;
anotherFunctionToCall();
- console.log(foo)
+ console.log(foo) //global 2
}
run(function() {
foo = 2;
});
- console.log(foo); // what is foo? why?
-*/
+ console.log(foo); // what is foo? why? - global 2
+
diff --git a/pset5_declarations_vs_expressions.js b/pset5_declarations_vs_expressions.js
index a8253d7..181a5df 100644
--- a/pset5_declarations_vs_expressions.js
+++ b/pset5_declarations_vs_expressions.js
@@ -14,6 +14,26 @@
return the remainder of the sum of a,b,c divided by 3
*/
+//DECLARATION
+function remainder(a, b, c) {
+ let sum = a + b + c;
+ return sum % 3;
+}
+console.log(remainder(1, 2, 3));
+
+//EXPRESSION - anonymous function
+const remainder = function(a, b, c) {
+ let sum = a + b + c;
+ return sum % 3;
+}
+console.log(remainder(1, 2, 3));
+
+//ARROW - anonymous function
+const remainder = (a, b, c) => {
+ let sum = a + b + c;
+ return sum % 3;
+}
+console.log(remainder(1, 2, 3));
/*
PROBLEM 2:
@@ -21,12 +41,43 @@
return a string that addes a dash in between them.
so, if a = 'one', b = 'two', c='three'
- then function will return 'one-two-three'
-*/
+ then function will return 'one-two-three'*/
-/*
- PROBLEM 3:
+//DECLARTION
+ function string(a, b, c) {
+ return `${a}-${b}-${c}`;
+ }
+ console.log(string("one", "two", "three"));
+//EXPRESSION
+ const string = function(a, b, c) {
+ return `${a}-${b}-${c}`;
+ }
+ console.log(string("one", "two", "three"));
+//ARROW
+ const string = (a, b, c) => {
+ return `${a}-${b}-${c}`;
+ }
+ console.log(string("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)
*/
+//DECLARTION
+ function randomNum() {
+ return Math.random() * 11;
+ }
+ console.log(randomNum());
+//EXPRESSION
+ const randomNum = function() {
+ return Math.random() * 11;
+ }
+ console.log(randomNum());
+//ARROW
+ const randomNum = () => {
+ return Math.random() * 11;
+ }
+ console.log(randomNum());
+
diff --git a/pset6.js b/pset6.js
index ea1f2f2..cb0e6d9 100644
--- a/pset6.js
+++ b/pset6.js
@@ -8,7 +8,11 @@
@example addTwoNumbers(1,2) // 3
@example addTwoNumbers(1) // 1
*/
-
+const addTwoNumbers = (a=0,b=0) =>{
+ return a+b;
+}
+console.log(addTwoNumbers(1,2));
+console.log(addTwoNumbers(1));
/*
@func turnNumberToString
@@ -19,6 +23,17 @@
@example turnNumberToString(1); // "1"
@example turnNumberToString("2"); // "2"
*/
+const turnNumberToString = (a) => {
+ if(typeof a === 'number'){
+ return `${a}`;
+ }
+ else if(typeof a === 'string'){
+ return a;
+ }
+ throw new Error('please insert a number');
+}
+console.log(turnNumberToString(1));
+console.log(turnNumberToString("2"));
/*
@func fullName
@@ -30,7 +45,17 @@
@example fullName('Taq', 'Karim'); // "Taq Karim"
@example fullName('Foo'); // "Foo"
*/
-
+const fullName = (firstName, lastName) => {
+ if (typeof firstName === 'string' && typeof lastName === 'undefined') {
+ return firstName;
+ }
+ else if (typeof firstName === 'string' && typeof lastName === 'string'){
+ return firstName.concat(' ', lastName);
+ }
+ throw new Error('please insert a firstName and lastName, as strings')
+}
+console.log(fullName('Taq', 'Karim'));
+console.log(fullName('Foo'));
/*
@func fullNameSentence
@param {string} firstName
@@ -45,7 +70,10 @@
// expect: "John Smith is awesome"
fullNameSentence('John', 'Smith', 'is awesome');
*/
-
+const fullNameSentence = (firstName, lastName, restOfSentence) => {
+ return fullName(firstName,lastName).concat(' ', restOfSentence);
+}
+console.log(fullNameSentence('John', 'Smith', 'is awesome'));
/*
@func fullNameSentenceWithChecks
@param {string} firstName
@@ -65,7 +93,17 @@
// expect: 'Required variables are not set!'
fullNameSentenceWithChecks('John', 'Smith');
*/
+const isRequired = () => {
+ throw new Error('param is required');
+}
+
+const fullNameSentenceWithChecks = (firstName, lastName = isRequired(), restOfSentence = isRequired()) => {
+ return fullName(firstName, lastName).concat(' ', restOfSentence );
+}
+
+
+console.log(fullNameSentence('John', 'Smith', 'is awesome'));
/*
@func fToC
@param {number} f
@@ -77,6 +115,16 @@
@example fToC(); // 0
*/
+const fToC = (number) => {
+ if (number === undefined) {
+ return 0;
+ }
+ let celsius = (number - 32) * 5/9;
+ return celsius;
+}
+console.log(fToC( 32 ));
+console.log(fToC( 212 ));
+console.log(fToC());
/*
@func fToKelvin
@@ -94,6 +142,16 @@
@example fToC(); // 273.15
*/
+const fToKelvin = (number) => {
+let kelvin = number + 273.15;
+return kelvin;
+}
+console.log('---',fToKelvin(fToC( 32 )));
+console.log('---',fToKelvin(fToC( 212 )));
+console.log('---',fToKelvin(fToC()));
+
+
+
/*
@func fToKelvinWithChecks
@param {number} f
@@ -111,4 +169,13 @@
@example fToC(); // "ERROR: variable 'f' is not set"
*/
+const fToKelvinWithChecks = (number) => {
+ if (typeof number === 'undefined'){
+ throw new Error("ERROR: variable 'f' is not set");
+ }
+ return fToKelvin(fToC(number));
+}
+console.log(fToKelvinWithChecks(32));
+console.log(fToKelvinWithChecks(212));
+console.log(fToKelvinWithChecks());