basically I have created an object with a method which adds several attributes in the object together. But when I try and call the method to the console log it fires out the code (which is a if statement) to me instead of the value which I was hoping it would return, so I am confused!
Why is this happening ?
Code Below:
var Granite = function(ty, gr, th, wi, le, ed, ad){
this.type = ty;
this.group = gr;
this.thickness = th;
this.width = wi;
this.length = le;
this.edgeProfile = ed;
this.addOns = ad;
this.groupPrice = function(){
if (thickness === 20){
switch(group)
{
case 1:
return 160;
break;
case 2:
return 194;
break;
case 3:
return 244;
break;
case 4:
return 288;
break;
case 5:
return 336;
break;
case 6:
return 380;
break;
default:
return 380;
}
}else{
switch(group)
{
case 1:
return 200;
break;
case 2:
return 242;
break;
case 3:
return 305;
break;
case 4:
return 360;
break;
case 5:
return 420;
break;
case 6:
return 475;
break;
default:
return 475;
}
}
}
this.price = function(){
if(length <= 2000 && length > 1000){
return ((edgeProfile + groupPrice)*2) - addOns;
}else if(length <= 3000 && length > 2000){
return ((edgeProfile + groupPrice)*3) - addOns;
}else if(length <= 4000 && length > 3000){
return ((edgeProfile + groupPrice)*4) - addOns;
}else if(length <= 5000 && length > 4000){
return ((edgeProfile + groupPrice)*5) - addOns;
}
}
}
var granite1 = new Granite("Rosa Porrino", 1, 30, 400, 3200, 30.05, 86.18);
console.log(granite1.groupPrice);
It returns the full if statement within the groupPrice method to me
You are not calling the method but providing reference of function to console,log(). In JavaScript you need use '()' to call a function.
this will surely work console.log(granite1.groupPrice());
In side this.price
use this.groupPrice(). instead of groupPrice
Modified this,price Method
this.price = function(){
if(length <= 2000 && length > 1000){
return ((this.edgeProfile + this.groupPrice())*2) - addOns;
}else if(length <= 3000 && length > 2000){
return ((this.edgeProfile + this.groupPrice())*3) - addOns;
}else if(length <= 4000 && length > 3000){
return ((this.edgeProfile + this.groupPrice())*4) - addOns;
}else if(length <= 5000 && length > 4000){
return ((this.edgeProfile + this.groupPrice())*5) - addOns;
}
}
If you're calling the function, append with () otherwise you're just referencing the function.
Related
I'm new to Javascript, I'm working on a small game to get a better handle of it. I'm trying to define a character object with methods, but for some reason I'm getting weird errors from my IDE, "Label 'updateHealth' on function statement, Missing name in function declaration". I'm just trying to figure out what I'm doing wrong. In my code, display is how the character's health display's on the screen.
function Character(display) {
this.health = 100;
this.display = display;
// updates the health on the screen
updateHealth: function() {
if(health == 100) {
this.display.innerText = 'HP: ' + health;
}
else if(health > 10 && health < 100) {
this.display.innerText = 'HP: 0' + health;
}
else if(health < 10 && health > 0) {
this.display.innerText = 'HP: 00' + health;
}
else {
this.display.innerText = 'HP: 000';
}
}
// returns true if character has died
checkForDeath: function() {
if(health <= 0) return true;
else return false;
}
// function used when damage is inflicted on
// a character object
takeDamange: function(damage) {
this.health -= damage;
}
// handles the four possible moves
// opponent is null because if player heals
// then it does not make sense for there to be
// an opponent
makeMove: function(move, opponent=null) {
switch(move) {
case 'PUNCH':
opponent.takeDamage(parseInt(Math.random() * 100) % 10);
opponent.updateHealth();
break;
case 'HEAL':
this.health += 20;
break;
case 'KICK':
opponent.takeDamage(parseInt(Math.random() * 100) % 20);
opponent.updateHealth();
break;
case 'EXTERMINATE':
opponent.takeDamage(opponent.health);
opponent.updateHealth();
break;
}
return opponent.checkForDeath();
}
}
Object's can be instantiated via a constructor function such as your Character() function however, you'll need to ensure object methods (such as updateHealth(), etc) are "attached" to the instance of the character object.
One way to achieve that is via the this keyword:
/* Attach the checkForDeath() function as a method of "this" Character instance */
this.checkForDeath = function() {
/* Accessing "this" corresponds to the instance of the character object */
if (this.health <= 0) return true;
else return false;
}
By making these changes, checkForDeath() is now defined as a member function of the corresponding character instance. You'll need to ensure that you access fields on the instance via this, as shown on this line if(this.health <= 0) { ... }
You'll also need to ensure that you instantiate instances of Character via the new operator like this:
const characterInstance = new Character( someElement );
Here is a revised version of your code demonstrating this approach:
function Character(display) {
this.health = 100;
this.display = display;
this.updateHealth = function() {
const health = this.health; /* Add this */
if (this.health == 100) {
this.display.innerText = 'HP: ' + health;
} else if (health > 10 && health < 100) {
this.display.innerText = 'HP: 0' + health;
} else if (health < 10 && health > 0) {
this.display.innerText = 'HP: 00' + health;
} else {
this.display.innerText = 'HP: 000';
}
}
this.checkForDeath = function() {
if (this.health <= 0) return true;
else return false;
}
this.takeDamange = function(damage) {
this.health -= damage;
}
this.makeMove = function(move, opponent = null) {
switch (move) {
case 'PUNCH':
opponent.takeDamage(parseInt(Math.random() * 100) % 10);
opponent.updateHealth();
break;
case 'HEAL':
this.health += 20;
break;
case 'KICK':
opponent.takeDamage(parseInt(Math.random() * 100) % 20);
opponent.updateHealth();
break;
case 'EXTERMINATE':
opponent.takeDamage(opponent.health);
opponent.updateHealth();
break;
}
return opponent.checkForDeath();
}
}
const player = new Character( document.querySelector('p') );
player.takeDamange();
player.updateHealth();
<p></p>
change : to =, and assign it to a local property, such as
this.updateHealth = function() {
...
}
I'd recommend using the class syntax.
class Character {
constructor(display) {
this.health = 100;
this.display = display;
}
// updates the health on the screen
updateHealth() {
this.display.innerText = `HP: ${Math.max(health, 0).toString().padStart(3, '0')}`;
}
// returns true if character has died
checkForDeath() {
return health <= 0;
}
// function used when damage is inflicted on
// a character object
takeDamange(damage) {
this.health -= damage;
}
// handles the four possible moves
// opponent is null because if player heals
// then it does not make sense for there to be
// an opponent
makeMove(move, opponent = null) {
switch (move) {
case 'PUNCH':
opponent.takeDamage(parseInt(Math.random() * 100) % 10);
opponent.updateHealth();
break;
case 'HEAL':
this.health += 20;
break;
case 'KICK':
opponent.takeDamage(parseInt(Math.random() * 100) % 20);
opponent.updateHealth();
break;
case 'EXTERMINATE':
opponent.takeDamage(opponent.health);
opponent.updateHealth();
break;
}
return opponent.checkForDeath();
}
}
I also did some slight refactoring, which should make it easier to understand what is happening.
My task was to try and code the game of Pig. I am trying to have the code use a switch statement to determine which chunk of code to follow but it is skipping case 1 and case 2 and going directly to the default case. The roll.score is coming from this Javascript file:
function Dice(d1, d2){ //d1 = die 1 d2 = die 2
this.d1 = d1?d1:parseInt(Math.random()*6 + 1);
this.d2 = d2?d2:parseInt(Math.random()*6 + 1);
}
Dice.prototype.score = function(){ //d1 = die 1 d2 = die 2
if(this.d1 == 1 || this.d2 == 1){
return 1; //return score 0 for turn
}else if(this.d1 == 1 && this.d2 == 1){
return 2; //return 13 as code to reset score to 0
}else
return parseInt(this.d1 + this.d2);
}
Dice.prototype.toString = function(){
return "Rolled " + this.d1 + " and " + this.d2;
}
What it is supposed to do is return either 1, 2, or whatever the 2 number added together are. Like I mentioned above, no matter what the roll.score() returns, the switch statement always goes to the default case.
var again = true;
do {
var roll = new Dice(parseInt(Math.random() * 6 + 1), parseInt(Math.random() * 6 + 1));
window.alert(roll.toString());
turnCounter++;
switch (roll.score) {
case 1: // 1 die = 1
playerScore = roll.score();
again = false;
rollCounter++;
turnCounter++;
document.write("Enters case 1");
break;
case 2: //2 = snake eyes
playerTotal = 0;
playerScore = 0;
again = false;
rollCounter++;
turnCounter++;
break;
default:
playerScore += roll.score();
rollCounter++;
displayScore();
document.write(roll.score() + "<br/>");
var rollAgain = window.prompt("Do you want to roll again?(Y/N)");
if (rollAgain.toUpperCase() === "N") {
again = false;
playerTotal += playerScore;
displayScore();
turnCounter++;
if (playerScore > highScore)
highScore = playerScore;
}
break;
}
rollCounter++;
}while (again);
switch (roll.score) { is not the same as switch (roll.score()) {
roll.score is a function, whereas you want to switch on the result on the returned result (roll.score()).
I recently completed a problem in CodeWars using an if-else statement, but I wanted to retry it and use a switch statement instead. Too bad that it is not working the way that I thought it would!
The problem I am solving is to take in a distance(s) that an Ironman triathlon athlete has completed and return an object that shows a key based on whether the athlete should be Swimming, Biking or Running with a value of the length of the race to go.
My If-Else Solution:
function iTri(s) {
var triLength = 140.60;
var result = {};
var str = ' to go!';
var lengthLeft = (triLength - s).toFixed(2);
if (s === 0) {
return 'Starting Line... Good Luck!';
} else if (s <= 2.4) {
result.Swim = lengthLeft + str;
} else if (s <= 114.4) {
result.Bike = lengthLeft + str;
} else if (s < 130.60) {
result.Run = lengthLeft + str;
} else if (s < 140.60) {
result.Run = 'Nearly there!';
} else {
return 'You\'re done! Stop running!';
}
return result;
}
The (non-working) Switch statement:
function iTri(s){
let tri = (2.4 + 112 + 26.2).toFixed(2);
let left = tri - s;
let str = ' to go!'
let result = {};
switch(s) {
case (s === 0):
return "Starting Line... Good Luck!";
break;
case (s <= 2.4):
result.Swim = left + str;
return result;
break;
case (s <= 114.4):
result.Bike = left + str;
return result;
break;
case (s <= 130.60):
result.Run = left + str;
return result;
break;
case (s < 140.60):
result.Run = 'Nearly there!';
return result;
break;
default:
return 'You\'re done! Stop running!';
}
}
These are the tests:
Test.describe("Example tests",_=>{
Test.assertSimilar(iTri(36),{'Bike':'104.60 to go!'});
Test.assertSimilar(iTri(103.5),{'Bike':'37.10 to go!'});
Test.assertSimilar(iTri(2),{'Swim':'138.60 to go!'});
});
And the Output:
✘ Expected: '{ Bike: \'104.60 to go!\' }', instead got: '\'You\\\'re done! Stop running!\''
✘ Expected: '{ Bike: \'37.10 to go!\' }', instead got: '\'You\\\'re done! Stop running!\''
✘ Expected: '{ Swim: \'138.60 to go!\' }', instead got: '\'You\\\'re done! Stop running!\''
Also is it worth it to convert it to a switch statement? What are benefits/drawbacks of doing it as if/else vs switch?
Is it even worth it to try to convert it to a switch statement?
No. switch is only useful if you have multiple exact matches. This is not the case for you.
If you turn the problem around slightly then you can use a switch like this. Your switch is very close to this already
function iTri(s) {
var triLength = 140.60;
var result = {};
var str = ' to go!';
var lengthLeft = (triLength - s).toFixed(2);
switch(true) {
case s === 0:
return 'Starting Line... Good Luck!';
case s <= 2.4:
result.Swim = lengthLeft + str;
break;
case s <= 114.4:
result.Bike = lengthLeft + str;
break;
case s < 130.60:
result.Run = lengthLeft + str;
break;
case s < 140.60:
result.Run = 'Nearly there!';
break;
default:
return 'You\'re done! Stop running!';
}
return result;
}
console.log(iTri(0));
console.log(iTri(2));
console.log(iTri(50));
console.log(iTri(120));
console.log(iTri(135));
console.log(iTri(145));
A little while ago I needed help with a complicated switch, but now I am substituting the direct variables for arrays. I thought it best to use a for statement, and it isn't displaying the results.
This is a side project for me, so please let me know if there are any aspects you see that could be improved.
<script>
var strMod=0;
var dexMod=0;
var conMod=0;
var intMod=0;
var wisMod=0;
var chaMod=0;
var strength = prompt("what is your strength?");
var dexterity = prompt("what is your dexterity?");
var constitution = prompt("what is your constitution?");
var intelligence = prompt("what is your intelligence?");
var wisdom = prompt("what is your wisdom?");
var charisma = prompt("what is your charisma?");
var abilities=[strength,dexterity,constitution,intelligence,wisdom,charisma];
var abiMod=[strMod,dexMod,conMod,intMod,wisMod,chaMod];
for (var i=0; i<abilities.length;i++){
switch(true){
case (abilities(i)>=0 && abilities(i)<2 && abilities(i)!==null):
abiMod(i)=-5;
break;
case (abilities(i)>=2 && abilities(i)<4):
abiMod(i)=-4;
break;
case (abilities(i)>=4 && abilities(i)<6):
abiMod(i)=-3;
break;
case (abilities(i)>=6 && abilities(i)<8):
abiMod(i)=-2;
break;
case (abilities(i)>=8 && abilities(i)<10):
abiMod(i)=-1;
break;
case (abilities(i)>=10 && abilities(i)<12):
abiMod(i)=0;
break;
case (abilities(i)>=12 && abilities(i)<14):
abiMod(i)=1;
break;
case (abilities(i)>=14 && abilities(i)<16):
abiMod(i)=2;
break;
case (abilities(i)>=16 && abilities(i)<18):
abiMod(i)=3;
break;
case (abilities(i)>=18 && abilities(i)<20):
abiMod(i)=4;
break;
case (abilities(i)>=20 && abilities(i)<22):
abiMod(i)=5;
break;
default:
abiMod(i)= prompt("what is your"+ abilities(i) +"modifier?");
break;
};
alert(abiMod(i));
};
</script>
Rather than using the switch statement, there is a common theme across all case and so a mathematical formula can be used instead.
Here is the revised code
for (var i = 0; i < abilities.length; i++) {
if (abilities[i] !== null && abilities[i] >= 0 && abilities[i] < 22) {
abiMod[i] = Math.floor(abilities[i] / 2) - 5;
}
else {
abiMod[i] = prompt("what is your "+ abilities[i] +" modifier?");
}
};
For starters, you need to access array indices via brackets, not parens.
for (var i=0; i<abilities.length;i++){
switch(true){
case (abilities[i]>=0 && abilities[i]<2 && abilities[i]!==null):
abiMod[i]=-5;
break;
case (abilities[i]>=2 && abilities[i]<4):
abiMod[i]=-4;
break;
case (abilities[i]>=4 && abilities[i]<6):
abiMod[i]=-3;
break;
case (abilities[i]>=6 && abilities[i]<8):
abiMod[i]=-2;
break;
case (abilities[i]>=8 && abilities[i]<10):
abiMod[i]=-1;
break;
case (abilities[i]>=10 && abilities[i]<12):
abiMod[i]=0;
break;
case (abilities[i]>=12 && abilities[i]<14):
abiMod[i]=1;
break;
case (abilities[i]>=14 && abilities[i]<16):
abiMod[i]=2;
break;
case (abilities[i]>=16 && abilities[i]<18):
abiMod[i]=3;
break;
case (abilities[i]>=18 && abilities[i]<20):
abiMod[i]=4;
break;
case (abilities[i]>=20 && abilities[i]<22):
abiMod[i]=5;
break;
default:
abiMod[i]= prompt("what is your"+ abilities[i] +"modifier?");
break;
};
alert(abiMod[i]);
};
</script>
Thanks for the response. I have solved my problem. I really did see that its a list of callback functions. After some work i managed to shoot by intervals, but the first shot was after 1 second.
1 - a problem - if I call the function in setInterval imidiatly and then set interval - shoots rapidly.
2 - I fixed the problem by making setTimeout to set a bool value hasShooted to false after 1 second and if that value is false i can shoot. In the function i do that i set it to true.
3 - I realized I need only that last function with set timeout and not setInterval at all.
var PlayerManager = (function(parent){
'use strict';
var bulletPossLeft,
bulletPossTop,
FIRE_SPEED = 1000,
hasShot = false;
PlayerManager.prototype = Object.create(parent.prototype);
function PlayerManager() {
parent.call(this);
this.moveLeft= false;
this.moveRight= false;
this.moveForward= false;
this.moveBack= false;
this.isShooting= false;
this.bulletManager = new BulletManager();
}
PlayerManager.prototype.onGameLoop = function(obj) {
if (this.isShooting) {
bulletPossLeft = obj.positionLeft + Math.floor(obj.planeWidth /2);
bulletPossTop = obj.positionTop - Math.ceil(obj.planeHeight /2);
if(!hasShot){
this.shoot();
hasShot = true;
setTimeout(function(){
hasShot = false;
}, FIRE_SPEED);
}
}
if (this.moveLeft && (obj.positionLeft - obj.speed) > 0) {
obj.positionLeft -= obj.speed;
}
if (this.moveRight && (obj.positionLeft + obj.speed) < Game.getContextValue('width')) {
obj.positionLeft += obj.speed;
}
if (this.moveForward && (obj.positionTop - obj.speed) > 0) {
obj.positionTop -= obj.speed;
}
if (this.moveBack && (obj.positionTop + obj.speed) < Game.getContextValue('height')) {
obj.positionTop += obj.speed;
}
obj.move();
};
PlayerManager.prototype.shoot = function(){
this.bulletManager.spawn(new Bullet(bulletPossLeft, bulletPossTop, 'orange'));
};
PlayerManager.prototype.keyboardListener = function(e) {
var value = e.type == 'keydown';
switch (e.keyCode) {
case 37:
this.moveLeft = value;
break;
case 38:
this.moveForward = value;
break;
case 39:
this.moveRight = value;
break;
case 40:
this.moveBack = value;
break;
case 32:
this.isShooting = value;
break;
default:
break;
}
};
return PlayerManager;
})(Manager);
You are setting a new interval every time onGameLoop is executed and this.isShooting equals to true. Therefore when you use clearInterval, you are clearing only the last interval, not all of them.
I recommend you clearing the variable shootInterval after clearing interval (for example: shootInterval = null;) and in the first condition (if (this.isShooting)) check if shootInterval is not null.
Your code should look like this:
var bulletPossLeft,
bulletPossTop,
fireSpeed = 1000,
shootInterval,
self;
PlayerManager.prototype = Object.create(parent.prototype);
function PlayerManager() {
parent.call(this);
this.moveLeft= false;
this.moveRight= false;
this.moveForward= false;
this.moveBack= false;
this.isShooting= false;
this.bulletManager = new BulletManager();
self = this;
}
PlayerManager.prototype.onGameLoop = function(obj) {
if (this.isShooting && shootInterval == null) {
bulletPossLeft = obj.positionLeft + Math.floor(obj.planeWidth /2);
bulletPossTop = obj.positionTop - Math.ceil(obj.planeHeight /2);
shootInterval = setInterval(function(){
self.shoot();
} , fireSpeed);
}
if(!this.isShooting) {
clearInterval(shootInterval);
shootInterval = null;
}
if (this.moveLeft && (obj.positionLeft - obj.speed) > 0) {
obj.positionLeft -= obj.speed;
debugger;
}
if (this.moveRight && (obj.positionLeft + obj.speed) < Game.getContextValue('width')) {
obj.positionLeft += obj.speed;
}
if (this.moveForward && (obj.positionTop - obj.speed) > 0) {
obj.positionTop -= obj.speed;
}
if (this.moveBack && (obj.positionTop + obj.speed) < Game.getContextValue('height')) {
obj.positionTop += obj.speed;
}
obj.move();
};
PlayerManager.prototype.shoot = function(){
this.bulletManager.spawn(new Bullet(bulletPossLeft, bulletPossTop, 'orange'));
};
PlayerManager.prototype.keyboardListener = function(e) {
var value = e.type == 'keydown';
switch (e.keyCode) {
case 37:
this.moveLeft = value;
break;
case 38:
this.moveForward = value;
break;
case 39:
this.moveRight = value;
break;
case 40:
this.moveBack = value;
break;
case 32:
this.isShooting = true;
break;
default:
break;
}
if(e.type == 'keyup'){
this.isShooting = false;
}
};
return PlayerManager;
})(Manager);