forked from CoachJulian/2023TeamEdgeTerm0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhile_loops.js
175 lines (112 loc) · 4.26 KB
/
while_loops.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/********************************************************************
*
* Team Edge Mini-project: WHILE LOOP CHALLENGES
*
* Complete the following loop challenges below. Follow the ToDos
* 1. IN YOUR PRIME: Declare a while loop that prints all the prime
* numbers between 0 and 100 (use the helper function provided)
* 2. FOUND: use a while loop to search the contents of an array for
* the key!
* 3. BUGGIN: Find out why these while loops don't do what they say
* they do.
* 4. MATH QUIZ: Prompt a user to solve a random math problem, if
* they get it right, say congrats, if not, keep prompting.
* 5. WHAT AM I: Write a game loop that prompts that never stops
* asking, "Iknow you are a _____, but what am I?"
*
* ***************************************************************/
const READLINE = require("readline-sync");
console.log(`
hh iii lll lll
ww ww hh lll eee lll oooo oooo pp pp sss
ww ww hhhhhh iii lll ee e lll oo oo oo oo ppp pp s
ww ww ww hh hh iii lll eeeee lll oo oo oo oo pppppp sss
ww ww hh hh iii lll eeeee lll oooo oooo pp s
pp sss
`)
console.log("------------------- CHALLENGE 1 : IN YOUR PRIME -------------------")
//Here is a humble while loop in action. We need a variable to hold the counter value.
let num = 0
while(num <= 10){
console.log("example counter--> " + num)
num++
}
//-->TODO: Declare a while loop that prints all the prime numbers between 0 and 100, use test_prime() helper function
console.log("------------------- CHALLENGE 2 : FOUND -------------------")
//here is an array full of items
let items = ["pencil" , "eraser" , "mirror" , "comb" , "spoon" , "key" , "earrings" ,"cat food" , "magazine"]
//-->TODO: Use a while loop to search the contents of an array for the key! If it exists, print "found the key!"
console.log("------------------- CHALLENGE 3 : BUGGIN -------------------")
//Oh no! these functions have loops that don't do what they say they should do. Can you fix that?
//One more thing...to stop an infite loop you hit Control + C in the terminal
//-->TODO: Make me count 2, 4, 6,..., 50
function evenNumbersToFifty(){
let num = 50
while(num <50){
console.log("number: " + num)
}
}
evenNumbersToFifty()
//-->TODO: Make this design below
//
// [ 0 ]
// [ 0, 1 ]
// [ 0, 1, 2 ]
// [ 0, 1, 2, 3 ]
// [ 0, 1, 2, 3, 4 ]
// [ 0, 1, 2, 3, 4, 5 ]
// [ 0, 1, 2, 3, 4 ]
// [ 0, 1, 2, 3 ]
// [ 0, 1, 2 ]
// [ 0, 1 ]
// [ 0 ]
function pattern(){
let index = 0
let array =[]
while(index <= 5 ){
array.push(index)
// console.log(array)
index++
}
}
pattern()
console.log("------------------- CHALLENGE 4 : MATH QUIZ -------------------")
//-->TODO: Make a Math Quiz that asks two random numbers (between 0 and 100 to make it easy).
// The user enters the answer. If wrong, keep prompting. If correct, say congrats!!
// Use this handy boolean to get you started! You will need readline_sync!
let isCorrect = false
console.log("------------------- CHALLENGE 5 : WHAT AM I? -------------------")
//-->TODO: Write a game loop that prompts that never stops asking, "Iknow you are a _____, but what am I?"
// You are given to starter functions and a loop to get started!
// Notice how one function calls the other and uses the returned value as the input. This is called Recursion!
let keepAsking = false
while(keepAsking){
//response(promptUser())
}
function promptUser(){
}
function response(response){
}
//-->TODO: Challenge! write a secret word to break out of the loop!
//------------ Helper function, do not mess with this part below ---------------??
function test_prime(n)
{
if (n===1)
{
return false;
}
else if(n === 2)
{
return true;
}else
{
for(var x = 2; x < n; x++)
{
if(n % x === 0)
{
return false;
}
}
return true;
}
}