-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15 Intermediate Javascript Exercises.txt
151 lines (137 loc) · 3.71 KB
/
15 Intermediate Javascript Exercises.txt
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//Love Calculator (random number generator) exercise
var name1 = prompt("What is your name?");
var name2 = prompt("What is their name");
var chance = Math.floor(Math.random() * 101);
alert("You have a " + chance + "% chance of having a successful relationship with " + name2);
//Control Statements: Using If-Else Conditionals & Logic & Comparators
var name1 = "Nic";
var name2 = "Allison";
var chance = Math.floor(Math.random() * 101);
if (chance === 100) {
alert("You all are a perfect match! You have a " + chance + "% chance of having a successful relationship with " + name2);
}
else if (chance >= 50 && chance < 100){
alert("You are likely to make a good couple! You have a " + chance + "% chance of having a successful relationship with " + name2);
}
else if (chance !== 0){
alert("It's possible. You have a " + chance + "% chance of having a successful relationship with " + name2);
}
else{
alert("run");
}
//Compartors exercise, BMI Calculator expansion
function bmiCalculator(weight, height) {
var bmi = Math.round(weight / Math.pow(height, 2));
if (bmi < 18.5) {
var interpretation = "Your BMI is " + bmi + ", so you are underweight.";
}
else if (bmi >= 18.5 && bmi <= 24.9) {
var interpretation = "Your BMI is " + bmi + ", so you have a normal weight.";
}
else {
var interpretation = "Your BMI is " + bmi + ", so you are overweight.";
}
return interpretation;
}
var interp = bmiCalculator(65, 1.8);
console.log(interp);
//Leap Year Challenge Exercise
function isLeap(year) {
var yes = "Leap year.";
var no = "Not leap year.";
if(year % 4 == 0) {
if(year % 100 == 0) {
if(year % 400 ==0) {
return yes;
}
else
return no;
}
else {
return yes;
}
}
else
return no;
}
console.log(isLeap(2000));
console.log(isLeap(2100));
//Section 106: FizzBuzz array exercise
var output = [];
for (var x=1; x < 101; x++){
if (x % 3 == 0) {
if (x % 5 != 0) {
output.push("Fizz");
}
else {
output.push("FizzBuzz");
}
}
else if (x % 5 == 0){
output.push("Buzz");
}
else {
output.push(x);
}
}
console.log(output);
//FizzBuzz as a function
var output = [];
var count = 1
function fizzbuzz () {
if(count > 15) {
console.log("Too high....tooo...hard");
return;
}
else if(count % 3 == 0) {
if(count % 5 == 0) {
output.push("FizzBuzz");
}
else {
output.push("Fizz");
}
}
else if (count % 5 == 0) {
output.push("Buzz");
}
else
output.push(count);
console.log(output);
count++;
}
//Coding Exercise 6: Who's buying Lunch
//Random name chosen from the list, returns output of who is buying Lunch
function whosPaying(names) {
var string = null, int = 0;
int = Math.floor(Math.random() * (names.length));
string = names[int] + " is going to buy lunch today!";
console.log(string);
return string;
}
//Coding exercise 7: The fibonacci Exercise
function fibonacciGenerator (n) {
//Do NOT change any of the code above 👆
var sequence = [];
var i = 0;
if (n <= 0) {
console.log("Must be a positive number");
}
else if (n==1) {
sequence.push(0);
}
else if (n==2) {
sequence.push(0);
sequence.push(1);
}
else {
sequence.push(0);
sequence.push(1);
for(i = 2; i < n; i++) {
sequence.push(sequence[i-2] + sequence[i-1]);
}
}
return sequence;
//Do NOT change any of the code below 👇
}
var output = fibonacciGenerator(15);
console.log(output);