From 6dc0fe1d161c521e52f59a1ae86a90f5a4458f3f Mon Sep 17 00:00:00 2001 From: Kennedy Maling Date: Sat, 10 Aug 2019 00:58:00 +0530 Subject: [PATCH 1/4] update JS Function-Coding style --- .DS_Store | Bin 0 -> 6148 bytes assignments/codingStyle.md | 20 +++ assignments/functionTypes.js | 320 ++++++++++++++++++++++++++++++++++- assignments/functions.md | 84 ++++++--- 4 files changed, 395 insertions(+), 29 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d1f069b055e82da15e37490cda539f06bb255b5b GIT binary patch literal 6148 zcmeHKIc~#13?vg30^GPvxnJ-P7J~DFd?3DH7*OIuNw3Q9%F{AD8bNkvH^xvPXP4rv zpeaOAM6~A!;D$AE4D?S2f{y^e25C2} zeU<Eo^g9;3)=7^y|N4{iTO&kM*E}FxK=94ui6!oX${Nm-JHIR`CP=Toe zud!WP|9^+ynE$6F?x+A2cq#?7S%0iMyi)eo*~?k4E$~mc)%?NDuyzW9w_~8UV{EJ) ezj;yA6{24G^XjI_Q3j71`>=q#a literal 0 HcmV?d00001 diff --git a/assignments/codingStyle.md b/assignments/codingStyle.md index b6f4caf..111c2b0 100644 --- a/assignments/codingStyle.md +++ b/assignments/codingStyle.md @@ -23,4 +23,24 @@ else ## Solution ```js // your code goes here + +function pow(x, n) { + let result = 1; + + for (let i = 0; i < n; i++) { + return *= x; + } + + return result; + +} + + let x = prompt("x?", ""); + let n = prompt("n?", ""); + + if (n <= 0) { + alert("Power ${n} is not supported, please enter an integer number greater than 0"); + } else { + alert( pow(x, n) ); + } ``` \ No newline at end of file diff --git a/assignments/functionTypes.js b/assignments/functionTypes.js index 07825e1..68e650b 100644 --- a/assignments/functionTypes.js +++ b/assignments/functionTypes.js @@ -7,7 +7,7 @@ // Example Start /** - * Converts a number a string. + * Converts a number to a string. * @param {number} n * @return {string} the number as a string */ @@ -43,12 +43,58 @@ convertToString(21); // "21" * @return {number} */ +// my code Function Declaration + +function addOne(n = 0) { + return ++n; +} + +// Function Expression + +let addOne = function addOneInput (n = 0) { + return ++n +}; + +// Arrow Function without the curly braces + +let addOne = n => ++n + +// Arrow Function with Curly Braces + +let addOne = n => { + return ++n +}; + + /** * Subtracts one from a given number. * @param {number} n * @return {number} */ +// my code Function Declaration + +function subtractOne(n = 0) { + return --n; +} + +// Function Expression + +let subtractOne = function subtractOneInput(n = 0) { + return --n; +}; + +// Arrow Function without the Curly Braces + +let subtractOne = n => --n; + +// Arrow Function with the Curly Braces + +let subtractOne = n => { + return --n +}; + + /** * Adds two numbers. * @param {number} x @@ -56,6 +102,38 @@ convertToString(21); // "21" * @return {number} the sum */ +//my code Function Declaration + +function addNumbers( x = 0, y = 0 ) { + if (typeof x == "number" && typeof y == "number") { + return x + y; + } else { + alert("not a number"); + } +} + +// Function Expression + +let addNumbers = function addTwoNumbersInput( x = 0, y = 0 ) { + if (typeof x == "number" && typeof y == "number") { + return x + y; + } else { + (alert("not a number"); + } +}; + +// Arrow Function without the curly braces + +let addNumbers = (x = 0, y = 0) => x + y; + + +// Arrow Function with the Curly Braces + +let addNumbers = (x = 0, y = 0) => { + return x + y; +}; + + /** * Subtracts the second number from the first. * @param {number} x @@ -63,6 +141,38 @@ convertToString(21); // "21" * @return {number} the difference */ +//my code Function Declaration + +function subtractSecondNumberFromFirst ( x = 0, y = 0 ) { + if (typeof x == "number" && typeof y == "number") { + return y - x; + } else { + alert("not a number"); + } + } + + +// Function Expression + +var subtractSecondNumberFromFirst = function ( x = 0, y = 0) { + if (typeof x == "number" && typeof y == "number") { + return y - x; + } else { + alert("not a number"); + } +}; + +// Arrow Function without the curly braces + +let subtractSecondNumberFromFirst = (x = 0, y = 0) => y - x; + + +// Arrow Function with the Curly brackets + +let subtractSecondNumberFromFirst = (x = 0, y = 0) => { + return y - x; +}; + /** * Multiplies two numbers. * @param {number} x @@ -70,6 +180,37 @@ convertToString(21); // "21" * @return {number} the product */ +//my code Function Declaration + + function multiplyTwoNumber(x = 0, y = 0) { + if (typeof x == "number" && typeof y == "number") { + return (x * y); + } else { + alert("not a number"); + + } + } + +// Function Expression + +let multiplyTwoNumber = function multiplyTwoNumbersInput(x = 0, y = 0) { + if (typeof x == "number" && typeof y == "number") { + return x * y; + } else { + alert("not a nuumber"); + } +} + +// Arrow Function without the Curly Braces + +let multiplyTwoNumber = (x = 0, y = 0) => x * y; + +// Arrow Function with the Curly Braces + +let multiplyTwoNumber = (x = 0, y = 0) => { + return (x * y); +} + /** * Divides the first number by the second. * @param {number} x @@ -77,12 +218,65 @@ convertToString(21); // "21" * @return {number} the quotient */ +//my code Function Declaration + +function divideSecondNumber( x = 0, y = 0 ) { + if (typeof x == "number" && typeof == "number") { + return y / x; + } else { + alert("not a number"); + } +} + +// Function Expression + +let divideSecondNumber = function secondNumber( x = 0, y = 0) { + if (typeof x == "number" typeof == "number") { + return y / x; + } else { + alert("not a number"); + } +}; + +// Arrow Function without the curly Brackets + +let divideSecondNumber = (x = 0, y = 0) => y / x; + +// Arrow with the curly brackets + +let divideSecondNumber = (x = 0, y = 0) => { + return y / x +}; + /** * Multiplies a number by itself. * @param {number} x, number to be squared * @return {number} squared */ +//my code Function Declaration + +function multiplyNumberItself( x = 0) { + return (x**2); + } + +// Function Expression + +var multiplyNumberItself = function multiplyItself( x = 0) { + return (x**2); +}; + +// Arrow Function without the curly brackets + +var multiplyNumberItself = (x = 0) => x**2; + +//Arrow Function without the Curly Brackets + +var multiplyNumberItself = (x = 0) => { + return x**2; +} + + /** * Performs a mathematical operation on two numbers. * Also prints out the equation: (i.e.) "1 + 5 = 6" or "8 / 2 = 4". @@ -92,6 +286,25 @@ convertToString(21); // "21" * @return {number} the result */ +//my code Function Declaration + +//let operation = "add", "subtract", "multiply", "divide"; + +function performMathOperation (x, y, operation) { + if (operation == "+") { + return x + y; + } else if (operation == "-") { + return x - y; + } else if (operation == "*") { + return x * y; + } else if (operation == "/") { + return x / y; + } else { + return "none"; + } +} + + /** * Returns true if `a` is greater than `b`. * @param {number} a @@ -99,6 +312,16 @@ convertToString(21); // "21" * @return {boolean} `a` is larger than `b` */ +//my code Function Declaration + +function greaterOrLarger( a, b) { + if (a > b) { + return true; +} else { + return false; +} +} + /** * Returns true if `a` is less than `b`. * @param {number} a @@ -106,6 +329,16 @@ convertToString(21); // "21" * @return {boolean} `a` is smaller than `b` */ +//my code Function Declaration + +function performTrueFalse( a, b) { + if (a < b) { + return true; +} else { + return false; +} +} + /** * Returns true if `a` and `b` are equal. * @param {number} a @@ -113,6 +346,16 @@ convertToString(21); // "21" * @return {boolean} the numbers are equal */ +//my code Function Declaration + +function equalNumbers( a = 0, b =0 ) { + if (a == b) { + return "the numbers are equal"; + } else { + return "the numbers are not equal" + } +} + /** * Returns the smallest value of two numbers. * @param {number} x @@ -120,6 +363,18 @@ convertToString(21); // "21" * @return {number} the smallest number */ +//my code Function Declaration + +function smallestValue(x = 0, y =0) { + if (x < y) { + return x; + } else if (y < x) { + return y; + } else { + return ""; + } +} + /** * Returns the largest value of two numbers. * @param {number} x @@ -127,18 +382,49 @@ convertToString(21); // "21" * @return {number} the largest number */ +//my code Function Decalaration + +function largestValue(x=0 , y=0) { + if (x > y) { + return x; + } else if (y > x) + return y; + } +} + /** * Returns true if `n` is even. * @param {number} n * @return {boolean} the number is even */ +//my code + +let n = 0; + function getEvenNumber(n =0) { + if (n % 2 == 0) { + return true; + } else { + return false; + } + } + /** * Returns true if `n` is odd. * @param {number} n * @return {boolean} the number is odd */ +//my code Function Expression + +var getOddNumberOut = function getOddNumber(n = 0) { + if (n%2==0) { + return "odd"; + } else { + return "even"; + } +}; + /** * Returns a letter grade. * "A": 90-100% @@ -151,9 +437,41 @@ convertToString(21); // "21" * @return {string} the score represented as a letter grade */ +//my code + +function calculateGrades(score, total) { + let percentage = (score * 100) / total + switch (true) { + case (percentage < 60): + return "F"; + break; + case (percentage >= 60 && percentage < 69): + return "D"; + break; + case (percentage >= 70 && percentage < 79): + return "C"; + break; + case (percentage >= 80 && percentage < 89): + return "B"; + break; + case (percentage >= 90 && percentage < 100): + return "A"; + break; + default: + return "check with your teacher"; + + } +} /** * Joins two strings with a space. * @param {string} word1 * @param {string} word2 * @return {string} joined the words joined with a space */ + +//my code + +function stringsWithSpace(word1, word2) { + return (word1+ " " +word2); +} + diff --git a/assignments/functions.md b/assignments/functions.md index 041da59..809e915 100644 --- a/assignments/functions.md +++ b/assignments/functions.md @@ -1,31 +1,47 @@ 1. 🎖Write a function named calculateDogAge that: - * [ ] Takes 1 argument: your puppy's age. - * [ ] Calculates your dog's age based on the conversion rate of 1 human year to 7 dog years. - * [ ] Outputs the result to the screen like so: "Your doggie is NN years old in dog years!" - * [ ] Add an additional argument to the function that takes the conversion rate of human to dog years. + +- [ ] Takes 1 argument: your puppy's age. +- [ ] Calculates your dog's age based on the conversion rate of 1 human year to 7 dog years. +- [ ] Outputs the result to the screen like so: "Your doggie is NN years old in dog years!" +- [ ] Add an additional argument to the function that takes the conversion rate of human to dog years. ```js // your code goes here + + ``` + 2. 🎖Write a function named calculateSupply that: - * [ ] takes 2 arguments: age, amount per day. - * [ ] calculates the amount consumed for rest of the life (based on a constant max age). - * [ ] outputs the result to the screen like so: "You will need NN to last you until the ripe old age of X" - * [ ] Accept floating point values for amount per day, and round the result to a round number. + +- [ ] takes 2 arguments: age, amount per day. +- [ ] calculates the amount consumed for rest of the life (based on a constant max age). +- [ ] outputs the result to the screen like so: "You will need NN to last you until the ripe old age of X" +- [ ] Accept floating point values for amount per day, and round the result to a round number. ```js // your code goes here + +function calculateSupply(age, amount per day) { + +} + ``` + 3. 🎖Create a function called celsiusToFahrenheit: - * [ ] Store a celsius temperature into a variable. - * [ ] Convert it to fahrenheit and output "NN°C is NN°F". - * [ ] Create a function called fahrenheitToCelsius: - * [ ] Now store a fahrenheit temperature into a variable. - * [ ] Convert it to celsius and output "NN°F is NN°C." + +- [ ] Store a celsius temperature into a variable. +- [ ] Convert it to fahrenheit and output "NN°C is NN°F". +- [ ] Create a function called fahrenheitToCelsius: +- [ ] Now store a fahrenheit temperature into a variable. +- [ ] Convert it to celsius and output "NN°F is NN°C." ```js // your code goes here + + + ``` + 4. 🎖The function below returns true if the parameter age is greater than 18. Otherwise it asks for a confirmation and returns its result: ```js @@ -38,15 +54,23 @@ function checkAge(age) { } } ``` - 4.1 🎖Convert the above function using ternary operator. - ```js - // your code goes here - ``` - - 4.2 🎖Convert the above function using `||` operator. - ```js - // your code goes here - ``` + +4.1 🎖Convert the above function using ternary operator. + +```js +// your code goes here + + +``` + +4.2 🎖Convert the above function using `||` operator. + +```js +// your code goes here + +(age > 18) (return true) || (age < 18) (return confirm("Did parents allow you?");) +``` + Will the function work differently if else is removed like below? ```js @@ -56,17 +80,19 @@ function checkAge(age) { } // ... return confirm("Did parents allow you?"); -} +} + +// NO ``` Is there any difference in the behavior of these two variants? If there is what is that? 5. 🎖 Write a function pow(x,n) that returns x in power n. - * [ ] Use prompt to take values for x and n, and then shows the result of pow(x,n) using alert. - * [ ] In this task the function should support only natural values of n: integers greater then 1. +- [ ] Use prompt to take values for x and n, and then shows the result of pow(x,n) using alert. +- [ ] In this task the function should support only natural values of n: integers greater then 1. -```js +````js // Your code goes here // After writing code uncomment to check the answer. @@ -79,12 +105,14 @@ Is there any difference in the behavior of these two variants? If there is what ```js // your code goes here -``` +```` + 6. 🎖Write a program that asks the user for a number n using prompt and prints the sum of the numbers 1 to n ```js // your code goes here ``` + 7. 🎖Modify the previous program such that only multiples of 5 or 7 are considered in the sum, e.g. n = 20 (5,7,10,14,15,20) 71 ```js @@ -100,4 +128,4 @@ console.log(min(0, 10)); // → 0 console.log(min(0, -10)); // → -10 -``` \ No newline at end of file +``` From aabb8a21fa414b34498a61afaad8975cf8d3edf4 Mon Sep 17 00:00:00 2001 From: Kennedy Maling Date: Fri, 16 Aug 2019 09:54:10 +0530 Subject: [PATCH 2/4] update coding style --- assignments/functions.md | 95 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 6 deletions(-) diff --git a/assignments/functions.md b/assignments/functions.md index 809e915..5419e86 100644 --- a/assignments/functions.md +++ b/assignments/functions.md @@ -7,7 +7,10 @@ ```js // your code goes here - +function calculateDogAge(puppyAge, conversion) { + alert("Your doggies is " + puppyAge * conversion + " years old in dog years"); + return puppyAge * conversion; + } ``` @@ -20,11 +23,14 @@ ```js // your code goes here +var maxAge = 64; -function calculateSupply(age, amount per day) { - +function calculateSupply() { + var amountPerDay = +prompt("put your daily wage"); + return maxAge * amountPerDay; } +alert(`You will need calculateSupply() to last you until the ripe old age of ${maxAge}') ``` 3. 🎖Create a function called celsiusToFahrenheit: @@ -38,8 +44,15 @@ function calculateSupply(age, amount per day) { ```js // your code goes here +function celciusToFahrenheit(celcius) { + var celciusToFahrenheit = celcius* 9/5 + 32; + alert(celcius + "°C is " + celciusToFahrenheit + "°F") +} - +function fahrenheitToCelcius(fahrenheit) { + var fahrenheitToCelcius = ((fahrenheit- 32) * 5)/ 9; + alert(fahrenheit + "°F is " + fahrenheitToCelcius + "°C"); +} ``` 4. 🎖The function below returns true if the parameter age is greater than 18. Otherwise it asks for a confirmation and returns its result: @@ -59,7 +72,9 @@ function checkAge(age) { ```js // your code goes here +var age = 18; +(age > 18) ? true : confirm("Did parents allow you?") ``` @@ -68,7 +83,8 @@ function checkAge(age) { ```js // your code goes here -(age > 18) (return true) || (age < 18) (return confirm("Did parents allow you?");) +(age > 18) && return true || (age >= 18) && confirm("Did parents allow you?") + ``` Will the function work differently if else is removed like below? @@ -94,6 +110,16 @@ Is there any difference in the behavior of these two variants? If there is what ````js // Your code goes here +var x = +prompt("enter a number"); +var n = +prompt("enter a number"); + +function powerOfTwo(x, n) { + var result = 1; + for (i = 1; i <= n; i++) { + result = result * x; + } + alert(result); +} // After writing code uncomment to check the answer. // pow(3, 2); // 9 @@ -103,20 +129,69 @@ Is there any difference in the behavior of these two variants? If there is what 6. 🎖Write a program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Return the result accordingly. -```js +js // your code goes here +var n = +prompt("enter a number"); +var operator = prompt("enter add or product"); + +function computingSumProduct(n, operator) { + + if (operator == "+") { + var result = 0; + for (i = 1; i <= n; i++){ + result = result + i; + } + alert(result); + } else if (operator == "*") { + var result = 1; + for (i = 1; i <= n; i++){ + result = result * i; + } + alert(result); +} +} + +computingSumProduct(n, operator); + + ```` 6. 🎖Write a program that asks the user for a number n using prompt and prints the sum of the numbers 1 to n ```js // your code goes here +var n = +prompt("enter a number"); + +function sumOfNumbers(n) { + + var result = 0; + for (i = 1; i <= n; i++){ + result = result + i; + } + alert(result); +} + + ``` 7. 🎖Modify the previous program such that only multiples of 5 or 7 are considered in the sum, e.g. n = 20 (5,7,10,14,15,20) 71 ```js // your code goes here + +function multiplyOfTwoNumbers(n) { + + var result = 0; + for (i = 1; i <= n; i++){ + if (i % 5 == 0 || i % 7 == 0) { + result = result + i; + } + } + alert(result); +} + multiplyOfTwoNumbers(); + + ``` 8. 🎖Write a function `min` that takes two arguments and returns their minimum. @@ -124,6 +199,14 @@ Is there any difference in the behavior of these two variants? If there is what ```js // Your code here. +function min( x, y ) { + if (x < y) { + return x; + } else { + return y; + } +} + console.log(min(0, 10)); // → 0 console.log(min(0, -10)); From 576f8c993d4b477353a39084ac4ce490ed17c758 Mon Sep 17 00:00:00 2001 From: Kennedy Maling Date: Fri, 16 Aug 2019 12:09:45 +0530 Subject: [PATCH 3/4] upadating minor changes --- assignments/functionTypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/functionTypes.js b/assignments/functionTypes.js index 68e650b..f050e6e 100644 --- a/assignments/functionTypes.js +++ b/assignments/functionTypes.js @@ -13,7 +13,7 @@ */ //Function Decleration -function convertToString(n) { +function convertToNumber(x) { return String(n); } From 30c1f18ac68d84f0778fc9ec838c43d4abb8413a Mon Sep 17 00:00:00 2001 From: Kennedy Maling Date: Fri, 16 Aug 2019 12:12:59 +0530 Subject: [PATCH 4/4] updating functionTypes.js --- assignments/functionTypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/functionTypes.js b/assignments/functionTypes.js index f050e6e..76d4d8f 100644 --- a/assignments/functionTypes.js +++ b/assignments/functionTypes.js @@ -13,7 +13,7 @@ */ //Function Decleration -function convertToNumber(x) { +function convertTostring(n) { return String(n); }