Last active
October 5, 2020 12:22
-
-
Save Headmast/d884e0c4f660d40c68013a845e021bbe to your computer and use it in GitHub Desktop.
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
// 1 | |
void reverse([String text = '']) { | |
print('New line: ${text.split(' ').reversed.join(' ')}'); | |
} | |
void main() { | |
var a = 'hello world'; | |
reverse(); | |
} | |
//2 | |
double average([List <double>? darray]) { | |
double sum = 0; | |
if ((darray == null) || (darray.length == 0)) { | |
return double.nan; | |
} | |
darray.forEach( | |
(value) { | |
sum += value; | |
} | |
); | |
return sum/darray.length; | |
} | |
void main() { | |
List <double> darray = [2, 4, 2, 3]; | |
print ('Average: ${average(darray)}'); | |
} |
double sum;
double len = darray?.length ?? 0;
darray?.forEach();
return sum/len;
Можно еще так с использованием аналога элвиса из Котлин( ?? )
Да только на 0 потом делить нельзя)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if ветку можно заменить с помощью null-safety. Предлагаю почитать про работу с null в оф. доке и переписать функцию average в более простом формате.