Recap - Simple Calculations
Part of the Fundamentals section of Coddy's Dart journey — lesson 21 of 94.
Challenge
EasyYou're developing a simple game score calculator that tracks a player's progress through different levels. Follow these steps carefully to create your program:
Step 1: Initialize Variables
- Create an integer variable called
playerScoreand set it to 100 - Create an integer variable called
currentLeveland set it to 5
Step 2: Calculate Game Statistics
Calculate and store these values in new variables:
- Level Bonus: Multiply the current level by 20 and store the result in a variable called
levelBonus - Power-ups Available: Each power-up costs 25 points. Use integer division (
~/) to find how many complete power-ups the player can afford from their current score. Store this inpowerUps - Remaining Points: Use the modulo operator (
%) to find how many points are left after buying all possible power-ups. Store this inremainingPoints
Step 3: Update Player Score and Level
- Add the level bonus to the player's score using the
+=operator - Increase the current level by 1 using the prefix increment operator (
++currentLevel) and store this new value in a variable callednewLevel - Subtract 15 points from the player's score using the
-=operator - Double the player's score by multiplying it by 2 using the
*=operator
Step 4: Display Results
The print statements are already provided in the code. Do not modify them. Your job is to fill in the missing variable declarations and calculations above so the output matches exactly.
Note: One of the provided print statements displays playerScore / 1.5, which performs regular (non-integer) division and produces a decimal result. This is expected — the output for that line will be 246.66666666666666.
Important: Make sure your calculations are done in the exact order specified above, as each step affects the next one.
Try it yourself
void main() {
// Initialize player score and level
int playerScore = ?;
int currentLevel = ?;
// Calculate level bonus, power-ups, and remaining points
int levelBonus = ?;
int powerUps = ?;
int remainingPoints = ?;
// Add level bonus to score using assignment shortcut
?;
// Increment level and store new level
int newLevel = ?;
// Decrease score by 15 using assignment shortcut
playerScore ?;
// Calculate final score (multiply by 2)
playerScore ?;
// Print the results
print('Initial score: 100');
print('Level bonus: $levelBonus');
print('Available power-ups: $powerUps');
print('Remaining points: $remainingPoints');
print('New level: $newLevel');
print('Score after bonus and penalty: ${playerScore / 1.5}');
print('Final score with multiplier: $playerScore');
}All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number