diff --git a/pset1.js b/pset1.js index 9261c6d..e09e910 100644 --- a/pset1.js +++ b/pset1.js @@ -9,11 +9,34 @@ if that number is > 100, should return NaN if invalid input given, return -1 ******************/ -function myAge( ageNow, numYears ) { - +function myAge(ageNow = 26, numYears = 10) { + let sum = ageNow + numYears; + if (sum < 100) { + let string = `In ${numYears} years, I will be ${sum}`; + return string; + } + else if (sum > 100) { + return NaN; + } + else if ((typeof ageNow !== 'number') || (typeof numYears !== 'number')){ + return -1; } +} +console.log(myAge()); +const myAge = function(ageNow, numYears) { + let sum = ageNow + numYears; + let string = `In ${numYears} years, I will be ${sum}`; + return string; +} +console.log(myAge()); +const myAge = (ageNow, numYears) => { + let sum = ageNow + numYears; + let string = `In ${numYears} years, I will be ${sum}`; + return string; +} +console.log(myAge()); /****************** Concatenate Strings @@ -29,11 +52,24 @@ Concatenating string variables - Call myConcatenate function ******************/ -function myConcatenate( firstStr, secondStr, thirdStr ) { - +function myConcatenate( firstStr = 'I', secondStr = "am", thirdStr = "iron man" ) { + let mySentence = `${firstStr} ${secondStr} ${thirdStr}`; + return mySentence; +} +console.log(myConcatenate()); + +const myConcatenate = function(firstStr, secondStr, thirdStr) { + let mySentence = `${firstStr} ${secondStr} ${thirdStr}`; + return mySentence; } +console.log(myConcatenate()); -myConcatenate('I', 'am', 'iron man'); // 'I am iron man' +const myConcatenate = (firstStr, secondStr, thirdStr) => { + let mySentence = `${firstStr} ${secondStr} ${thirdStr}`; + return mySentence; +} +console.log(myConcatenate()); +//myConcatenate('I', 'am', 'iron man'); // 'I am iron man'*/ /****************** Subtract Function @@ -44,10 +80,18 @@ This function will take two numbers and subtract them Ensure that both of the inputs are numbers ******************/ -function subtract(a,b) { - +function subtract(a = 0, b = 0) { + return a - b; +} +console.log(subtract()); + +const subtract = function(a = 0, b = 0) { + return a - b; } + console.log(subtract()); +const subtract = (a = 0, b = 0) => a - b; +console.log(subtract()); /****************** Area of A Circle @@ -60,11 +104,20 @@ A = π * r2, where is π is Pi and r is the radius squared ******************/ -function areaOfaCircle(radius){ +function areaOfaCircle(radius = 1){ + return Math.PI * (radius ** 2); +} +console.log(areaOfaCircle); + +const area = function(radius = 1) { + return Math.PI * (radius ** 2); } +console.log(areaOfaCircle); +const area = (radius = 1) => Math.PI * (radius ** 2); +console.log(area()); /****************** Temperature Converter Fahrenheit to Celsius @@ -74,7 +127,28 @@ Now store a fahrenheit temperature into a variable. Convert it to celsius and output "NN°F is NN°C." ******************/ +function fahrenheitToCelsius(F = 32){ + const C = ((F-32) * (5/9)); + const fahrenheitToCelsius = `${F}°F is ${C}°C.`; + return fahrenheitToCelsius; +} +console.log(fahrenheitToCelsius()); + +const fahrenheitToCelsius = function(F = 32) { + const C = ((F-32) * (5/9)); + const fahrenheitToCelsius = `${F}°F is ${C}°C.`; + return fahrenheitToCelsius; +} +console.log(fahrenheitToCelsius()); +const fahrenheitToCelsius = (F = 32) => { + const C = ((F-32) * (5/9)); + const fahrenheitToCelsius = `${F}°F is ${C}°C.`; + return fahrenheitToCelsius; +} +console.log(fahrenheitToCelsius()); + +/* /****************** Temperature Converter Celsius to Fahrenheit @@ -85,7 +159,26 @@ Store a celsius temperature into a variable. Convert it to fahrenheit and output "NN°C is NN°F". ******************/ +function celsiusToFahrenheit(C = 0){ + let F = (C * 9/5) + 32; + const celsiusToFahrenheit = `${C}°C is ${F}°F.`; + return celsiusToFahrenheit; +} +console.log(celsiusToFahrenheit()); + +const celsiusToFahrenheit = function(C = 0) { + let F = (C * 9/5) + 32; + const celsiusToFahrenheit = `${C}°C is ${F}°F.`; + return celsiusToFahrenheit; +} +console.log(celsiusToFahrenheit()); +const celsiusToFahrenheit = (C = 0) => { + let F = (C * 9/5) + 32; + const celsiusToFahrenheit = `${C}°C is ${F}°F.`; + return celsiusToFahrenheit; +} +console.log(celsiusToFahrenheit()); /****************** Is it the weekend? @@ -103,7 +196,57 @@ console.log(today); // No, it's the weekday If you are having trouble, please note that Javascript has a helpful built-in function to help get the current day ******************/ +function isItTheWeekend(day) { + + if (day === "Saturday") { + return 'Yes, it\'s the weekend'; + } + + else if (day === "Sunday") { + return 'Yes, it\'s the weekend'; + } + else { + return 'No, it\'s the weekday'; + } + + } + + console.log(today("Monday")); + +const today = (day) => { + + if (day === "Saturday") { + return 'Yes, it\'s the weekend'; + } + + else if (day === "Sunday") { + return 'Yes, it\'s the weekend'; + } + else { + return 'No, it\'s the weekday'; + } + + } + + console.log(today("Monday")); + + +const today = function isItTheWeekend(day) { + if (day === "Saturday") { + return 'Yes, it\'s the weekend'; + } + + else if (day === "Sunday") { + return 'Yes, it\'s the weekend'; + } + else { + return 'No, it\'s the weekday'; + } + + } + + console.log(today("Monday")); /****************** Finding the absolute value of a number @@ -115,7 +258,18 @@ The function should return the absolute value of the number The absolute value of a negative number is the positive version of that same number, and the absolute value of a positive number (or zero) is that number itself. ******************/ +function absolute(num) { + return Math.abs(num); +} +console.log(absolute()); + +const absolute = function(num) { + return Math.abs(num); +} +console.log(absolute()); +const absolute = (num = -1) => Math.abs(num); +console.log(absolute()); /****************** Create a function that counts the number of characters in your name @@ -128,4 +282,26 @@ return the number of characters in the string call function 'countChars' ******************/ +function countChars(nameString = "Jane Doe") { + return nameString.length +} +console.log(countChars()); +const countChars = function(nameString = "Jane Doe")) { + return nameString.length +} +console.log(countChars()); + +const countChar = (nameString = "Jane Doe")) => nameString.length; +console.log(countChars("Osita")); + +const simpleConditionalExamples = () => { + if (true){ + console.log('this will show up!'); + } + if (false){ + console.log('will this show up tho…?'); + } + return 1; + } + simpleConditionalExamples(); diff --git a/pset2.js b/pset2.js index 27d3937..170579a 100644 --- a/pset2.js +++ b/pset2.js @@ -9,8 +9,12 @@ returns that computed value @example doMath( 1,2,3 ); // 9 */ - - +const doMath = (firstNum, secondNum, thirdNum) => { + let sum = firstNum + secondNum; + let multiplies = sum * thirdNum; + return multiplies; +} +console.log(doMath( 1,2,3 )); /* @function addMiddleName @param firstName @@ -24,8 +28,14 @@ return firstname and last name @example addMiddleNmae('John', 'Mabel', 'Smith'); // John Mabel Smith */ - - +const addMiddleName = (firstName, lastName, middleName = "") =>{ + + if (middleName.length === 0) { + return firstName.concat(' ',lastName); + } + return firstName.concat(' ',middleName, ' ',lastName); +} +console.log(addMiddleName('John', 'Mabel','Smith')); /* @function defaultPlaceholder @param placeholder {string} @@ -35,8 +45,11 @@ @example defaultPlaceHolder('Hello Wrold!'); // */ - - +const defaultPlaceHolder = (placeholder = '') => { + const inputHolder = placeholder; + return ``; +} +console.log(defaultPlaceHolder('Hello Wrold!')); /* @function addClasses @param {string} class1 @@ -47,8 +60,10 @@ @example addClass('foo', 'bar', 'stuff'); //
stuff
*/ - - +const addClasses = (class1, class2, content) => { + return `
${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());