-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.js
151 lines (122 loc) · 4.09 KB
/
home.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
///////////////////////////////////////////////
///////////////////HOME.JS/////////////////////
///////////////////////////////////////////////
/*
In this file, you'll be writing code that
helps with some functionality on the home
page of the restaurant's website.
*/
//////////////////PROBLEM 1////////////////////
/*
Write an *arrow* function called `greetUser`
It should have one parameter, `username`
The function should return a string that says:
'Welcome back, [USERNAME]'
Where USERNAME is the `username` arguement sent in
For example, if called with `Andrew` as the
argument, `greetUser` should return the string:
'Welcome back, Andrew'
*/
//CODE HERE
let greetUser = (username) => {
return `Welcome back, ${username}`
}
console.log('---------- Log function greetUser ---------')
console.log(greetUser('Andrew'))
console.log('\n')
//////////////////PROBLEM 2////////////////////
/*
Below is an array of zip codes that are in
the restaurant's delivery zone.
Write a function called `canWeDeliver` that
takes in one argument, `zipCode`.
If the zip code passed in is in the array,
return a string letting the user know they
are eligible for delivery. If they are not,
return a string letting them know that.
For example:
canWeDeliver(84606)
// `Sorry, we can't deliver to that address`
canWeDeliver(85205)
// `You're in our delivery zone!`
*/
const deliveryAreaZipCodes = [85205, 85204, 85203, 85213, 85206]
//CODE HERE
function canWeDeliver(zipCode) {
for (let code of zipCode) {
if (code.startsWith(85)) return `You're in our delivery zone!`
else return `Sorry, we can't deliver to that address`
}
}
console.log('\n')
console.log('---------- Log Convert to String ---------')
console.log(canWeDeliver(84606))
console.log(canWeDeliver(85205))
console.log('\n')
/*
Problem 2 Continued
Now you're going to rewrite your function.
If you wrote `canWeDeliver` using a loop of
some kind, write a new function (`canWeDeliverTwo`)
below, using the `includes` array method.
Look it up on MDN if you're not sure how to use
it.
If you already used the `includes` method,
write a new function using some sort of
loop (for loop, higher order array method).
Name your new function `canWeDeliverTwo`.
*/
// CODE HERE
function canWeDeliverTwo(zipCode) {
for (let code of zipCode) {
if (code.includes(85)) return `You're in our delivery zone!`
else return `Sorry, we can't deliver to that address`
}
}
console.log('---------- Log Array method includes ---------')
console.log(canWeDeliver(84606))
console.log(canWeDeliver(85205))
console.log('\n')
//////////////////PROBLEM 3////////////////////
/*
Below is an array of objects that have some
information about a couple of deals that are
available at the restaurant currently.
You are going to access the object's properties
and change some values. Don't edit the array
directly, let's use the `replace` method.
Read on for more instructions.
*/
const deals = [
{
title: '15% Off!',
desc: 'Applied to your entire order when you spend $30 or more'
},
{
title: 'Free Kids Meal with 2 Regular Entrees',
desc: ' This deal lasts until the end of March! '
}
]
/*
The owner has decided to take the 15% off
deal down to 10%.
Reassign the value of the first deal's title
to be itself, but use the `replace` method
to replace the 15 with a 10.
*/
//CODE HERE
let dealsOfTitle = deals[0].title.replace('15%', '10%')
console.log(dealsOfTitle)
/*
The restaurant is going to continue its
family deal for another month.
Reassign the value of the second deal's desc
to be itself, but use the `replace` method
to replace the word March with April.
You should also make sure that there is no
whitespace in this string, since it seems
to be displaying wrong on the live site.
*/
//CODE HERE
let dealsOfDesc = deals[1].desc.replace('March', 'April')
console.log(dealsOfDesc)