Question

In: Computer Science

Write a function that counts the colors in a string using JavaScript. String "The quick brown...

Write a function that counts the colors in a string using JavaScript. String "The quick brown fox jumped the blue fence and landed on red paint." This should return the number of colors. The colors you are looking for are "blue, green, brown, gray, black, brown, red, purple".

Solutions

Expert Solution

Source Code:

function count_colors(str)

{

var words=str.split(" ");

var colors=["blue","green","brown","gray","black","brown","red","purple"];

var count=0;

for(var i=0;i<words.length;i++)

{

for(j=0;j<colors.length;j++)

{

if(words[i]==colors[j])

{

count=count+1;

break;

}

}

}

return count

}

var str="The quick brown fox jumped the blue fence and landed on red paint.";

res=count_colors(str);

console.log(res);


Related Solutions

write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam Use of any built in string function...
Write a JavaScript function to calculate the surface area of a sphere. Input the radius using...
Write a JavaScript function to calculate the surface area of a sphere. Input the radius using a NumericUpDown. Have the function calculate the surface area and display it on your page. Label the inputs and the output on your page. Change the title to "Oh, Boy! JavaScript!". Put your name at the top in camelCase. Put your major at the bottom in camelCase.
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write a program that counts the letters in a given string and then display the letter...
Write a program that counts the letters in a given string and then display the letter count in order from high to low. The program should: Display a message stating its goal Prompt the user to enter a string input Count the letters in the string (hint: use dictionaries) Display the letter count in order from high to low (sort your letter count) For example, for the input Google the output should be G - 2 O - 2 E...
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Q3)   Write a C program that counts the number of odd numbers with using function count()...
Q3)   Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Exercise Define a function that generates a random code. The functions takes in a string colors...
Exercise Define a function that generates a random code. The functions takes in a string colors whose characters represent distinct colors to choose from, and a positive integer code_length representing the length of the code. For instance, get_code('ROYGBP',4) returns a code of 4 code pegs randomly with colors chosen from 'R'ed, 'O'range, 'Y'ellow, 'G'reen, 'B'lue, and 'P'urple. One possible outcome is 'ROYY'. Following is my python code, and I don't know how to continue, can someone help me to finished...
Java Write a method that counts how many times one string appears in another string. The...
Java Write a method that counts how many times one string appears in another string. The method takes three input parameters: two Strings and one Boolean. The Boolean value determines whether the words are allowed to overlap each other. For example: When the method is called with input parameters (“balloon”, “oo”, false), it returns 1. When the method is called with input parameters (“hh”, “hhhhhh”, true) returns 5. When the method is called with input parameters (“cc”, “abcdefg”, true), returns...
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT