-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms-part1.js
125 lines (103 loc) · 2.86 KB
/
algorithms-part1.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
124
125
// Coding Dojo's Algorithms Book - Part 1
// My Solutions
// Print -52 to 1066
// Print integers from -52 to 1066 using a FOR loop.
for (let integers =-52; integers<=1066; integers++) {
console.log(integers);
}
// Don’t Worry, Be Happy
// Create beCheerful(). Within it, console.log
// string "good morning!" Call it 98 times.
function beCheerful() {
console.log("good morning!")
}
for (let x = 1; x<=98; x++) {
beCheerful();
}
// Multiples of Three – but Not All
// Using FOR, print multiples of 3 from -300 to 0.
// Skip -3 and -6.
for (let m = -300; m<=0; m+=3) {
if (m == -3 || m == -6) {
continue;
}
console.log(m);
}
// Printing Integers with While
// Print integers from 2000 to 5280, using a WHILE.
let i = 2000;
while (i <=5200)
{
console.log(i);
i++;
}
// You Say It’s Your Birthday
// If 2 given numbers represent your birth month
// and day in either order, log "How did you
// know?", else log "Just another day...."
let given1 = 3;
let given2 = 9;
if (given1 == 9 || given1 == 3 && given2 == 9 || given2 == 3) {
console.log("How did you know?");
} else {
console.log("Just another day....");
}
// Leap Year
// Write a function that determines whether a given
// year is a leap year. If a year is divisible by four,
// it is a leap year, unless it is divisible by 100.
// However, if it is divisible by 400, then it is.
let year = 2020;
function isLeapYear() {
return (year % 100 == 0) ? (year % 400 == 0) : (year % 4 == 0);
}
console.log(isLeapYear(year));
// Print and Count
// Print all integer multiples of 5, from 512 to 4096.
// Afterward, also log how many there were.
let counter=0;
for (let m=512; m<=4096; m++) {
if (m % 5 == 0) {
console.log(m);
counter++
};
}
console.log(`Number of times: ${counter}`);
// Multiples of Six
// Print multiples of 6 up to 60,000, using a WHILE.
let mu = 1;
while (mu<=60000) {
if (mu % 6 == 0) {
console.log(mu);
}
mu++;
}
// Counting, the Dojo Way
// Print integers 1 to 100. If divisible by 5, print
// "Coding" instead. If by 10, also print " Dojo".
for (let num = 1; num<=100; num++) {
if (num % 5 == 0) {
console.log('Coding');
if (num % 10 == 0) {
console.log('Dojo');
}
}
console.log(num);
}
// What Do You Know?
// Your function will be given an input parameter
// incoming. Please console.log this value.
function Hello(parameter) {
console.log(parameter);
}
Hello('Howdy');
// Whoa, That Sucker’s Huge…
// Add odd integers from -300,000 to 300,000, and
// console.log the final sum. Is there a shortcut?
let oddsum = 0;
for (let i =-300000; i<=300000; i++) {
if (i % 2 == 0) {
oddsum = oddsum+i;
}
}
console.log(`The sum of odd numbers: ${oddsum}`);