-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg30.js
123 lines (109 loc) · 2.63 KB
/
pg30.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//pg30
//Chapter 2 - Fundementals Part II
var twelveBars = function(){
for(var i=0;i<=12;i++){
console.log(i);
console.log("chick");
console.log("boom");
};
};
//Fibonacci sequence calculator
var fibonacci = function(val){
if(val==0){
return 0
}
if(val==1){
return 1
}
var first = 0;
var second = 1;
for(var i=2;i<val;i++){
var temp=second;
second = first + second;
first = temp;
}
return second+first;
}
//Make a function that accepts a value and then repeatedly adds up that numbers individual digits until there is only one digit left
var sumToOne = function(num){
var testFunction = function(val){
var arr = [];
while((val/10)>1){
arr.push(val%10);
val = Math.floor(val/10)
};
arr.push(val);
//now have a flipped array of each number
var newVal = 0;
for(var i=0;i<arr.length;i++){
newVal += arr[i];
};
return newVal;
};
var repeat = testFunction(num)
while(repeat/10>1){
repeat = testFunction(repeat)
};
return repeat;
};
//make a function that accepts a number of number of seconds, up to 12 hours worth, and will the return the angles at which a clock would be at to display that give time.
var clockHand = function(given){
var remainder = given%3600;
var numerator = given-remainder;
var hours = numerator/3600;
var temp = remainder
remainder = remainder%60
numerator = temp-remainder;
var minutes = numerator/60;
var seconds = remainder;
//now have individual hours, minutes, and seconds
var sAngle = (360/60)*seconds
var mAngle = (360/60)*minutes
mAngle += (seconds/60)*6;
var hAngle = (360/12)*hours;
hAngle +=(minutes/60)*30;
console.log(hAngle, mAngle, sAngle);
}
//return whether a given integer is prime
var isPrime = function(num){
if(num==0 || num==1 || num==2){
return true;
}
if(num%2==0){
return false;
}
for(var i=3;i<num;i+=2){
if(num%i==0){
return false;
}
}
return true;
}
//Make a function that, given a number and a digit place number, returns the 0-9 value at that digit place number position
var extractNumber = function(num, digit){
//gives us a number where the wanted digit is in the units place.
var wantedInUnit = num/Math.pow(10,digit-1);
//returns the value of the units place by giving the remainder of the original number when divided by 10.
return wantedInUnit%10;
}
//Make a function that returns the most significant digit in a number
//DOESN'T WORK!!!
var mostSigDigit = function(num){
if(num>1){
var trigger = true;
while(trigger){
if(num%10 != 0){
var store = num%10
}
num = Math.floor(num/10);
if(num<1){
trigger = false;
}
}
}
else{
var test = 123;
}
return store;
}
console.log(mostSigDigit(1));