Core Java G Select Language Question No. 1 of 1 | 35 Marks Numper Search The function NumberSearch(str) takes the string as parameter, search for all the numbers in the string, add them together, then return that final number divided by the total amount of letters in the string. For example: if string is Hello6 9World 2, Nic8e D7ay! the output should be 2. Explanation : First if you add up all the numbers, 6 + 9 + 2 + 8 + 7 you get 32. Then there are 17 letters in the string 32/17 = 1.882, and the final answer should be rounded to the nearest whole number, so the answ Only single digit numbers separated by spaces will be used throughout the whole string (So this wo ever be the case: hello2222 world). Each string will also have at least one letter. Sample Input 1: Hello9-9 Sample Output 1: 4 Sample Input 2: One Number*1* Sample Output 2: 0 PES MAG docx A
Answers
Answer:
उय्य्य्फ्य्फ्टफ् hystgigyxij
jvjyctxtxyvigyxrjbjvvhftdtc nvjvyfy jvjvo
Explanation:
ivugydtcuhjctxgvivyxtxygihiftztvihojohjvvuftdrdygihojojokoi
Using the JavaScript language, have the function NumberSearch(str) take the str parameter, search for all the numbers in the string, add them together, then return that final number divided by the total amount of letters in the string. For example: if str is “Hello6 9World 2, Nic8e D7ay!” the output should be 2. First if you add up all the numbers, 6 + 9 + 2 + 8 + 7 you get 32. Then there are 17 letters in the string. 32 / 17 = 1.882, and the final answer should be rounded to the nearest whole number, so the answer is 2. Only single-digit numbers separated by spaces will be used throughout the whole string (So this won’t ever be the case: hello2222 world). Each string will also have at least one letter.
Sample Input 1:
Input = “H3ello9-9”
Expected Output = 4
Sample input 2:
Input = “One Number1”
Expected Output = 0
To Find:
A code on Java based on the above mention criteria.
Solution:
Here is a code on Java that will take up strings like “H3ello9-9” and add up all the numbers in it i.e, (3+9+9) = 21 and will divide this number by the length of the string which is 6, and at last divide 21 by 6 to give an expected outcome of 4 after rounding off.
[code language=”javascript” collapse=”true”]
function numberSearch(str) {
var separatedString = str.split(”);
var letters = [];
var numbers = [];
var sum = 0;
for (var i=0; i<str.length; i++) {
var currentItem = separatedString[i];
if (isNaN(parseInt(currentItem))) {
letters.push(currentItem);
} else {
numbers.push(currentItem);
};
};
for (var i=0; i<numbers.length; i++) {
currentNumber = Number(numbers[i]);
sum = sum + currentNumber;
};
return (Math.round(sum/letters.length));
};
[/code]
This is the required code in java which will give the expected outcomes in each case for the sample.
#SPJ2