Covering Losses


#1

PLease I would like to show how many strawberries was picked but my code doesn´t work!

https://www.codecademy.com/en/courses/introduction-to-javascript/0/5#

```

var earnings = 0;
var berries = 0;
//Fill in the outer loop with an inner loop that loops through
//10 strawberries and increment your earnings by 2.25 for every
//extra strawberry that you pick
for (var days = 7; days > 0; days–){
{
for (var strawberries=10; strawberries > 0; strawberries–){
if (strawberries > 0){
earnings = earnings + 2.25;
}
}
}
}
//Insert inner for loop here
for (var days = 7; days > 0; days–){
for (var berries=0; berries < 10;berries++){
if(berries <10){
berries=berries + 1;
}
}
}
//Boolean that returns true if your losses have been covered
if (earnings >= 225) {
var lossesCovered = true
}else{
var lossesCovered = false
}
console.log(berries)
console.log(earnings)
console.log(lossesCovered)


#2

Not needed. That line may be removed.

All of those lines may be removed.

That line should be removed.

That should be replaced with,

earnings = earnings + 2.25;    // or, earnings += 2.25

var keywords do not belong in if statements. Declare the variable beforehand.

var lossesCovered = false;
if (earnings >= 225) {
    lossesCovered = true;
}

This meets the requirements of the instructions. You will learn later on how booleans help us to short-circuit and simplify code.

var lossesCovered = earnings >= 225;

Says it all. No if’s, and’s or but’s. It is or it isn’t. Binary. THat is what to think when looking at an if , for or while conditional. They are all boolean.

console.log(`Losses covered? `, earnings >= 225 ? `Yup!` : `Nope!`);

If you have any trepidations at this point with regard to booleans, stop and spend the time to really dig into the subject. Master the condtional and you master control flow. It’s not something we can gloss over and move on.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.