Fork this repository to your GitHub account, then clone your GitHub copy onto your computer. Write the functions assigned below in the file functions.js
. Use snippets to run and observe the results of your code.
Here's how to use a snippet:
- Open the Chrome Developer Console (Option + Command + I)
- Navigate to the
Sources
tab in the Chrome Developer Console. - Click on the
Snippets
tab within the Sources display. - Right-click and choose
New
to create a new Snippets file. - Name the Snippets File
myFunctions
. - Write or paste code into the editing window and press
cmd
+return
to run the code.
You'll be filling in a bunch of empty function definitions. Type each blank function definition into the snippet, then you write the rest of the code to make the function work.
After you implement a function successfully in your snippet, copy it over into the functions.js
file. Unless a different behavior is specified, have the function log the answer to the console AND return it.
Notice there is a solutions.js file. Make a full attempt at finishing these on your own and refer to the solutions only when you are finished or unbearably stuck. Also recognize that the solutions are only one solution and that there are many good ways to solve these problems.
For some of the problems, you'll have to use functions or other variables supplied by JavaScript's built-in Math
object. Here is the documentation on using the Math
object.
Example Usage of Math
var absoluteValue = Math.abs(-4);
// evaluates to 4
var fourSquared = Math.pow(4, 2);
// evaluates to 16
var roundedToNearestInteger = Math.round(1.22343);
// evaluates to 1
Misha LeClair https://github.com/sf-wdi-31/functions-training.git I was able to work through the mathematical functions okay. I ran into trouble with toTheNthPower and countVowels. Both stumped me about creating a count that adds one as you iterate through with a loop. I think with more practice I'll get it. I couldn't complete the pyramids. I looked at the solutions but was confused by them, so I plan to ask for help during a break or after class this evening to understand better and complete these final two.
Return a new string that is the combination of two arguments passed into the function
Example: dog
and house
will display doghouse
function combineWords(word1, word2) {
// TODO: Place your code here
}
var result = combineWords('dog', 'house');
console.log(result);
// displays 'doghouse'
Display an argument phrase to the console n times
function repeatPhrase(phrase, n) {
// TODO: Place your code here
}
repeatPhrase("Hello", 5);
// displays
// Hello
// Hello
// Hello
// Hello
// Hello
Return number power without using built-in Math
functions
Example:
45 = 4 * 4 * 4 * 4 * 4 = 1024
function toTheNthPower(number, power) {
// TODO: Place your code here
}
var result = toTheNthPower(4, 5);
console.log(result);
// displays 1024
Return the area of a circle given the radius
background information
function areaOfACircle(radius) {
// TODO: Place your code here
}
var result = areaOfACircle(2);
console.log(result);
// displays approximately 12.57
Return c given a and b
background information
function pythagoreanTheorem(a, b) {
// TODO: Place your code here
}
var result = pythagoreanTheorem(3, 4);
conosle.log(result);
// should display 5;
Return a boolean value whether or not X can be divided by Y without any remainders.
Hint: Explore the world of Modulus operators!
function isXEvenlyDivisibleByY(x, y) {
// TODO: Place your code here
}
var result = isXEvenlyDivisibleByY(99, 3);
console.log(result);
// displays true
Return the number of occurrences of vowels in a word.
Vowels are a
, e
, i
, o
, u
, and y
function countVowels(word) {
// TODO: Place your code here
}
var result = countVowels("stealing");
console.log(result);
// displays 3
Challenge: Can you alter the code to count both upper case AND lower case?
Given an array, return true
if it contains the string "wdi" and false
if it does not contain that string.
Example:
findWdi([9,'Bart Simpson', true, 'wdi']) // returns true
findWdi(['a','b','c']) // returns false
function findWdi(arr){
// TODO: Place your code here
}
Display a simple triangle with asterisks
Example:
printTriangle(5)
*
**
***
****
*****
function printTriangle(length) {
// TODO: Place your code here
}
printTriangle(3);
// displays
// *
// **
// ***
Example: printPyramid(10);
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Warning: This is a surprisingly tricky interview-level exercise. Try at your own risk!
function printPyramid(length) {
// TODO: Place your code here
}