In: Computer Science
Write Javascript code for the function malwareFrequencies() that shows the malware analyst a prompt. The analyst enters a list of malware names (separated by spaces) for each malware incident they have heard about in the last month. Note that the same malware can be involved in multiple incidents. Your function should print those malware names and their frequencies to the screen. Sample output is shown below.
Zeus 1
Emotet 3
WannaCry 2
Emotet 3
Emotet 3
WannaCry 2
Since there were no particular instructions, i wrote the simplest code.
you want to modify the method for prompt according to your JS environment.
I've split the code into two functions so that it gets easier to understand
please drop a comment if there's an issue.
//-------------INPUT/OUTPUT-----------------------
//---------------------------------------------------------
//this function counts the occurence of "malware" in "list"
function count(list, malware) {
//intitialization
let freq = 0;
//loops over the entire list
for (let i = 0; i < list.length; i++) {
//checks if the curret elemnt is same as "malware"
if (list[i] == malware) {
//incremnts
freq++;
}
}
return freq;
}
//this is the required function
function malwareFrequencies() {
//PROMPT
let input = prompt("Enter the malwate list: ");
//splits the entered string into a list
let list = input.split(" ")
//loops over the list
for (let i = 0; i < list.length; i++) {
//prints the current elment along with their frequency
console.log(list[i], count(list, list[i]))
}
}
malwareFrequencies();