0

I'm having trouble with this logic. I'm trying to find a way where a single variable will have multiple values without using array. Here is my code

var num = {
 from: 500,
 to: 700
  };
 if (num == 500 && num == 600 && num==700) { 
 console.log("working");
 }
 else { 
 console.log("not working")
 }
5
  • num is an object, thus, you can't compare it to a number, it always be falsy. You should do somthing like; num.from === 500 or num.to === 500 in order to check and assign. Commented Apr 5, 2018 at 9:03
  • Possible duplicate of How to detect the variable when used? Commented Apr 5, 2018 at 9:04
  • mmh, you don't want to use and array for values, but you use an object (associative array in JS, kind of).. It's just semantics though, your question is valid, only description is a bit confusing Commented Apr 5, 2018 at 9:07
  • the challenge is not to remove the code from the else statement. I'm trying to find a way to make it work and exploring the possibilities. Thank you for the idea Commented Apr 5, 2018 at 9:07
  • I did try to use array but it seems like I also have to change the else statement. Commented Apr 5, 2018 at 9:08

3 Answers 3

1

You could take a function for checking the range.

function check({ from, to }, value) {
    return from <= value && value <= to;
}

var num = { from: 100, to: 300 };

if (check(num, 500) && check(num, 600) && check(num, 700)) {
  console.log("working");
} else {
  console.log("not working")
}

console.log(check(num, 200));

Sign up to request clarification or add additional context in comments.

Comments

1

Use a function and try comparing the values.

var num = {
 from: 100,
 to: 300,
 checkValue:function(num){
  return this.from <= num && num <= this.to
 }
};
console.log('1st check');
if (num.checkValue(100) && num.checkValue(600) && num.checkValue(700)) { 
 console.log("working");
}
else { 
 console.log("not working")
}

console.log('2nd check');
if (num.checkValue(120) && num.checkValue(150) && num.checkValue(250)) { 
 console.log("working");
}
else { 
 console.log("not working")
}

5 Comments

@RyanArellano check now:p
oh I'm so sorry, I got my code wrong, the range is suppose to be 500 to 700. thus keeping the else statement intact
@RyanArellano change from and to in num object.
awesome dude, you helped me a lot!!
@RyanArellano Accept and upvote if it works. Happy coding ;)
1

To access your object (Variables with multiple values) you have to do like this :

var num = {
    from: 100,
    to: 300
}

console.log(num.from); //This will print 100
console.log(num.to); //This will print 300

and you can do this

num.from += 1;
console.log(num.from); //This will print 101

So your (Ryan Arellano) code must be like this :

function check(num, value) {
    return num.from <= value && value <= num.to;
}

var num = { from: 100, to: 300 };

if (check(num, 500) && check(num, 600) && check(num, 700)) {
    console.log("working");
} else {
    console.log("not working")
}

console.log(check(num, 200));

3 Comments

I'm not suppose to remove the code from the else statement
I edited my response. Try with this code and it will work :)
You shouldn't include someone's answer without giving credit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.