Home > Back-end >  Dart: dart.exe consumes all memory
Dart: dart.exe consumes all memory

Time:07-08

I'm learning Dart and when I run dart project, I'm facing this issue, it make my project unable to run. How can I fix it this

Code:

void main(List<String> arguments) async {

  String checkNum = oddOrEvenNum(12);
  print('checkNum hahahahah');
}



String oddOrEvenNum(int number) {
  if (number < 10)
    return 'Odd degit';
  else {
    int degitNum = 0;
    do {
      number ~/= 10;
      degitNum  ;
      if (number < 10) {
        degitNum  ;
      }
    } while (number < 10);

    if (degitNum % 2 == 0)
      return 'Even number with $degitNum';
    else
      return 'Odd number with $degitNum';
  }
}

CodePudding user response:

Your loop have a bug which makes your program run forever, without printing anything, since the program will just end up staying inside your while loop.

You should change the following:

} while (number < 10);

Into:

} while (number > 10);

Since number are being reduced in size for each loop iteration.

  •  Tags:  
  • dart
  • Related