-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
23 lines (20 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const userInput=prompt("Please enter the Froyo flavors seperated by a comma","vanilla,vanilla,vanilla,strawberry,coffee,coffee"); //prompt the user for input
if(userInput.length>0){ //make sure that the user entered something
const flavorArray=userInput.split(","); //convert the user input into an array
function flavorFunction(anArray){
let flavorObj={}; //an object to track the repetition of flavor
for( i in anArray){
if(anArray[i] in flavorObj){
flavorObj[anArray[i]]+=1; //increase the count of the flavor if it already exist
}
else{
flavorObj[anArray[i]]=1; //add the flavor in the object if not previously added
}
}
return flavorObj; //return an object
}
console.table(flavorFunction(flavorArray)); //print the content of the object in a table format
}
else{
console.log("You did not enter any flavor!"); //print this if the user input in not valid
}