[CoffeeScript] Level 4 Arrays, Objects, Iterations -- Ex

Coffee on the Range

Create an array with numbers 1 until 10 using the inclusive (two dot) range syntax.

[1..10]

 

Coffee on the Range II

Create an array with numbers 1 through 10 using the exclusive range syntax.

复制代码
Exclusive ranges are defined using three dots between integers, inside square brackets
[1...6]

Exclusive ranges do not include the last number.
values = [1...5]
# this expands to [1, 2, 3, 4]

The answer is
[1...11]
复制代码

 

Object Literals

Create a variable named coffee which is an object with a name property set to 'Russian', alevel property set to 2 and an isRussian property which holds a function that returns true. Use an object literal.

coffee = 
  name: 'Russian'
  level: 2
  isRussian: ->
    true

 

or In Loop

Using the for element in collection loop, iterate over the people collection and print the names of people over 18 years old (person.age > 18). Use the console.log function to print the person.name.

for person in people
  console.log(person.name) if person.age > 18

 

List Comprehension

Modify the loop below to use a list comprehension.

console.log "#{person.name}" for person in people when person.age > 18

 

List Comprehension II

Refactor the code below to make use of list comprehension.

addCoffee coffee for coffee in coffeeList when not coffee.isRussian?()

 

Splat Arguments

Change the displayTopPicks function to accept a variable number of suggested coffees by using the splat operator. Use suggested.join(", ") to alert all of the suggested coffees.

displayTopPicks = (bestCoffee, suggested...) ->
  alert('Top #1 ' + bestCoffee)
  alert('Suggested: ' + suggested.join(","))
  

 

posted @   Zhentiw  阅读(311)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示