A
A
Alexander Osadchy2022-04-21 15:36:53
JavaScript
Alexander Osadchy, 2022-04-21 15:36:53

How to return a value as a number?

Hello!

I have two constants that return true / false at different screen resolutions:

isMobile - returns true if the window width is less than 992px
isTablet - returns true if the window width is less than 1600px

Linter in the project prohibits the use of triple ternary conditions, etc., then yes it doesn't work

{isMobile ? 'Меньше 992' : isTablet ? 'Меньше 1600, но больше 992' : 'Больше 1600'}


That is, I need a constant that returns a value based on a condition - something like this (only I messed up here, and it doesn’t work (

const blockWidth = () => {
    if (isMobile) {
      return 100;
      console.log('Меньше 992');
    }
    if (isTablet) {
      return 150;
      console.log('Меньше 1600, но больше 992');
    }
    return 200;
    console.log('Больше 1600');
  };


Please help me collect the correct variable(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Adamos, 2022-04-21
@Adamos

return 200 - 50 * (isMobile + isTablet);

M
MrColdCoffee, 2022-04-21
@MrColdCoffee

Try like this

let isMobile = true;
let isTablet = false;

console.log( ( isMobile ? 'Меньше 992' : (isTablet ? 'Меньше 1600, но больше 992' : 'Больше 1600' ) ) );

const blockWidth = () => {
    if (isMobile) {
      console.log('Меньше 992');
      return 100;
      
    }
    if (isTablet) {
      console.log('Меньше 1600, но больше 992');
      return 150;
      
    }
    console.log('Больше 1600');
    return 200;
    
  };
  
 let b = blockWidth();
 console.log(b);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question