Ad

As always, I think it's fun to try to express the high-level logic in lines which are close to natural language. And then have some lower-level functions with the more common style, of course.

Code
Diff
  • function getGrade(...all_marks) {
      this_years_rules='students get grade A when average of marks is at least 90, B when 80 or more, a D if they reach at least 60, and F otherwise';
      return grade_of_student_with(average_of(all_marks)).applying(this_years_rules);
    
      function grade_of_student_with(average_of_marks) {
        return {
          applying:rules=>rules.split(',')
            .map(rule=>/ ([A-Z]) \D*([0-9]*)/.exec(rule).slice(1))
            .filter(([grade,threshold])=>average_of_marks>=(+threshold||0))
            [0][0]
        };
      }
      function average_of(numbers) {
        return numbers.reduce((sum,n)=>sum+n,0)/numbers.length;
      }
    }
    • function getGrade (s1, s2, s3) {
    • let avg = (s1 + s2 + s3) / 3;
    • if (avg >= 90) {
    • return "A"
    • function getGrade(...all_marks) {
    • this_years_rules='students get grade A when average of marks is at least 90, B when 80 or more, a D if they reach at least 60, and F otherwise';
    • return grade_of_student_with(average_of(all_marks)).applying(this_years_rules);
    • function grade_of_student_with(average_of_marks) {
    • return {
    • applying:rules=>rules.split(',')
    • .map(rule=>/ ([A-Z]) \D*([0-9]*)/.exec(rule).slice(1))
    • .filter(([grade,threshold])=>average_of_marks>=(+threshold||0))
    • [0][0]
    • };
    • }
    • else if (avg >= 80) {
    • return "B"
    • function average_of(numbers) {
    • return numbers.reduce((sum,n)=>sum+n,0)/numbers.length;
    • }
    • else if (avg >= 70) {
    • return "C"
    • }
    • else if (avg >= 60) {
    • return "D"
    • }
    • else {return "F"}
    • }

67 bytes

Code
Diff
  • getGrade=(a,b,c,x=(a+b+c)/3)=>"FDCBA"[(x>59)+(x>69)+(x>79)+(x>89)]
    • getGrade=(a,b,c,avg=(a+b+c)/3)=>["F","D","C","B","A"][(avg>=60)+(avg>=70)+(avg>=80)+(avg>=90)]
    • getGrade=(a,b,c,x=(a+b+c)/3)=>"FDCBA"[(x>59)+(x>69)+(x>79)+(x>89)]

I sometimes like to have the top-level logic read (almost) like plain english.

Here's a first shot at that :)

Code
Diff
  • function rps(player_one, player_two) {
      let rules='rock breaks scissors, scissors cut paper, paper covers rock';
      if(both_players_play_same_item()) {
        return "Draw!";
      } else if(player_one_beats_player_two()) {
        return "Player 1 won!";
      } else {
        return "Player 2 won!";
      }
      
      function both_players_play_same_item() {
        return player_one==player_two;
      }
      function player_one_beats_player_two() {
        return rules.match(player_one+' [^,]+ '+player_two);
      }
    }
    • const rps = (p1, p2) => {
    • if (p1 === p2) return "Draw!";
    • var rules = {rock: "scissors", paper: "rock", scissors: "paper"};
    • if (p2 === rules[p1]) {
    • function rps(player_one, player_two) {
    • let rules='rock breaks scissors, scissors cut paper, paper covers rock';
    • if(both_players_play_same_item()) {
    • return "Draw!";
    • } else if(player_one_beats_player_two()) {
    • return "Player 1 won!";
    • }
    • else {
    • } else {
    • return "Player 2 won!";
    • }
    • };
    • function both_players_play_same_item() {
    • return player_one==player_two;
    • }
    • function player_one_beats_player_two() {
    • return rules.match(player_one+' [^,]+ '+player_two);
    • }
    • }

Even shorter :)

Code
Diff
  • const rps=(p,z)=>p==z?'Draw!':`Player ${1+/sr|ps|rp/.test(p[0]+z[0])} won!`;
    • const rps=(p,z)=>p==z?'Draw!':/rockscissors|scissorspaper|paperrock/.test(p+z)?'Player 1 won!':'Player 2 won!';
    • const rps=(p,z)=>p==z?'Draw!':`Player ${1+/sr|ps|rp/.test(p[0]+z[0])} won!`;

Building upon previous idea, trying a more concise but less readable representation of the various game combinations.

Code
Diff
  • function rps(a,b) {
      return a==b
        ? 'Draw!'
        : `Player ${1+['sr','ps','rp'].includes(a[0]+b[0])} won!`;
    }
    
    • function rps(player1, player2) {
    • return player1 == player2 ? 'Draw!' :
    • player1 == 'rock' && player2 == 'scissors' ||
    • player1 == 'scissors' && player2 == 'paper' ||
    • player1 == 'paper' && player2 == 'rock' ? 'Player 1 won!' : 'Player 2 won!';
    • function rps(a,b) {
    • return a==b
    • ? 'Draw!'
    • : `Player ${1+['sr','ps','rp'].includes(a[0]+b[0])} won!`;
    • }
Code
Diff
  • const reverseStr = s => [...s].reverse().join('');
    • const reverseStr = str => str.split.split('').reverse().join('');
    • const reverseStr = s => [...s].reverse().join('');
Code
Diff
  • def print_String():
        return lst[::-1]
        
    • def print_String():
    • return lst.reverse()
    • return lst[::-1]
Code
Diff
  • def print_String():
        # Printing the index of a string in the list.
        lst = ['double', 'switch', 'off', 'on', 'jump']
        print(lst.index('double'))
        
        # Reversing the items in the list
        print('Reversed List:',lst[::-1])
    • def print_String():
    • # Printing the index of a string in the list.
    • lst = ['double', 'switch', 'off', 'on', 'jump']
    • print (lst.index('double'))
    • print(lst.index('double'))
    • # Reversing the items in the list
    • print ('Reversed List:',lst.reverse())
    • print('Reversed List:',lst[::-1])

This solution both avoids the repetition of Number.integer(n/divisor) AND allows for any number of divisors.

Code
Diff
  • const isDivisible = (n,...x) => x.map(d=>Number.isInteger(n/d)).every(e=>e)
    • const isDivisible = (n, x, y) => Number.isInteger(n/x) && Number.isInteger(n/y)
    • const isDivisible = (n,...x) => x.map(d=>Number.isInteger(n/d)).every(e=>e)
Algorithms
Logic

This slight shuffle makes the function more robust when given non-array values. Not sure that's a desirable feature :-/

Code
Diff
  • const flatten = a => 
      Array.isArray(a) ? a.reduce((acc,item) => acc.concat(flatten(item)), []) : a;
    • const flatten = arr =>
    • arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);
    • const flatten = a =>
    • Array.isArray(a) ? a.reduce((acc,item) => acc.concat(flatten(item)), []) : a;