LMU ☀️ CMSI 185
COMPUTER PROGRAMMING
HOMEWORK #3 PARTIAL ANSWERS
  1. Evaluating given expressions:
    1.  false
    2.  'l'
    3.  'cat'
    4.  10
    5.  {lat: 33, lon: -112}
    6.  0.30000000000000004
    7.  false
    8.  1.111111111111111e+29
    9.  111111111111111111111111111111n
    10.  ["I", "do", "not", "think"]
    11.  {dog: "rex", c: ["Black", "Purple", "Red", "Green", "Blue", "Cyan"]}
    12.  '["home",{"lat":33,"lon":-112}]'
    13.  "Not today"
    14.  100
    15.  -2
  2. Fully parenthesizing:
    1.  ((2 * 5) - (7 / (- 6))) + 4
    2.  (2 > 4) || (true && false)
    3.  (1 <= 2) <= 3
    4.  ((! x) || ((! y) && z)) || ((6 / 5) & 1)
    5.  (- (- 9)) - (--x)
  3. Turning English descriptions into JavaScript expressions:
    1.  s.substring(s.length-5) or
      s.slice(-5)
    2.  x.toString(2)
    3.  a.splice(8, 7)
    4.  a.splice(0, 0, 0, 0, 0) or
      a.unshift(0, 0, 0)
    5.  sick || weekend || holiday
    6.  Math.max(...a)
    7.  Math.floor(4 * x)
    8.  Math.floor(Math.random() * 16 + 5)
    9.  'na'.repeat(16)
    10.  s.includes('💩') || s.includes('😉')
    11.  Math.hypot(8, 3, 99, 14, -3)
    12.  t[4][2].x
    13. new Date(0).toLocaleString('ar-EG') or
      new Date(Date.UTC(1970, 0, 1)).toLocaleString('ar-EG')
    14. {
        A:2, B:2, C:2, D:3, E:3, F:3, G:4, H:4, I:4,
        J:5, K:5, L:5, M:6, N:6, O:6, P:7, Q:7, R:7, S:7,
        T:8, U:8, V:8, W:9, X:9, Y:9, Z:9
      }
    15.  Number.isInteger(x) && x > 0 && x < 1E6 * Math.PI
  4. The statement
    const notice = "She is" + present ? "" : "n't" + " here."
    
    assigns the value "" to notice no matter what value is stored in the variable present. The reason has to do with precedence. The operator ?: has lower precedence than +, therefore the above statement is identical to:
    const notice = ("She is" + present) ? ("") : ("n't" + " here.")
    
    Regardless of the value of present, the leftmost operand of the conditional expression will be a non-empty string (for example "She istrue" or "She isfalse"), which is truthy. Therefore the value of the expression will be the middle argument of the conditional, which is (always) the empty string.

    The preferred way to write this code is:

    const notice = `She is ${present ? "" : "n't"} here.`
    

    Here there are no precedence issues, since we never see two operators at the same syntactic level.

  5. Filling in the pieces of the width-of-the-universe script:
    const WIDTH_IN_LIGHT_YEARS = 93e9
    const SPEED_OF_LIGHT = 299792458
    const DAYS_PER_YEAR = 365.2422
    const SECONDS_PER_DAY = 60 * 60 * 24
    const METERS_PER_YOTTAMETER = 1e24
    const WIDTH_IN_YOTTAMETERS = WIDTH_IN_LIGHT_YEARS * DAYS_PER_YEAR *
      SECONDS_PER_DAY * SPEED_OF_LIGHT / METERS_PER_YOTTAMETER
    console.log(`The universe is ${WIDTH_IN_YOTTAMETERS} Ym wide.`)
    
  6. A picture of an object:

    readysetgo.png

  7. Variable Declarations:
    const employee = {
      name: 'María',
      salary: { period: 'week', amount: 1988, currency: 'USD' },
      hired: new Date(2008, 0, 5),
      supervisor: null
    }
    
    const dayNames = [
      'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo'
    ]
    
    const song = {
      title: 'Johnny Tarr',
      artist: 'Gaelic Storm',
      trackNumber: 3,
      album: 'Tree',                        // IRL this would be a separate object
      releaseDate: new Date(2001, 5, 19),
      producer: 'Jim Cregan',
      writers: ['Steve Wehmeyer', 'Steve Twigger', 'Jim Cregan', 'Shep Lonsdale'],
      genre: 'Celtic',
      lengthInSeconds: 210,
      ASIN: 'B003LLOIR6',
      label: 'Higher Octave',
      openingLyrics: 'Let me tell you a little story about a man named Johnny Tarr',
      synopsis: 'Hard drinker dies of thirst'
    }
    
  8. A more complex object:
    const artist = {
      name: 'Purity Ring',
      origin: {
        city: 'Edmonton',
        province: 'Alberta',
        country: 'CA'
      },
      members: [
        {name: 'Megan James', role: 'vocalist'},
        {name: 'Clayton Knight', role: 'producer'}
      ],
      genres: ['synth-pop', 'trip hop', 'dream pop', 'witch house'],
      website: 'purityringthing.com',
      studioAlbums: [
        {
          title: 'Shrines',
          release: '2012-07-24',
          peaks: [{'ranking': 32, chart: 'Billboard 200'}]
        },
        {title: 'Another Eternity', release: '2015-03-03'},
        {title: 'Womb', release: '2020-04-03'},
      ],
      labels: ['4AD', 'Last Gang']
    }