-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathArgue the toss.js
51 lines (44 loc) · 1.62 KB
/
Argue the toss.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
/*
Description:
Your task is to write a function 'anArgument' that doesn't have a fixed number of parameters. The function should count the arguments passed to it and return a string that specifies how many arguments were passed in, and states what they were.
If the function call is
anArgument("pigs", "giraffes", "unicorns");
The returned string should say
'You gave me 3 arguments and they are "pigs", "giraffes" and "unicorns".'
If there is only one argument passed in, the string should say
'You gave me 1 argument and it is "pigs".'
If there are two arguments, the string should say
'You gave me 2 arguments and they are "pigs" and "giraffes".'
If there are no arguments, the string should say
'You didn't give me any arguments.'
###Punctuation and grammar notes
There are commas after each argument except the last and second-last, which are separated with 'and'
Arguments are presented in quotation marks
The singular: '1 argument' as opposed to multiples: '3 arguments'
Verb agreement: 'and it is' as opposed to 'and they are'
*/
function anArgument() {
let arg=[...arguments]
if (arg.length===0){
return `You didn't give me any arguments.`
}
if (arg.length===1){
return `You gave me 1 argument and it is "${arg[0]}".`
}
if (arg.length===2){
return `You gave me 2 arguments and they are "${arg[0]}" and "${arg[1]}".`
}
let str = '';
for (let i=0;i<arg.length;i++){
if (i<arg.length-2){
str+=`"${arg[i]}", `
}
if (i===arg.length-2){
str+=`"${arg[i]}" `
}
if (i===arg.length-1){
str+=`and "${arg[i]}".`
}
}
return `You gave me ${arg.length} arguments and they are ${str}`
}