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
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 implementing the "Maximum Students Taking Exam" algorithm.
Given an m * n matrix seats that represent seat distributions in a classroom. If a seat is broken, it is denoted by '#' character otherwise it is denoted by a '.' character.
Students can see the answers of those sitting next to the left, right, upper left, and upper right, but he cannot see the answers of the student sitting directly in front or behind him. Return the maximum number of students that can take the exam together without any cheating being possible.
Students must be placed in seats in good condition.
My solution: I loop the matrix if I find a good seat ".", I check left, right, upper left, and upper right to see if there is another student (avoid cheating). If not, I increment my answer and set this seat as occupied.
var maxStudents = function(seats) {
let ans = 0;
if (seats === null || seats.length === 0) return ans;
for (let i = 0; i < seats.length; i++) {
for (let j = 0; j < seats[i].length; j++) {
if (seats[i][j] === ".") {
if (seatStudent(seats, i, j)) {
seats[i][j] = "S";
ans++;
}
}
}
}
return ans;
};
const seatStudent = function(seats, i, j) {
if (j + 1 < seats[i].length && seats[i][j + 1] === "S") return false;
if (j - 1 > 0 && seats[i][j - 1] === "S") return false;
if (i - 1 > 0 && j + 1 < seats[i].length && seats[i - 1][j + 1] === "S")
return false;
if (i - 1 > 0 && j - 1 > 0 && seats[i - 1][j - 1] === "S") return false;
return true;
};
For the input:
seats = [["#",".","#","#",".","#"],
[".","#","#","#","#","."],
["#",".","#","#",".","#"]]
The answer should be 4. However, I'm getting 5. I can't understand why it is returning 5.
Thanks
if(i-1 > 0)
should be
if(i-1 >= 0)
done.
This is the one of many code challenges from one company that they gave me to solve.
Write a program that outputs sequentially the integers from 1 to 99, but on some conditions:
- when the integer is a multiple of 3 print “Open” instead of the number
- when it is a multiple of 7 print “Source” instead of the number
- when it is a multiple of both 3 and 7 print “OpenSource” instead of the number
I'v wrote this chunk of code:
let array = []
for (let i = 1; i < 100; i++) {
array.push(i);
if (i % 3 === 0) {
array[i] = "open";
}
if (i % 7 === 0) {
array[i] = "source";
}
if (i % 3 === 0 && i % 7 === 0) {
array[i] = "opensource";
}
}
console.log(array);
As you can see from the output, something is messy at index 2-3. All other integers are replaced correctly.
What is wrong with the code?
Array's first index is 0 not 1. So either insert in arr[i - 1] or use the following approach to fix your issue.
Reasoning of my solution - The loop will start from 1 in my case as we want to check from 0 to 100. And after that, I check the logic if the current value(value) is multiple of 3 or 7 or both. And after that, just push the value into the array and it will automatically add the values in respective indexes.
In your scenario - we need to do arr[index - 1] to compensate the difference between current value and index.
let array = []
for (let i = 1; i < 100; i++) {
let value = i;
if (i % 3 === 0) {
value = "open";
}
if (i % 7 === 0) {
value = "source";
}
if (i % 3 === 0 && i % 7 === 0) {
value = "opensource";
}
array.push(value);
}
console.log(array);
If you debug the code, you'll notice, that the first three values are undefined:
let array = []
for (let i = 1; i < 100; i++) {
array.push(i);
if (i < 10) {
console.log(i, i % 3, i % 7, array[i]);
}
if (i % 3 === 0) {
array[i] = "open";
}
if (i % 7 === 0) {
array[i] = "source";
}
if (i % 3 === 0 && i % 7 === 0) {
array[i] = "opensource";
}
}
console.log(array);
So you'll need to fix your indexing logic.
My question is: why the first code doesn't work very well but the second works perfectly?
First code:
function oddOrEven(N) {
if (N % 2 == 1) {
console.log('Weird');
} else if (2 <= N <= 5) {
console.log('Not Weird');
} else if (6 <= N <= 20) {
console.log('Weird');
} else if (N > 20) {
console.log('Not Weird');
}
}
Second code:
function oddOrEven(N) {
if (N % 2 == 1) {
console.log('Weird');
} else if (N >= 2 && N <= 5) {
console.log('Not Weird');
} else if (N >= 6 && N <= 20) {
console.log('Weird');
} else if (N > 20) {
console.log('Not Weird');
}
}
Taking 2 <= N <= 5 as an example, both of the following return true:
2 <= 3 <= 5
2 <= 6 <= 5
Empirically, it appears that the first inequality on the left is evaluated first, which is true in both cases. Then the following happens:
true <= 5
true <= 5
which really evaluates as:
1 <= 5
1 <= 5
both of which are true. Hence, you get a false flag true in the second example, even though you intend it to be logically false.
In practice, your second code snippet is how you would write such an inequality in JavaScript, and most other languages.
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 :)