-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loop.sol
101 lines (69 loc) · 2.72 KB
/
Loop.sol
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
//Loop allow you to iterate through data and take functional action depending on the data
// A "For" Loop is used to repeat a specific block of code a known number of times.
// For example, if we want check the grade of every student in the class, we loop from 1 to that number.
pragma solidity >= 0.7.0 < 0.9.0;
contract loopContract {
// a list of number ranging from 1 to 10
uint [] public numbersList = [1,2,3,4,5,6,7,8,9,10];
// Ex. if we have 4 how many numbers in our list can be a multiple
// A Loop that acts as a multiple checker in our numberList
function checkMultiples(uint _number) public view returns (uint){
uint count = 0;
// The 3 part statement of a for loop
// 1. initialize start of loop
// 2. determine length of loop
// 3. to direct the indez after each turn
for(uint i; i<numbersList.length; i++)
{
//Logic for the loop in here
if(checkMultiplesModulo(numbersList[i], _number)){
count ++;
}
}
return count;
}
//create a function that can check if two numbers are divisible by each other - or are they
// multiples of each other
// 4 and 2 - return true
//3 and 5 - return false
function checkMultiplesModulo(uint _num, uint _nums) public view returns(bool)
{
if (_num % _nums == 0){
return true;
}
else{
return false;
}
}
}
// Looping Practice:
//1. create a contract myLoopingPracticeContract and place all the following code within:
// 2. create a list that ranges from 1 to 20 called longList
// 3. create a list called numbersList of the following numbers: 1, 4, 34, 56
// 4. create a function that loops through numbersList and returns a true value if the number
// that the user inputs exists in the list otherwise it should return false
// 5. create a function that loops through and returns how many even numbers there are in the long list
contract myLoopingPracticeContract{
uint [] public longList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
uint [] public numbersList = [1,4,34,56];
function checkEvenNumber(uint _num) public view returns(bool)
{
if (_num % 2 == 0){
return true;
}
else{
return false;
}
}
function CheckIfNumberExist(uint userNumber) public view returns(uint){
uint numberExist = false ;
for (uint i = 0; i< numbersList.length; i++ )
{
if (checkEvenNumber(numbersList[i] == userNumber )) //doesnt work
{
numberExist = true;
}
}
return numberExist;
}
}