Last active
January 30, 2020 04:48
-
-
Save IhwanID/c51260e501813908b6b882474109ff14 to your computer and use it in GitHub Desktop.
looping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| List<int> numbers = [1, 2, 3, 4, 5]; | |
| print(total(numbers)); | |
| print(combine(numbers)); | |
| } | |
| int total(List<int> value){ | |
| int i = 0; | |
| int result = 0; | |
| while(i < value.length){ | |
| result += value[i]; | |
| i++; | |
| } | |
| return result; | |
| } | |
| int combine(List<int> values){ | |
| return values.fold(0, (result, value) => result + value); | |
| } | |
| int sum(List<int> values){ | |
| int result = 0; | |
| for(int i = 0; i < values.length; i++){ | |
| result += values[i]; | |
| } | |
| //simple | |
| for(int value in values){ | |
| result += value; | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment