Code
void main() {
int printit(var y, var z){
var c = y+10;
print (y);
return c;
}
printit(20,23);
var b = printit(1,3);
print (b);
}
Why does the above code in DartPad gives output as this?
20
1
11
Output
Why does it prints the function for the second time when I am trying to add the return value of the function to a variable 'b'?
It prints it because you call print(b) in the following line.
Remove the line and the output should be as below:
20
1
Try using the code snippet below
void main() {
int printit(var y, var z){
var c = y+10;
return c;
}
var b;
b= printit(20,23);
print (b);
b = printit(1,3);
print (b);
}
It prints 1 because you assigned to b the value printit(1,3) and in your function itself you ask for printing y (here 1) with print(y).
void main() {
int printit(var y, var z){
var c = y+10;
print (y); // this line is responsible for printing 1. Try removing this.
return c;
}
printit(20,23);
var b = printit(1,3);
print (b);
}
Related
I have a list of strings that when button is pressed, uses the random widget to randomly select one to be saved as a string for use later.
void Randomiser() {
savedString = listOfStrings[Random().nextInt(9)];
}
the above code works but it often randomises the same number multiple times in a row, which i don't want.
I saw someone had posted this code as a fix:
Set<int> setOfInts = Set();
setOfInts.add(Random().nextInt(max));
which works but I can't get it to then save to the string.
thanks so much
you can use your method and store the last returned random number to be sure that you don't get it again :
import 'dart:math';
void main() {
var lastRandom = 1;
final listOfStrings = ['first','second','third'];
String Randomiser(lastRandom) {
var newRandom = lastRandom;
while(newRandom == lastRandom){
newRandom = Random().nextInt(3);
}
lastRandom = newRandom;
return listOfStrings[newRandom];
}
for( var i = 0 ; i < 20; i++ ) {
print(Randomiser(lastRandom));
}
}
I want to do something like this in dart.
var z = +
and then put it in another var like this
var x = 5 z 5
And when I did this
var z = +
I get an Error
Expected an identifier.
you can not make variable operators in dart though what you can do is to create a custom variable for the same by doing:
void main() {
var operators = {
'+': (a, b) { return a + b; },
'<': (a, b) { return a < b; },
// ...
};
var op = '+';
var x = operators[op]!(10, 20);
print(x);
}
When i read data from a big txt file block by block ,I got the error as blow:
Unfinished UTF-8 octet sequence (at offset 4096)
code:
File file = File(path!);
RandomAccessFile _raf = await file.open();
_raf.setPositionSync(skip ?? 0);
var data = _raf.readSync(block);// block = 64*64
content.value = utf8.decode(data.toList());
UTF*8 is variable length encoding.
The error come from data not align to UTF8 boundary
Alternative way is to trim data byte on left and right before call utf.decode
This will lost first and last character. You may read and add more bytes to cover last character and align with utf8 boundary
bool isDataByte(int i) {
return i & 0xc0 == 0x80;
}
Future<void> main(List<String> arguments) async {
var _raf = await File('utf8.txt').open();
_raf.setPositionSync(skip);
var data = _raf.readSync(8 * 8);
var utfData = data.toList();
int l, r;
for (l = 0; isDataByte(utfData[l]) && l < utfData.length; l++) {}
for (r = utfData.length - 1; isDataByte(utfData[r]) && r > l; r--) {}
var value = utf8.decode(utfData.sublist(l, r));
print(value);
}
Optional read more 4 bytes and expand to cover last character
bool isDataByte(int i) {
return i & 0xc0 == 0x80;
}
Future<void> main(List<String> arguments) async {
var _raf = await File('utf8.txt').open();
_raf.setPositionSync(skip);
var block = 8 * 8;
var data = _raf.readSync(block + 4);
var utfData = data.toList();
int l, r;
for (l = 0; isDataByte(utfData[l]) && l < block; l++) {}
for (r = block; isDataByte(utfData[r]) && r < block + 4; r++) {}
var value = utf8.decode(utfData.sublist(l, r));
print(value);
}
Suppose I have a variable int a = 12345, I want to split a to [1,2,3,4,5] and add them like [1+2+3+4+5] and in final I want to get a result of a = 15 how can I achieve this?
All u have to do is recursively add every digit individually.
void main() {
int a = 12345;
int sum = 0;
while(a>0){
sum = sum + (a%10);
a = (a/10).floor();
}
print(sum);
//if u want to store in a
a = sum;
}
There are different ways to achieve the same, one of the ways is as:
void main() {
int num = 12345;
int sum = 0;
String numAsString = num.toString();
for (int i = 0; i < numAsString.length; i++) {
sum += int.parse(numAsString[i]);
}
print(sum); // 15
}
You can achieve using the split() as
void main(){
var i=34567;
var iStr=i.toString().split('');
var exp= iStr.join('+');
var sum=iStr.fold(0,(a,b)=>int.parse(a.toString())+int.parse(b));
print(exp);
print(sum);
}
Output:
3+4+5+6+7
25
If you need only the sum of the integer then
void main() {
var i = 34567;
var iStr = i.toString().split('');
var sum = iStr.fold(0, (a, b) => int.parse(a.toString()) + int.parse(b));
print(sum);
}
I would approach it by first converting the integer to a String.
Then mapping each single character into an int and finally simply
reduce the iterator of ints into the sum.
int num = 12345;
print(num
.toString()
.split('')
.map(
(c) =>int.parse(c)
).reduce((a,b) => a+b));
I saw a class A, after its class definition
class A
constructor: (#x=0, #y=0) ->
#A = A
what does #A = A here?
It helps to look at the generated output.
// Generated by CoffeeScript 1.9.3
(function() {
var A;
A = (function() {
function A(x, y) {
this.x = x != null ? x : 0;
this.y = y != null ? y : 0;
}
return A;
})();
this.A = A;
}).call(this);
So #A translates to this.A. When this is used at the top level, it refers to window. So #A = A exports the A class onto the window object.
This is normally done to export a library. For example, window.$ = jQuery or window._ = lodash.