forked from SF-WDI-LABS/functions-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
108 lines (92 loc) · 2.05 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//1.
function combineWords(word1, word2) {
//concatenate two words
return word1+word2;
}
var result = combineWords('dog', 'house');
console.log(result);
// displays 'doghouse'
//2.function repeatPhrase(phrase, n) {
//Display an argument phrase to the console n times
var i;
for(i=0; i<5; i++){
console.log(phrase);
}
}
repeatPhrase ("Hello", 5);
// displays
// Hello
// Hello
// Hello
// Hello
// Hello
//3.
function toTheNthPower(number, power) {
// Display number power
var i;
var nThNumber = number;
for(i=1; i<power; i++){
nThNumber = nThNumber * number;
}
console.log(nThNumber);
}
toTheNthPower(4, 5);
// displays 1024
//4.
function areaOfACircle(radius) {
// Display the area of a circle given the radius
var area = Math.PI * Math.pow(radius,2);
console.log(area);
}
areaOfACircle(2);
// displays approximately 12.57
//5.
function pythagoreanTheorem(a, b) {
// Pythagorean Theorem: a2 + b2 = c2
var cSqr = Math.pow(a,2) + Math.pow(b,2);
var c = Math.sqrt(cSqr);
//display c
console.log(c);
}
pythagoreanTheorem(3, 4);
// should display 5;
//6.
function isXEvenlyDivisibleByY(x, y) {
/* Return a boolean value whether or not X can be
divided by Y without any remainders.*/
return (x % y === 0);
}
isXEvenlyDivisibleByY(99, 3);
// displays true
//7.
function countVowels(word) {
//Count the number of occurence of vowels in a word.
//Vowels are a, e, i, o, u, and
var i;
var counter=0;
for(i=0; i < word.length; i++){
if(word[i].toLowerCase()==='a' || word[i].toLowerCase()==='e' ||
word[i].toLowerCase()==='i' || word[i].toLowerCase()==='o' ||
word[i].toLowerCase()==='u'){
counter++;
}
}
console.log(counter);
}
countVowels("stealing");
// displays 3
//8.
function printTriangle(length) {
// Display a simple triangle with asterisks
var i;
var line = "";
for(i=1; i<=length; i++){
line = line + "*";
console.log(line);
}
}
printTriangle(3);
// displays
// *
// **
// ***