So I made this code awhile ago, and it worked then, but now it won't. Can someone tell me what's wrong? What the code does is simple: I take a weapon damage value from a Fallout game (averaging the value first if FO1/FO2/FOT), tell it what game it's from, and it outputs how much damage it does in d20 Modern. I don't know if explaining what it does helps, but I hope it's clear.
var systemSelect = prompt("What system are you using? FO, F2, FT, F3, or FNV?")
var damage = parseInt(prompt("How much damage does the weapon do?"))
if (systemSelect === "FO" or "F2" or "FT") {
if (damage >= 1 && < 11) {
damage = "2d4";
} else if (damage >= 11 && < 26) {
damage = "2d6";
} else if (damage >= 26 && < 46) {
damage = "2d8";
} else if (damage >= 46 && < 61) {
damage = "2d10";
} else if (damage >= 61 && < 81) {
damage = "2d12";
} else if (damage >= 81 && < 101) {
damage = "4d6";
} else {
damage = "2d20";
}
}
if (systemSelect === "F3" or "FNV") {
if (damage >= 1 && < 8) {
damage = "2d4";
} else if (damage >= 8 && < 15) {
damage = "2d6";
} else if (damage >= 15 && < 25) {
damage = "2d8";
} else if (damage >= 25 && < 37) {
damage = "2d10";
} else if (damage >= 37 && < 61) {
damage = "2d12";
} else if (damage >= 61 && < 81) {
damage = "4d6";
} else {
damage = "2d20";
}
}
Your problem seems to be bad syntax in your if-conditions.
First of all, you can't say if (damage >= 1 && < 11), you need to specify the variable in each condition, i.e. if (damage >= 1 && damage < 11).
Secondly, the "or" operator is not or, it's || so you need if (systemSelect=="FO" || systemSelect=="F2" || systemSelect=="FT")
Fix those problems and try again - good luck :)
Related
problem statement is here: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards
the problem comes when i tried to get 0 Hold in the return by the Cards Sequence 7, 8, 9 ,but i can't. I know that there are better options for solving this problem, but i wanna do it this way, someone can help?
function cc(card) {
// Only change code below this line
if (card = ( 2 || 3 || 4 || 5 || 6 )) {
count += 1;
}
else if (card = ( 7 || 8 || 9 )) {
count += 0;
}
else if (card = ( 10 || "J" || "Q"|| "K" || "A" )) {
count -= 1;
}
if (count <= 0) {
return count + " Hold";
}
else if (count > 0) {
return count + " Bet";
}
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');```
Keeping the same gist of your function:
function cc(card) {
if ("23456".indexOf(card) >= 0) count++;
if ("10JQKA".indexOf(card) >= 0) count--;
return count + (count <= 0 ? " Hold" : " Bet");
}
I'm new to all this, i have a project for college, can't seem to get this to work and it seems to have a lot of repeating code, any help would be great, thanks.
var strength = document.getElementById('getstren');
strength.innerHTML = Math.floor((Math.random() * 17) + 1);
if (strength == 1) {
document.getElementById('ath').innerHTML = "-5"
}else if (strength == 2 || strength == 3) {
document.getElementById('ath').innerHTML = "-4"
}else if (strength == 4 || strength == 5) {
document.getElementById('ath').innerHTML = "-3"
}else if (strength == 6 || strength == 7) {
document.getElementById('ath').innerHTML = "-2"
}
else if (strength == 8 || strength == 9) {
document.getElementById('ath').innerHTML = "-1"
}
else if (strength == 10 || strength == 11) {
document.getElementById('ath').innerHTML = "0"
}
else if (strength == 12 || strength == 13) {
document.getElementById('ath').innerHTML = "1"
}
else if (strength == 14 || strength == 15) {
document.getElementById('ath').innerHTML = "2"
}
else if (strength == 16 || strength == 17) {
document.getElementById('ath').innerHTML = "3"
}
else if (strength == 18) {
document.getElementById('ath').innerHTML = "4"
}
I think it should display a figure for strength and should display another figure for athletics depending on the first figure!
Firstly, you need to access the innerHTML of strength and use parseInt to make it an integer. Secondly, you can use the following statement to make your code simpler: document.getElementById('ath').innerHTML = Math.floor(strength / 2) - 5.
var strength = document.getElementById('getstren');
strength.innerHTML = Math.floor((Math.random() * 17) + 1);
document.getElementById('ath').innerHTML = Math.floor(parseInt(strength) / 2) - 5;
The variable strength is a HTMLElement. Therefore, it will never be equal to an integer.
To access the html of the HTMLElement, you can use the innerHTML property which returns a string. You can use parseInt to cast it to an integer.
Example:
// Assumes strength is defined
const strengthInt = parseInt(strength.innerHTML)
if(strength == 1){
// Set ath html
}
//Other if-else here
As for an alternative to if-else, look at switch.
I basically just can't figure this one out and don't want to hack it.
It will look so messy if I do this for 52 weeks in a year.
Any tips?
Update
This question is not about getting the year's current week.
This question is about getting weeks elapsed since a date defined.
I want the next week's workout to show up on 6th day of the current week ;)
Example
My days since start are 99: 2018-05-30 18:39:29.
Some of your examples are showing me on week 15.
My code however shows week 16, which is right. See the caveat?
calculateUsersCurrentWorkoutWeek: function(timestamp) {
let daysSinceSignup = moment().diff(timestamp, "days");
if (daysSinceSignup <= 6) {
return 1;
} else if (daysSinceSignup > 6 && daysSinceSignup <= 13) {
return 2;
} else if (daysSinceSignup > 13 && daysSinceSignup <= 20) {
return 3;
} else if (daysSinceSignup > 20 && daysSinceSignup <= 27) {
return 4;
} else if (daysSinceSignup > 27 && daysSinceSignup <= 34) {
return 5;
} else if (daysSinceSignup > 34 && daysSinceSignup <= 41) {
return 6;
} else if (daysSinceSignup > 41 && daysSinceSignup <= 48) {
return 7;
} else if (daysSinceSignup > 48 && daysSinceSignup <= 55) {
return 8;
} else if (daysSinceSignup > 55 && daysSinceSignup <= 62) {
return 9;
} else if (daysSinceSignup > 55 && daysSinceSignup <= 62) {
return 10;
} else if (daysSinceSignup > 62 && daysSinceSignup <= 69) {
return 11;
} else if (daysSinceSignup > 69 && daysSinceSignup <= 76) {
return 12;
} else if (daysSinceSignup > 76 && daysSinceSignup <= 83) {
return 13;
} else if (daysSinceSignup > 83 && daysSinceSignup <= 90) {
return 14;
} else if (daysSinceSignup > 90 && daysSinceSignup <= 97) {
return 15;
} else {
return 16;
}
}
Divide by 7 and ceil?
const daysSinceSignup = moment().diff(timestamp, "days");
return Math.ceil(daysSinceSignup / 7);
Did you miss out on coffee this morning? ;-)
Note: The function above will tell you "the number of the week since timestamp" which is what I understand from your text, but your code sample does not reflect it.
To get the same result of your code sample, you'll need to .floor() and add 1 instead of .ceil():
const daysSinceSignup = moment().diff(timestamp, "days");
return Math.floor(daysSinceSignup / 7) + 1;
As #Michel said, divide by 7 and floor + 1
calculateUsersCurrentWorkoutWeek: function(timestamp) {
let daysSinceSignup = moment().diff(timestamp, "days");
return Math.floor(daysSinceSignup / 7) + 1;
}
What you are looking for is
return Math.ceil((daysSinceStartup + 1) / 7);
This will give the same results as your code for any real number between -1 and 62 (because around 62 you forgot to replace the copied ranges). It returns 1 for 6 days, and 2 for 6.01 days, just as the original code.
The easy way could also be to create a integer array in ascending order so that the values contain the upper limit of the comparison. Then for each values of daysSinceSignup you can find the index and add 1 to it since you require the return values as 1, 2, 3, ... and so on. This will be useful if you do not have uniform interval, like currently the interval is always 7 so the division and ceil approach could also work but if the interval is not uniform then this approach could be really useful.
var indexArray = [6,13,20,27,34,41,48,55,62,69,76,83,90,97];
function calculateUsersCurrentWorkoutWeek() {
let daysSinceSignup = 14;
var elemIndex = indexArray.findIndex((item)=> item > daysSinceSignup);
return elemIndex+1;
}
console.log(calculateUsersCurrentWorkoutWeek());
You can put your datas in array and use array.find:
var datas = [
{ min: -Infinity, max: 6, ret: 1 },
{ min: 6, max: 13, ret: 2 },
{ min: 13, max: 20, ret: 3 },
{ min: 20, max: 27, ret: 4 }
];
function calculateUsersCurrentWorkoutWeek(daysSinceSignup) {
return datas.find(({min, max}) =>
daysSinceSignup > min && daysSinceSignup <= max
).ret;
}
console.log(calculateUsersCurrentWorkoutWeek(5));
console.log(calculateUsersCurrentWorkoutWeek(7));
console.log(calculateUsersCurrentWorkoutWeek(14));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
OK, now my issue is that no matter what I do, it will only present the information as Level 1 instead of the designated level that I try. The code is as follows
function XPlevel(XP, level) {
if((XP >= 0 && XP < 300) && level === 1) {
level = 1;
} else if ((XP >= 300 && XP <900) || level === 2) {
level = 2;
} else if ((XP >= 900 && XP <2700) || level ===3) {
level = 3;
} else if ((XP >= 2700 && XP < 6500) || level === 4){
level = 4;
} else if ((XP >= 6500 & XP < 14000) || level === 5){
level = 5;
} else if ((XP >= 14000 && XP < 23000) || level === 6) {
level = 6;
} else if ((XP >= 23000 && XP < 34000) || level === 7) {
level = 7;
} else if ((XP >= 34000 && XP < 48000) || level === 8) {
level = 8;
} else if ((XP >= 48000 && XP < 64000) || level === 9) {
level = 9;
} else if ((XP >= 64000 && XP < 85000) || level === 10) {
level = 10;
} else if ((XP >= 85000 && XP < 100000) || level === 11) {
level = 11;
} else if ((XP >= 100000 && XP < 120000) || level === 12) {
level = 12;
} else if ((XP >= 120000 && XP < 140000) || level === 13) {
level = 13;
} else if ((XP >= 140000 && XP < 165000) || level === 14) {
level = 14;
} else if ((XP >= 165000 && XP < 195000) || level === 15) {
level = 15;
} else if ((XP >= 195000 && XP < 225000) || level === 16) {
level = 16;
} else if ((XP >= 225000 && XP < 265000) || level === 17) {
level = 17;
} else if ((XP >= 265000 && XP < 305000) || level === 18) {
level = 18;
} else if ((XP >= 305000 && XP < 355000) || level === 19) {
level = 19;
} else {
level = 20;
}
return level;
}
XPlevel(XP, level);
So when I plug in a character that is Level 5 for instance, it gives me back level 1 instead.
Any thoughts?
First of all, never code like this, never have multiple if-else statements that make semantically similar checks that could be written using an array or hash table in a few lines.
Your function could be re-written like this:
var xp_required = [0, 300, 900, 2700, 6500]; // ...etc, you fill this table with the XP required to be at Level = index + 1 (indices start at 0 in Arrays).
// XP for level: 1 2 3 4 5 ...
function getLevel(xp) {
for(var level = xp_required.length - 1; level >= 0; --level) {
if(xp >= xp_required[level] {
return level + 1; // The +1 is needed because Array's index starts at 0 but levels start at 1
}
}
console.log("XP value can not be negative. The given value was: " + xp);
return 0;
}
The for loop starts at the highest level and checks if the XP is enough for the player to be considered that level. If it's not, it means that the player is actually a lower level, thus decrementing the level value to be checked (until we reach index 0 which means Level 1). This means that once we get to the first level for which the XP sufficies it means that is indeed the correct level.
As a note, this could be improved by doing a binary search instead of a linear search, but I assume that this function is not called that often so the O(max_level) complexity is good enough.
Also, why is level both an input and output value for your function?
(Beside the XP stuff...) you're basically doing if level == 1 return 1 which is nonsensical.
If you already know the level than logically you don't need to check for the level.
The simplest & fastest way to get a level out of an array of XP values:
function getLevel(XP) {
var LV = 0;
[0, 300, 900, 2700, 6500, 14000].some(function(v, i) {
LV = i; // Level = index
return v > XP; // We have the LV value! Break out of loop (if condition is met)!
});
return LV;
}
Use like
var level = getLevel(2699); // 3
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some
added a while loop, and trying to end the loop by entering finish inside the loop. The game is still running after it the game is completed.
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
var correct = false;
while (!correct) {
correct = guessFunction();
var finish = false;
}
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!' + 'That was a total of ' + guessCount + ' guesses.');
correctGuess = true;
finish = true;
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
if (diff >= 1 && diff <= 10 && !correctGuess) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20 && !correctGuess){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30 && !correctGuess){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50 && !correctGuess){
alert('Cold');
guessFunction();
}
else if (diff > 50 && !correctGuess){
alert('Ice Cold');
guessFunction();
}
}
guessFunction();
Trying to get this code to run but it only allows for 2 alert windows when guessing the random number. Im not sure how to get this to run, perhaps the guessFunction is not running?
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!');
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
}
guessFunction();
if (diff >= 1 && diff <= 10) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50){
alert('Cold');
guessFunction();
}
else if ( diff > 50){
alert('Ice Cold');
guessFunction();
}
The script stops executing because you only call your function twice. If you want this to run until the user guesses the right number, you probably want a while loop:
var correct = false;
while (!correct) {
// guessFunction could return true if they get it right
correct = guessFunction();
}
Yes, your script is only executing twice before exiting. You've got your nesting wrong:
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!');
correctGuess = true;
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
if (diff >= 1 && diff <= 10 && !correctGuess) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20 && !correctGuess){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30 && !correctGuess){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50 && !correctGuess){
alert('Cold');
guessFunction();
}
else if (diff > 50 && !correctGuess){
alert('Ice Cold');
guessFunction();
}
}
guessFunction();
EDIT:
My answer is more verbose than it needs to be so that it relates directly to your question. The next step for you is to implement the better solution of a while loop mentioned in Menello's answer in your script.