diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d1f069b Binary files /dev/null and b/.DS_Store differ 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..76d4d8f 100644 --- a/assignments/functionTypes.js +++ b/assignments/functionTypes.js @@ -7,13 +7,13 @@ // Example Start /** - * Converts a number a string. + * Converts a number to a string. * @param {number} n * @return {string} the number as a string */ //Function Decleration -function convertToString(n) { +function convertTostring(n) { return String(n); } @@ -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..5419e86 100644 --- a/assignments/functions.md +++ b/assignments/functions.md @@ -1,31 +1,60 @@ 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 +function calculateDogAge(puppyAge, conversion) { + alert("Your doggies is " + puppyAge * conversion + " years old in dog years"); + return puppyAge * conversion; + } + ``` + 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 +var maxAge = 64; + +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: - * [ ] 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 + +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: ```js @@ -38,15 +67,26 @@ 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 +var age = 18; + +(age > 18) ? true : confirm("Did parents allow you?") + +``` + +4.2 🎖Convert the above function using `||` operator. + +```js +// your code goes here + +(age > 18) && return true || (age >= 18) && confirm("Did parents allow you?") + +``` + Will the function work differently if else is removed like below? ```js @@ -56,18 +96,30 @@ 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 +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 @@ -77,18 +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. @@ -96,8 +199,16 @@ 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)); // → -10 -``` \ No newline at end of file +```