function checkValue(val) {
if (val === 1 || val === 4 || val === 7) {
return val - 2
}
if (val === 2 || val === 5 || val === 8) {
return val - 1
}
if (val === 3 || val === 6 || val === 9) {
return val
}
}
console.log(6 - checkValue(6))
how to make this code simple and dynamic to prevent repetitive code writing
function checkValue(val){
const reminder = val % 3;
return reminder ? val - (3 - reminder) : val;
}
I'm looking to identify a category based on this table:
I have an if statement that seems to work for some conditions, but not others. R, P, and Q are working, but conditions that go between numbers aren't returning the right category.
If statement:
function getCategory(featureFunctionalScore, featureDysfunctionalScore) {
if (featureFunctionalScore == 4 && featureDysfunctionalScore == -2) {
return "Performance";
} else if (featureFunctionalScore == 4 && featureDysfunctionalScore <= -1 && featureDysfunctionalScore > 4) {
return "Attractive"
} else if (featureFunctionalScore <= -1 && featureFunctionalScore > 4 && featureDysfunctionalScore == 4) {
return "Expected"
} else if ((featureFunctionalScore >= -2 && featureFunctionalScore <= 2 && featureDysfunctionalScore == -2) || (featureFunctionalScore == -2 && featureDysfunctionalScore >= -2 && featureDysfunctionalScore <= 2)) {
return "Reverse"
} else if ((featureFunctionalScore == 4 && featureDysfunctionalScore == -2) || (featureFunctionalScore == 2 && featureDysfunctionalScore == -1) || (featureFunctionalScore == -1 && featureDysfunctionalScore == 2) || (featureFunctionalScore == -2 && featureDysfunctionalScore == 4)) {
return "Questionable"
} else {
return "Indifferent"
};
};
Am I missing something important?
Update
This statement works in Excel, but I'm struggling to get it to work in JS:
=IF(OR(AND(C3 <= 2, B3 <= -1), AND(C3 <= -1, B3 <= 2)), "R", IF(AND(C3 <= 2, C3 >= -1, B3 <= 2, B3 >= -1), "I", IF(AND(C3 >= 2,B3 >= -1, B3 <= 2),"A", IF(AND(C3 <= 2, B3 <= 4, B3 >= 2), "M", IF(AND(C3 >= 2, B3 >= 2), "P", "Q")))))
This should be what you're looking for. I'm sure it could be optimized, but it works. JSFiddle: https://jsfiddle.net/yxb7tr9n/
function getCategory(x,y){
var answer = -999;
if (x == 4 && y == 4){
answer = "p";
}else if([-1,0,2].indexOf(x) >= 0 && y == 4){
answer = "A";
}else if((x == -2 && y == 4) || (x == -1 && y == 2) || (x == 4, y == -2)){
answer = "Q";
}else if(x == 4 && [-1,0,2].indexOf(y) >= 0) {
answer = "M";
}else if((x == -1 && [-1,0].indexOf(y) >= 0) || (x == 0 && [-1,0,2].indexOf(y) >= 0) || (x == 2 && [0,2].indexOf(y) >= 0)){
answer = "I";
}else if ((x == -2 && [-2,-1,0,2].indexOf(y) >= 0) || (y == -2 && [-2,-1,0,2].indexOf(x) >= 0)) {
answer = "R";
}else{
answer = "??";
}
return answer;
}
UPDATE: Alternate version using a coordinate mapping system. JSFiddle: https://jsfiddle.net/g2d6p4rL/4/
function indexOfCustom (parentArray, searchElement) {
for ( var i = 0; i < parentArray.length; i++ ) {
if ( parentArray[i][0] == searchElement[0] && parentArray[i][1] == searchElement[1] ) {
return i;
}
}
return -1;
}
function getCategory2(x,y){
var p = [[4,4]];
var q = [[-2,4],[-1,2],[2,-1],[4,-2]];
var a = [[-1,4],[0,4],[2,4]];
var m = [[4,2],[4,0],[4,-1]];
var i = [[0,2],[2,2],[-1,0],[0,0],[2,0],[-1,-1],[0,-1]];
var r = [[-2,2],[-2,0],[-2,-1],[-2,-2],[-1,-2],[0,-2],[2,-2]];
coord = [x,y];
if (indexOfCustom(p,coord) >= 0){
return "p";
} else if (indexOfCustom(q,coord) >= 0){
return "Q";
} else if (indexOfCustom(a,coord) >= 0){
return "A";
} else if (indexOfCustom(m,coord) >= 0){
return "M";
} else if (indexOfCustom(i,coord) >= 0){
return "I";
} else if (indexOfCustom(r,coord) >= 0){
return "R";
}else{
return "??";
}
}
Output of all answers:
[-2,-2] = R
[-2,-1] = R
[-2,0] = R
[-2,2] = R
[-2,4] = Q
[-1,-2] = R
[-1,-1] = I
[-1,0] = I
[-1,2] = Q
[-1,4] = A
[0,-2] = R
[0,-1] = I
[0,0] = I
[0,2] = I
[0,4] = A
[2,-2] = R
[2,-1] = Q
[2,0] = I
[2,2] = I
[2,4] = A
[4,-2] = Q
[4,-1] = M
[4,0] = M
[4,2] = M
[4,4] = p
Is there a way to reduce the number of "if" in this code?
function test(input) {
if ((input % 3 == 0) && (input % 5 == 0)) {
return 'fizzbuzz';
} else if (input % 3 == 0) {
return 'fizz';
} else if (input % 5 == 0) {
return 'buzz';
} else {
return '' + input;
}
}
for (var i = 1; i < 100; i++) {
console.log(test(i));
}
You can avoid that by storing the comparison values:
var mod3 = input % 3 == 0;
var mod5 = input % 5 == 0;
... creating a lookup table ...
var outs = [input, "fizz", "buzz", "fizzbuzz"];
... and indexing it ...
return outs[(+mod3) + 2 * (+mod5)];
... no ifs!
You can use the ternary operator:
return ((input%3==0)&&(input%5==0)) ? 'fizz buzz'
: (input%3==0) ? 'fizz'
: (input%5==0) ? 'buzz'
: '' + input;
If you don't want to use if's why not try case syntax?
http://www.w3schools.com/js/js_switch.asp
Alternatively use the JS shorthand for if
so that:
var something;
if (2 + 2 === 4){
something = "Yup!"
} else {
something = "Nope"
}
becomes
var something = 2 + 2 === 4 ? "Yup!" : "Nope";
You can get rid of the first "if" altogether, because 3 and 5 and "fizz" and "buzz" are used in the later ones. You can also wait to return until the end. Something like:
var str = "";
if (input % 3 === 0){
str +="fizz";
}
if (input % 5 === 0 ){
str +="buzz";
} else {
str = input;
}
return str;
I'm trying to do some simple tests to help further my javascript knowledge (which is quite fresh). Goal 1 is to print numbers from 1-100 that aren't divisible by 5 or 3.
I tried the following:
for (var i = 1; i <= 100; i ++)
{
if (i%3 !== 0 || i%5 !== 0){
console.log(i);
}
}
This logs EVERY number from 1-100, and I can't tell why. Probably the simplest simplest questions here but it's doing my head in!
I think you mean &&, not ||. With ||, you're basically testing to see if the number is not divisible by 3 or by 5 - only if a number is divisible by both do you reject it (in other words, multiples of 15).
The typical answer to FizzBuzz is:
if( i%3 == 0 && i%5 == 0) FizzBuzz
elseif( i % 3 == 0) Fizz
elseif( i % 5 == 0) Buzz
else number
So to get directly to the number you need for i%3==0 to be false AND i%5==0 to be false. Therefore, you want if( i%3 !== 0 && i%5 !== 0)
Here's a quite simple FizzBuzz function that accepts a range of numbers.
function fizzBuzz(from, to) {
for(let i = from; i <= to; i++) {
let msg = ''
if(i % 3 == 0) msg += 'Fizz'
if(i % 5 == 0) msg += 'Buzz'
if(msg.length == 0) msg = i
console.log(msg)
}
}
fizzBuzz(1, 25)
As for a more complex solution, that's one way you could define a higher order function which generates customized FizzBuzz functions (with additional divisors and keywords)
function fizzBuzzFactory(keywords) {
return (from, to) => {
for(let i = from; i <= to; i++) {
let msg = ''
Reflect.ownKeys(keywords).forEach((keyword) => {
let divisor = keywords[keyword]
if(i % divisor == 0) msg += keyword
})
if(msg.length == 0) msg = i
console.log(msg)
}
}
}
// generates a new function
const classicFizzBuzz = fizzBuzzFactory({ Fizz: 3, Buzz: 5 })
// accepts a range of numbers
classicFizzBuzz(1, 25)
const extendedFizzBuzz = fizzBuzzFactory({ Fizz: 3, Buzz: 5, Bazz: 7, Fuzz: 11 })
extendedFizzBuzz(1, 25)
I attacked this the same was as Niet the Dark Absol:
for (var n = 1; n <= 100; n++) {
if (n % 3 == 0 && n % 5 == 0)
console.log("FizzBuzz");
else if (n % 3 == 0)
console.log("Fizz");
else if (n % 5 == 0)
console.log("Buzz");
else
console.log(n);
}
However, you can also do it this way:
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0)
output += "Fizz";
if (n % 5 == 0)
output += "Buzz";
console.log(output || n);
}
One of the hardest parts of learning JavaScript - or any language - for me is understanding solutions can come in many ways. I like the first example more, but it's always good to keep thinking and look at other options.
Hello fellow StackOverflowers. I'm have a brain fart right now, and I cannot seem to figure this out.
I have the following code
if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0))
{
return true;
}
else {
return false;
}
Basically I need to test if the number is a multiple of 3 || 5 but not a multiple of both.
However when I enter any number I enter (whether it is multiple of 3 || 5 || both) the test always fails. I would have thought this was able to be performed in one statement.
This code though does work fine.
if (n % 3 === 0 || n % 5 === 0)
{
if( n % 3 === 0 && n % 5 === 0)
{
return false;
}
else {
return true;
}
}
else {
return false;
}
But I'm wondering what I am missing in the first test. I'd like all the test to be in one like, but like I said I'm having a brain fart and cannot figure out what I'm missing.
You can use the XOR operator, alternatively
return (n % 3 === 0 ^ n % 5 === 0);
If it is divisible by both 3 and 5, it'll be divisible by 15.
Please try the following condition
if ((n % 3 === 0 || n % 5 === 0) && ( n % 15 !== 0))
change
if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0))
to
if ((n % 3 === 0 || n % 5 === 0) && !(n % 3 === 0 && n % 5 === 0))
The first part of your logic is to determine if the number in question is a multiple of 3 or 5 whereas the second SHOULD be about wether only one of them is. So... I changed the second part to see if both match it and then I NOT'ed that.
It should be: if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 || n % 5 !== 0))
return (n % 3 === 0 && !(n % 5 === 0)) || (n % 5 === 0 && !(n % 3 === 0));
(untested)
Your second check is wrong:
if ((n % 3 === 0 || n % 5 === 0) &&**( n % 3 !== 0 && n % 5 !== 0)**)
Change it to:
(! (n%3 === 0 && n % 5 === 0 ) )
This is a short version of XOR implementation using conditional statement in javascript
if((n % 3 === 0)? (n % 5 !== 0) : (n % 5 === 0)) {
...
}
or you can also compare in this way, checking when the two conditions, when evaluated as boolean, return different values (one is true and other is false or vice-versa)
if( (n % 3 === 0) !== (n % 5 === 0)) {
...
}
so this code can be written really short