I'm new to coding, and this is my first post. Any help you can offer is greatly appreciated. I'm trying to create a level calculation system that you might see in an RPG for a character with different stats. When I run the program at different values, I get the following message:
Unexpected token 'this'
I have put the section of code where the error message occurs in stars.
const Sean = {
fullName: `Sean`,
attack: 1,
strength: 1,
defense: 38,
hitpoints: 40,
prayer: 15,
range: 98,
magic: 99,
calcCombatLevel: function () {
this.Meleelvl = .25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.attack + this.strength));
this.magiclvl = .25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.magic * (3 / 2)));
this.rangelvl = 0.25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.range * (3 / 2)));
if ((this.attack + this.strength) > ((2 / 3) * this.magic) && (this.attack + this.strength) > ((2 / 3) * this.range)) {
return this.meleelvl;
}
// **********************************
else if ((2 / 3) this.magic > (this.attack + this.strength) && (this.magic >** this.range)) {
return this.magiclvl;
}
else if((2 / 3) * this.range > (this.attack + this.strength) && (this.range > this.magic)) {
return this.rangelvl;
}
}
}
Sean.calcCombatLevel();
console.log(Math.floor(Sean.lvl));
The first elseif has:
(this.magic >** this.range)
// also missing * at start, has (2/3) magic, where the 'this' error is from
which I assume you meant to be:
else if ((2 / 3) * this.magic > (this.attack + this.strength) && (this.magic > this.range)) {
Didn't play with the code but that's the only first obvious thing that stands out to me.
Also as someone commented above Sean.lvl doesn't seem to be defined, so the output at end probably isn't working either. You could do something like the following instead (or define a field named lvl):
var mylvl = Math.floor(Sean.calcCombatLevel());
console.log(mylvl);
And at least in one place your variable names have inconsistent casing, this.Meleelvl and this.meleelvl, fix that too (first line in function should have this.meleelvl, keeping all variable names lowercase).
Related
I'm trying to validate in Javascript that a number is in increments of 50, if not, then throw a validation error. For example:
123 - invalid, can either be 100 or 150
272 - invalid, can either be 200 or 250 or 300
etc...
I'm thinking that the % remainder operator is what I need to use but not quite sure how to build a javascript validation rule to match this.
Are you looking for something like:
(val) => {
var remainder = val % 50;
if (remainder !== 0) {
var lower = val - remainder;
var higher = lower + 50;
throw new Error(val + ' - invalid, can either be ' + String(lower) + ' or ' + String(higher));
}
}
That could be reduced, but this way you can see the logic at work.
This is the math you want to preform:
Math.round(123/ 50)*50; //this gives 100
Math.ceil(123/ 50)*50; //this gives 150
and here is the validation function
function validate(number) {
var round = Math.round(number / 50) * 50;
var ceil = Math.ceil(number / 50) * 50;
if (number == round) { return console.log("valid"); }
return console.log(`${number} - invali, can eiher be ${round} or ${ceil}`);
}
I am attempting to write a function which provides results similar to the JavaScript built-in math.sqrt function. I am using the trial and error method where the function guesses, and then subsequently refines the guess for the square root of the number the user enters into the program, until the correct value is successfully reached.
Currently a recursive function with an if.. else statement is used to perform the calculations, and adjust the guess until it reaches the user defined target number when squared.
The function provides the correct answer for 11, 22, 33, 44, 55, any number where the square root is not a decimal, among others. However it returns an error - too much recursion with other numbers such as 13, although the program gets very close to solving the problem - usually just .00000000000000x above or below the target number.
This function has gone through multiple iterations where I finally settled on using two arrays to store previous guesses, sorted into one or the other depending on whether they were too large or too small, and storing the latest guess from each array in a variable which can be used in calculating the next guess.
Previous iterations were reaching the error of too much recursion much earlier, and were only returning correct results for numbers where the correct answer was arrived at within the first run through of the function, otherwise my browser would crash.
// HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Function library example</title>
<style>
input {
font-size: 2em;
margin: 10px 1px 0;
}
</style>
</head>
<body>
<input class="numberInput" type="text">
<p></p>
// JavaScript
<script>
var input = document.querySelector('.numberInput');
var para = document.querySelector('p');
function findSquareRoot(num){
function find(guess, square, lastSmallGuess, lastLargeGuess){
var smallerArray = [];
var greaterArray = [];
if(square == num){
return guess;
} else if(square > num){
greaterArray.push(guess);
console.log('Last Small Guess : ' + lastSmallGuess); // shows guess in developer tools
console.log(' Last Large Guess : ' + lastLargeGuess); // shows guess in developer tools
console.log((((guess * 1) + (lastSmallGuess * 1)) / 2), (((guess * 1) + (lastSmallGuess * 1)) / 2) * (((guess * 1) + (lastSmallGuess * 1)) / 2), lastSmallGuess, greaterArray[greaterArray.length-1]); // shows calculation process in dev tools
return find((((guess * 1) + (lastSmallGuess * 1)) / 2), (((guess * 1) + (lastSmallGuess * 1)) / 2) * (((guess * 1) + (lastSmallGuess * 1)) / 2), lastSmallGuess, greaterArray[greaterArray.length-1]);
} else if(square < num){
smallerArray.push(guess);
console.log('Last Small Guess : ' + lastSmallGuess); // shows guess in developer tools
console.log(' Last Large Guess : ' + lastLargeGuess); // shows guess in developer tools
console.log((((guess * 1) + (lastLargeGuess * 1)) / 2), (((guess * 1) + (lastLargeGuess * 1)) / 2) * (((guess * 1) + (lastLargeGuess * 1)) / 2), smallerArray[smallerArray.length-1], lastLargeGuess); // shows calculation process in dev tools
return find((((guess * 1) + (lastLargeGuess * 1)) / 2), (((guess * 1) + (lastLargeGuess * 1)) / 2) * (((guess * 1) + (lastLargeGuess * 1)) / 2), smallerArray[smallerArray.length-1], lastLargeGuess);
}
}
return find((num * 1) / 2, ((num * 1) / 2) * ((num * 1) / 2), 0, 0);
}
input.onchange = function() {
var num = input.value;
if(isNaN(num)) {
para.textContent = 'You need to enter a number!';
} else {
para.textContent = num + ' square root is ' + findSquareRoot(num) + '. ' +
num + ' square root is ' + Math.sqrt(num) + '.';
}
}
</script>
</body>
</html>
I expect my function to consistently deliver the same answer as the built in math.sqrt function. Although even when the function causes the browser to crash and therefore does not return the result, it has arrived at an answer very close to the correct one, as shown in the console section of the developer tools. With other numbers it returns precisely the same result as math.sqrt
Thank you for taking the time to read my question.
So I am calculating values, snapshotting them and send them into a interval. But the issue is, is its wildly increasing what the sum should be.
Here is the calculations:
// Set up mob attributes - SNAPSHOT
const mobId = '1';
var mob = {
mhp: (mobId + 10) * 15,
hp: (mobId + 10) * 15,
dmg: (mobId + 2) * 3,
defenseRating: (mobId + 3) * 4,
hitRating: (mobId + 2) * 2,
critRating: (mobId + 3) * 2,
critDmg: (mobId + 2) * 2,
dodgeRating: (mobId + 1) * 3,
firstHitRating: (mobId + 3) * 4,
};
console.log(mob);
I don't know about you, but in what world does 11 * 15 = 1650. This is the calculations BEFORE I send it through the interval, so it isn't messing up there. It is the intial calculations that being very weird.
Note: It may be worth noting that I am doing this through a socket event:
socket.on('fight mob', (mobId) => {
I doubt that is the issue but I am at a loss right now.
This is happening because your mobId variable is a string, not a number.
(1 + 10) * 15 = 165
("1" + 10) * 15 = 1650
To resolve this, you can coerce mobId's type using Number():
(Number(mobId) + 10) * 15 = 165
Good luck!
Due to loose typing, JavaScript automatically tries to change the types of variables when performing operations on them. In your case, mobId is a string and not a number. So, instead of addition, JavaScript performs concatenation.
So the operation
(mobId + 10) * 15
becomes something like this for mobId being '1':
'110' * 15
Again, JavaScript's type caster kicks in, but this time it converts the string to number for multiplication, so the result becomes 1650.
To mitigate, convert the mobId to number first. Use parseInt or parseFloat for that:
mobId = parseInt(mobId, 10);
(mobId + 10) * 15 // Yields 165
In the interest of better understanding recursion, I'm trying to figure out how to log a recursive trace to the console. I've got the 'trace down' part, but I'm not sure how to 'bubble up' the solution. Any suggestions for a perfectly placed console.log statement?
Here's what I've got so far:
function factorial (num) {
if (num === 1) {
console.log('factorial(' + num + ') = ' + num);
return 1;
} else {
console.log('factorial(' + num + ') = ' + num + ' * ' + 'factorial(' + (num - 1) + ')');
return num * factorial(num - 1);
}
}
which prints the following to the console:
factorial(5) = 5 * factorial(4)
factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1
120
But what about the 1 * 2 * 3 * 4 * 5 part? I know it's happening in there somewhere, how can I print it?
I guess I'd expect it to look something like this:
1
1 * 2
2 * 3
6 * 4
24 * 5
120
Thanks for any suggestions!
Ok after more searching I found this over at CodeRanch, unfortunately sans code (and written in Java):
Enter fact(6)
Enter fact(5)
Enter fact(4)
Enter fact(3)
Enter fact(2)
Enter fact(1)
Enter fact(0)
0!Ret: 1
Ret: 1 * fact(n-1) = 1 * fact(0) = 1 * 1 = 1
Ret: 2 * fact(n-1) = 2 * fact(1) = 2 * 1 = 2
Ret: 3 * fact(n-1) = 3 * fact(2) = 3 * 2 = 6
Ret: 4 * fact(n-1) = 4 * fact(3) = 4 * 6 = 24
Ret: 5 * fact(n-1) = 5 * fact(4) = 5 * 24 = 120
Ret: 6 * fact(n-1) = 6 * fact(5) = 6 * 120 = 720
fact(6) = 720
Pretty cool, right? After more experimenting, I still can't achieve this though...
function factorial (num) {
if (num === 1) {
console.log(num); //print new line after this
return 1;
} else {
var val = factorial(num - 1);
console.log(num +'*' + val); //print new line after this
return num * val;
}
}
I think it's best explained by using your example (edited a little) with comments. Assume you call this function with the parameter set to 5 the first time.
// num = 5, the first time it's called
function factorial (num) {
console.log('factorial(' + num + ')');
if (num === 1) {
// If num === 1, the function will just return 1 and exit.
return 1;
} else {
// Otherwise, which happens almost every time (since 1 is only
// reached once and then it stops). For 5, this would return
// 5 * factorial(4), which in order returns 4 * factorial(3),
// and so on.
return num * factorial(num - 1);
}
}
This output might help you understand:
factorial(5) == (5) * (factorial(4)) // (5) * (4 * 3 * 2 * 1)
factorial(4) == (4) * (factorial(3)) // (4) * (3 * 2 * 1)
factorial(3) == (3) * (factorial(2)) // (3) * (2 * 1)
factorial(2) == (2) * (factorial(1)) // (2) * (1)
factorial(1) == (1) // (1)
function factorial (num) {
for (var i=1; i<6-num; i++)
console.log(' ');
}
console.log('Enter fact('+num+')'); //add code to move new line
if(num==0) {
for (var i=1; i<6-num; i++)
console.log(' ');
}
console.log('0!Ret: 1 '); // add code to move new line
return 1;
} else {
int val = factorial(num-1);
for (var i=1; i<6-num; i++)
console.log(' ');
}
console.log('Ret:'+num+ ' * fact(n-1) = ' + num+ ' * fact('+(num-1)+') = '+num+' * ' + val + ' = ' + (num*val) ); // add code to move new line
return num*val;
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Addition is not working in JavaScript
So I've been working on a calculator of sorts for a game I play. I have the formula below to work it out, however, it's stringing the values together instead of actually adding them. So, if I put nothing in, I get "000000000000000000000000" for the value, and if I change totalattack to a 1 I get "10000000000000000000000". Having looked around a bit, I'm really not sure why it's doing that. Or how to fix it.
var invasionattack = totalattack + (600*DSC) + (60*translator) + (35*mindcontrol) + (30*psionic) + (40*mutagen) + (2500*cartridge) + (300*stryll) + (15*mech) + (20*bane) + (30*cbane) + (60*hbane) + (45*obane) + (75*mbane) + (decimator*200);
alert('Invasion Attack From Modules: ' + invasionattack);
If anyone is curious/if it's relevant, the full code can be found here:
http://glcalc.x10.mx/invasioncalc.html then view source.
There's also a bug with the multipliers somewhere, but I'll find that later.
String concatenation is not the same as addition. You need to make sure you're dealing with numbers, not strings.
That said, there are a number of other potential pitfalls with your calculatechance function .
You are declaring variables when you need them and then redeclaring variables later all of which will get you into trouble because of variable hoisting.
You are using "Truthy and Falsy" values, which in itself is not necessarily a bad thing and in fact (IMHO, one of the beautiful parts of JavaScript), but unless you understand how it works you can get into trouble with it.
You seem to be using anonymous code blocks:
{
if (decimator == 0) {
var decimatormult = 1;
};
if (!decimator == 0) for (var decimatormult = 1; decimator > 0; decimator--) {
decimatormult * 1.07
}
}
and I'm not sure how different browsers will interpret that. It would be better to eliminate them and use comments to delineate sections.
You're overwriting values for totalattack when you probably don't want to.
All said, your function may be better written as:
var calculatechance = function calculatechance() {
'use strict';
var bane = parseFloat(document.getElementById('bane').value, 10) || 0, // Convert to a number (float) and default to 0 if parsing fails.
cbane = parseFloat(document.getElementById('cbane').value, 10) || 0,
obane = parseFloat(document.getElementById('obane').value, 10) || 0,
hbane = parseFloat(document.getElementById('hbane').value, 10) || 0,
mbane = parseFloat(document.getElementById('mbane').value, 10) || 0,
exotic = parseFloat(document.getElementById('exotic').value, 10) || 0,
decimator = parseFloat(document.getElementById('decimator').value, 10) || 0,
mindcontrol = parseFloat(document.getElementById('mindcontrol').value, 10) || 0,
translator = parseFloat(document.getElementById('translator').value, 10) || 0,
anubix = parseFloat(document.getElementById('anubix').value, 10) || 0,
attack = parseFloat(document.getElementById('attack').value, 10) || 0,
// Calculate Invasion Attack
anubixattack = anubix === 100 ? 1 : 0, // Use of ternary operator
// Check Checkboxes
obelisk = document.getElementById("obelisk").checked ? 1 : 0, // Use of ternary operator with truthy/falsy as .checked equals "checked" (truthy) or "" (falsy)
foci = document.getElementById("foci").checked ? 1 : 0,
amp = document.getElementById("amp").checked ? 1 : 0,
overcharge = document.getElementById("overcharge").checked ? 1 : 0,
crux = document.getElementById("crux").checked ? 1 : 0,
mech = document.getElementById("mech").checked ? 1 : 0,
DSC = document.getElementById("DSC").checked ? 1 : 0,
kulgox = document.getElementById("kulgox").checked ? 1 : 0,
terror = document.getElementById("terror").checked ? 1 : 0,
psionic = document.getElementById("psionic").checked ? 1 : 0,
mutagen = document.getElementById("mutagen").checked ? 1 : 0,
stryll = document.getElementById("stryll").checked ? 1 : 0,
cartridge = document.getElementById("cartridge").checked ? 1 : 0,
// Other variables
exoticatt = 0,
decimatormult = 1,
totalattack = attack,
invasionattack = 0;
// Calculate Exotic Bio Disruptor Multiplier
// no logic currently here
// Calculate Exotic Bio Disruptor Static IAttack
switch (exotic) {
case 0:
exoticatt = 0;
break;
case 1:
exoticatt = 250;
break;
case 2:
exoticatt = 350;
break;
default:
exoticatt = (100 * exotic) + 150;
break;
}
//Calculate Atmospheric Decimator Multiplier
if (decimator !== 0) {
while (decimator > 0) {
decimatormult *= 1.07;
decimator -= 1;
}
}
//Calculate Attack
if (obelisk) {
totalattack += attack * 1.1;
}
if (foci) {
totalattack *= 1.05;
}
if (amp) {
totalattack *= 1.15;
}
if (crux) {
totalattack *= 1.1;
}
if (overcharge) {
totalattack *= 1.08;
}
if (anubixattack) {
totalattack += attack * 1.03;
}
//Calculate Invasion Attack
invasionattack = (
totalattack
+ (600 * DSC)
+ (60 * translator)
+ (35 * mindcontrol)
+ (30 * psionic)
+ (40 * mutagen)
+ (2500 * cartridge)
+ (300 * stryll)
+ (15 * mech)
+ (20 * bane)
+ (30 * cbane)
+ (60 * hbane)
+ (45 * obane)
+ (75 * mbane)
+ (decimator * 200)
+ exoticatt
);
alert('Invasion Attack From Modules: ' + invasionattack.toString());
invasionattack = invasionattack * decimatormult;
if (kulgox) {
invasionattack *= 1.1;
}
if (terror) {
invasionattack *= 1.08;
}
alert('Invasion Attack: ' + invasionattack);
};