I have this function that is supposed to loop through these additional functions while they won't take the totals up past 100. However, the iteration stops and I need it to finish up with the hunger and danger value either being 100 or 0 depending on which. The numbers that pass through are first: (50,50) second: (0, 100). I can't seem to figure out how to change the addition/subtractions amount when the if condition is no longer met. My consoles are all displaying the last iteration before going over the conditional values.
The problem is:
Create a function called frodo. frodo will take in two parameters: startingHungerValue (Number) and startingDangerValue (Number). frodo will need to store those values on internal variables.
frodo will then return an object with two methods: The first method will be called dinnerOverFire. dinnerOverFire will decrease hunger by 25 and will increase danger by 40.
The second method will be called hidingInBush. hidingInBush will increase hunger by 35 and decrease danger by 20.
Both methods need to return an object structured like this:
{ hunger: (modified hunger value), danger: (modified danger value) }
NOTE: Neither hunger nor danger should be able to exceed 100 or drop below 0.
function frodo(startingHungerValue, startingDangerValue) {
var shv = startingHungerValue;
var sdv = startingDangerValue;
console.log('startingHungerValue:', shv, 'startingDANGERvalue:', sdv)
return {
dinnerOverFire: () => {
if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
return {
hunger: shv - 25,
danger: sdv + 40
}
}
},
hidingInBush: () => {
if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
return {
hunger: shv + 35,
danger: sdv - 20
}
}
}
}
}
Not directly what you asked for, but I have a feeling you want to do something like below. It creates a class Frodo that you can call methods on like eat or hide.
You can see it in action here: jsfiddle
function Frodo(startingHungerValue, startingDangerValue) {
var self = this; //this could change per scope, self keeps a reference to the basic of the class
//store levels
self.hunger = startingHungerValue;
self.danger = startingDangerValue;
//show levels method
self.showLevels = function(){
console.log('My hungerlevel: '+ self.hunger);
console.log('My dangerlevel: '+ self.danger);
console.log('------');
}
//dinner method
self.dinnerOverFire = function() {
var newHunger = self.hunger - 25;
var newDanger = self.danger + 40;
if (newHunger<0) {
newHunger = 0; //no negatives
}
if (self.hunger==0) {
console.log('I\'m not hungry! No dinner!');
console.log('------');
return;
}
if (newDanger>100) {
console.log('Eating now would kill me! No dinner!');
console.log('------');
return;
}
self.hunger = newHunger;
self.danger = newDanger;
console.log('Thanks for dinner!');
self.showLevels();
}
//hiding method
self.hideInBush = function() {
var newHunger = self.hunger + 35;
var newDanger = self.danger - 20;
if (newDanger<0) {
newDanger = 0; //no negatives
}
if (newHunger>100) {
console.log('Hiding now would kill me! No hiding!');
console.log('------');
return;
}
if (self.danger==0) {
console.log('I\'m not scared at all! No hiding!');
console.log('------');
return;
}
self.hunger = newHunger;
self.danger = newDanger;
console.log('Thanks, I feel safer already!');
self.showLevels();
}
//initial message
console.log('Hi, i\'m frodo!');
self.showLevels();
}
//run your frodo
var frodo = new Frodo(50,50);
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
This would output:
Hi, i'm frodo!
My hungerlevel: 50
My dangerlevel: 50
------
Thanks for dinner!
My hungerlevel: 25
My dangerlevel: 90
------
Thanks, I feel safer already!
My hungerlevel: 60
My dangerlevel: 70
------
Eating now would kill me! No dinner!
------
Thanks, I feel safer already!
My hungerlevel: 95
My dangerlevel: 50
------
Thanks for dinner!
My hungerlevel: 70
My dangerlevel: 90
------
Hiding now would kill me! No hiding!
------
Eating now would kill me! No dinner!
------
Which already shows a problem with the current way. At the end he's to scared to eat and to hungry to hide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With