How to assign a conditional variable without mutation?

I adhere to the strict principles of functional programming without mutation.

How can I write something like the code below so that it does not change the variable greetingand does not return it to each block if?

const greet = (name, time) => { 
  let greeting = 'Morning';

  if(time >= 12) {
    greeting = 'Afternoon';
  }
  if(time >= 17) {
    greeting = 'Evening';
  }

  return `Good ${greeting} ${name}!`;
};

If it were only two conditions, I would do the following, but it will not work when there are 3 conditions:

const greeting = time > 12 ? 'Afternoon' : 'Morning'
+4
source share
6 answers

Ternary expressions can be composed by other ternary expressions, which allows us to consistently choose logical options

const greeting = time > 12 ? (time > 17 ? 'Evening' : 'Afternoon') : 'Morning'

However, I think this is a variable that makes a variable a variable ...


,

,

  • ( greeting)
  • (.. let, if, return, x = ...)

( ) , - ( ), .

const timeToPeriod = time =>
  time >= 17
    ? 'Evening'
    : time >= 12
      ? 'Afternoon'
      : 'Morning'

const greet = (name, time) =>
  `Good ${timeToPeriod(time)} ${name} !`
  
console.log(greet('Jonas', 9))  // Good Morning Jonas !
console.log(greet('Jonas', 13)) // Good Afternoon Jonas !
console.log(greet('Jonas', 22)) // Good Evening Jonas !
Hide result
+9
const greeting = [
  'Morning', 'Morning', 'Morning', 'Morning', 'Morning', 'Morning', 'Morning', 
  'Morning', 'Morning', 'Morning', 'Morning', 'Morning',
  'Afternoon', 'Afternoon', 'Afternoon', 'Afternoon', 'Afternoon',
  'Evening', 'Evening', 'Evening', 'Evening', 'Evening', 'Evening', 'Evening'
]

return `Good ${greeting[time]} ${name}!`

, , 'Noon' 12- . , ; Noon .

;)

+7

, 3 , :

['Evening', 'Afternoon', 'Morning']
[
[17,12,0].findIndex((el) => time >= el)
]
+2
(function (){
    const greet = (name, time) => { 
        const greetings = [ // Order is important
            {greeting: 'Evening',time: 17},
            {greeting: 'Afternoon',time: 12},            
            {greeting: 'Morning',time: -Infinity}
        ];
        const greeting = greetings.find(e=>time>=e.time).greeting;
        return `Good ${greeting} ${name}!`;
    }
    console.log(greet('Me',17));
})();
+2

, , let const. - javascript. - " const".

, , let . , , ( , , ).

, . , , .

, - , , .

+1

JavaScript . , , . . - , , 24- . (tod). , .. , :

let d = new Date();
let time = d.getHours();

const greeting = {
  "morn": "morning",
  "after": "afternoon",
  "evg": "evening"
};

const greet = function(strName) {
  const tod = (time < 12) ? greeting["morn"] :
    (time > 17) ? greeting["evg"] : greeting["after"];

  let salutation = "Good " + tod;
  salutation += ",";
  strName += "!";

  // adding another functional programming touch
  return function() {
    console.log([salutation, strName].join(" "));
  };
};

var user = "Zander";
greet(user)();
Hide result

, , String . , ; . .

const MDN:

The value of the constant cannot change upon reassignment, and it cannot be updated.

In this and other examples, where the constant is set in accordance with the time of day with respect to the triple expression, the constant itself is a "variable" to the extent that its value changes depending on the time of day when the script value works.

0
source

Source: https://habr.com/ru/post/1682334/


All Articles