I am trying to subtract the value of object1's objectProperty from object2's object property and I keep getting NaN on the console. Here is example code:
object1.objectProperty - object2.object2Property
If this isn't enough to go off, I can post the full code from my project.
If there is another way to do this or some kind of function that can help, please let me know.
edit: Here is the code..
var myPokemon = {
health: 25,
defense: 5,
attack: 10,
speed: 5
};
var moves = {
Scratch: 5,
Bite: 5,
Slap: 5,
Growl: 1
};
var computerPokemon = {
health: 20,
defense: 5,
attack: 10,
speed: 7
};
function calcDamage(firstPokemon, secondPokemon, move) {
if(move == moves.Growl){
//starts here
var newDefense = moves.Growl - firstPokemon.defense;
console.log(newDefense);
//ends here
}else{
var newHealth = (firstPokemon.health + firstPokemon.defense) - (secondPokemon.attack + move);
console.log(newHealth);
}
}
edit: When I did
moves.Growl - firstPokemon.defense || 0; it returned -4 instead of NaN which is what I wanted it to do, but the person that answered that removed the answer so this has been answered by whoever that guy was.
The problem is that you are adding the object in the second argument. Also your if statement will never execute, I have fixed both as following
var myPokemon = {
health: 25,
defense: 5,
attack: 10,
speed: 5
};
var moves = {
Scratch: 5,
Bite: 5,
Slap: 5,
Growl: 1
};
var computerPokemon = {
health: 20,
defense: 5,
attack: 10,
speed: 7
};
function calcDamage(firstPokemon, secondPokemon, move) {
if(moves.Growl!=undefined){
//starts here
var newDefense = moves.Growl - firstPokemon.defense;
alert(newDefense);
//ends here
}else{
var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl));
alert(newHealth);
}
}
calcDamage(myPokemon,computerPokemon,moves)
Usually, if you are getting NaN, you are probably working with other elements but numbers. Are you sure they both are numbers?
Just an example:
var x = {}, y = {};
x.r = 10;
y.r = 5;
x.r - y.r; // yields 5
Use parseInt to convert the values into integer and then do the math.
var value = parseInt(object1.objectProperty,10) - parseInt(object2.object2Property,10);
The Problem is here
var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl));
The last one Number(move.Growl) it should be Number(moves.Growl) moves not move. In your case move is just a number and you are trying Number(move.Growl) which will be NaN and hence your getting NaN.
Related
i´m new to JavaScript and learning with the Help of the Website https://www.jshero.net/koans/roman1.html.
The exercise is to code a converter, that converts roman numbers from a string 'CDLXXXIII' to the arabic number.
I made a code with a "while loop" that works, but the website wants me to do it with a recursive function.
Heres my code:
function roman(roemische){
let romBuchstaben = ['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM', 'M'];
let romZahlen = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
let summe = 0;
while (roemische.length > 0){
let suchzeichen = roemische[0] + roemische[1];
if (romBuchstaben.indexOf(suchzeichen) !== -1){
summe += romZahlen[romBuchstaben.indexOf(suchzeichen)];
roemische = roemische.substr(2,roemische.length-2);
} else {
summe += romZahlen[romBuchstaben.indexOf(roemische[0])];
roemische = roemische.substr(1, roemische.length-1);
}
}
return summe;
}
(I´m sorry that the var's are in german).
I´m not that familiar with recursion, could some1 give me an example, how to do it with one?
Greetings Marcel
You could change the storage of the values a bit by taking an object with roman signs as keys and decimal values.
For crating a recursive function, you could add an exit condition which is here just a check for an empty string and return zero in this case.
Then check if two character are in the object and if so take the value and add the result of calling the function again with the rest of the string.
If not take only the first character and the value and call the function again for getting the rest of the string.
function roman(number) {
const
values = { I: 1, IV: 4, V: 5, IX: 9, X: 10, XL: 40, L: 50, XC: 90, C: 100, CD: 400, D: 500, CM: 900, M: 1000 },
two = number.slice(0, 2);
if (!number) return 0;
return two in values
? values[two] + roman(number.slice(2))
: values[number[0]] + roman(number.slice(1));
}
console.log(roman('CDLXXXIII')); // 483
This might sound like a simple thing for you programmers out there but i can't seem to figure it out. I'm making a program that converts Decimal Numbers into Roman Numerals. I've got the core code working but i can't seem to think how i can display the results on the screen. I would like it so that when the user types a number into a text box the result appears in another text box at the press of a button. Thanks for your time & help.
function convertToRoman(num) {
var romans = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1,
};
var result = '';
for (var key in romans) {
if (num >= romans[key]) {
result += key.repeat(Math.trunc(num / romans[key]));
num -= romans[key] * Math.trunc(num / romans[key]);
}
}
return result;
}
If you add a div element and give it an id like "output", you can do something like this.
document.getElementById("output").innerHTML = result;
You can use the trusty old alert function. Just do alert(convertToRoman(10)). Put this in a <script> tag or inside body onload attribute
For fancier more professional debugging, you can use console.log. Originating with an old Firefox addon called Firebug, it is now supported by all browsers. Simply type console.log(convertToRoman(10)); The result will be printed out to console, to see it, right click your browser, and open inspect document and move to Console tab, or alternatively, Ctrl+Shift+I in Chrome
You are missing the part where you attach the result to the html. See working code below as an example.
function convertToRoman(num) {
var romans = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1,
};
var result = '';
for (var key in romans) {
if (num >= romans[key]) {
result += key.repeat(Math.trunc(num / romans[key]));
num -= romans[key] * Math.trunc(num / romans[key]);
}
}
document.getElementById('output').innerHTML = result;
}
convertToRoman(12345);
<div> my result <span id="output"></span></div>
I like to challenge myself just for fun and I've come across to a problem which I just simply can not solve on my own. I've tried and tried, but I can not find a solution to it and that's why I came here to ask from you.
So let's assume we have three different arrays:
var y1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var y2 = [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ];
var y3 = [ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ];
We also have five different integers:
var z1 = 200;
var z2 = 400;
var z3 = 600;
var z4 = 800;
var z5 = 1000;
How am I able to see the largest possible number, when the formula looks something like this:
x = z1*y1[0] + z2*y1[0] + z3*y2[0] + z4*y2[0] * z5*y3[0];
... and I only have 25 points to use to replace the zeros in the equation?
For example:
z1*y1[5] + z2*y1[5] + z3*y2[5] + z4*y2[5] * z5*y3[5] = 32000 (5+5+5+5+5 = 25)
z1*y1[0] + z2*y1[10] + z3*y2[0] + z4*y2[10] * z5*y3[5] = 35000 (0+10+0+10+5 = 25)
I think I know how it should be done (backtracking?) but I just can't figure out how to o it exactly. I also couldn't find this type of a problem from the Internet, so if you know where to look that would be great as well because I'm not looking for a direct answer, I'd like to learn.
So any help would be much appreciated!
You can think of it as having five machines(m) that needs to perform one job(n), of varying processing times(z), and y denote when to schedule each job.
Assuming a ascending heuristic:
max(25) = z1*y1[0] + z2*y1[0] + z3*y2[5] + z4*y2[10] * z5*y3[10] Result: 480006000
Trying to get the code to divide by 3,5,& 15. Keep getting syntax error.
//dividing by three
var pingpong = function(number) {
var threenumbers = ['3,6,9,12'];
var bythree = [];
threenumbers.forEach(function(number) {
bythree.push(number / 3);
{
var fivenumbers = ['5,10,15,20'];
var byfive = [];
fivenumbers.forEach(function(number){
byfive.push(number / 5);
var fifteennumbers = ['15, 25, 30, 36'];
var byfifteen = [];
fifteennumbers.forEach(function(number) {
byfifteen.push( number / 15);
};
how do I get it to run the code and divide it by the numbers?
uncaught syntax error?
I don't understand what you want to happen but ...
1- the data in the arrays threenumbers fivenumbers and fifteennumbers are strings not number.
2- as said in 1 the arrays have one string each so using forEach(function(number){ will iterate one time setting number to "3,6,9,12" "5,10,15,20" "15, 25, 30, 36".
3- you had a bunch of parenthesis open but not closed.
var pingpong = function(number) {
var threenumbers = [3,6,9,12];
var bythree = [];
threenumbers.forEach(function(number) {
bythree.push(number / 3);
var fivenumbers = [5,10,15,20];
var byfive = [];
fivenumbers.forEach(function(number){
byfive.push(number / 5);
var fifteennumbers = [15, 25, 30, 36];
var byfifteen = [];
fifteennumbers.forEach(function(number) {
byfifteen.push( number / 15);
});
});
});
}
pingpong(40);
this doesn't yell errors at you but also probably doesn't do what you want as I said I really dont know what you want
I have a list of FICO scores that I'd like to group depending on their value, with groups starting at 0-620, then 620-640, incrementing by 20 every time.
What it should return is a count of values in each group -- 0-620, 620-640, 640-660, 660-680, etc. up to 780-800.
I'm currently using _.countBy but it returns a count for every unique number, rather than for every group.
var counts = _.countBy(ficos, function(x) { return x });
//Counts should be {620: 22, 625: 19, 655: 24, 670: 20, 690: 30, 720: 29, 734: 17, 760: 21, 790: 18}
Is there a way to take advantage of _.countBy or some other function? I'm trying to avoid a long if statement, if possible.
So return the appropriate groups then:
function (x) {
if (x < 620) return '<620';
if (x >= 800) return '>800';
var lower = Math.floor(x / 20) * 20,
higher = lower + 20;
return lower + '-' + higher;
}