-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays.rb
99 lines (47 loc) · 1.14 KB
/
arrays.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# require 'pry'
# nums = [9, 3, 0, 3, 1, 2, 3, 4, 5]
# nums.max
# # new_num = nums.sort { |a, b| a <=> b }
# # puts new_num
# binding.pry
#array methods
# puts nums.length
# puts nums.first
# puts nums.methods
# puts nums[0...3]
# add elements to an array
# push
# nums.push(6, 7, 8)
# puts nums
# unshift
# nums.unshift(-2, -1, 0)
# puts nums
require 'pry'
names = ["Jack", "Mary", "Nelly", "Jack", "Andy", "Angel"]
# filter using the first word
filter_names = names.filter { |name| name[0] == "J" }
sort_names = names.sort
# new_names = ["Nelly", "Linah"]
# join_names = names << new_names
binding.pry
# new_names = ["Mitch", "John"]
# puts names << new_names
# students = names.each { |name| puts name.length}
# students = names.map { |name| puts name.upcase}
# puts names[0]
# puts names[1]
# nums << 10
# puts nums
# puts nums.concat(names)
# remove elements from array
# pop
# puts names.pop
# shift
# puts names.shift
# puts names
# Advanced methods
#sum
# puts nums.sum
#filter
# puts nums.filter((num) => num.indexOf <= 4)
# [1, 1, 2, 3, 5].filter((num, index, array) => array.indexOf(num) === index);