algorithm sum and group by Array data [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need some help., i am using vb.net and javascript.
I have dataset like this,
Row Price ProductID Region
1 100 12345 JK1
2 100 12345 JK2
3 100 23456 JK1
4 100 23456 JK2
i need the result JK1 = 200 and JK2 = 200. Result is SUM all price with grup by region. May be in SQL this is so simple, but in programming i haven't try.
I need the algoritm in vb.net or javascript. The problem is, the region have a lot of variation that i dont know how much that is.
can some one help?

I like to use Dictionaries for this purpose. I don't really have much experience with datasets but it doesn't really matter for this example. I will just define input as a List(Of RegionPrice) with
Structure RegionPrice
Dim Region as String
Dim Price as Integer
End Structure
[...]
Dim Data as List(Of RegionPrice) = ....
Just loop through your data and get the Region and Price of the current item. Then
Dim dictSum as New Dictionary(Of String, Integer)
For i = 0 to Data.Count - 1
If dictSum.ContainsKey(Data(i).Region) Then
dictSum(Data(i).Region) += Data(i).Price
Else
dictSum.Add(Data(i).Region,Data(i).Price)
End If
Next
You will then have the sums you want for each region in the Dictionary.
Maybe that helps for these kinds of problems.

DataTable.Compute Method can help you:
Dim sumPrice As Object = dataTable.Compute("Sum(Price)", "Region ='" + "JK1" + "'")

Related

Please, how to convert decimal to binary to hex to octal without using toString() or any other pre-made method? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
let num = 100;
document.write("Num: " + num + "");
document.write("Binary: " + num.toString(2) + "");
document.write("8: " + num.toString(8) + "");
document.write("16: " + num.toString(16));
//I want this, but without toString() or any other method.
welcome to Stack Overflow! This sounds like a homework question, because you have been asked to implement something that would normally be done using builtin methods. Here is the outline of how I would approach this, to help you get started.
If you had to write the number 100 in base 16, what would the last digit of the answer be?
How did you come to that conclusion?
Now what would the decimal value of the remainder of the hexadecimal number have to be? Hint: answer = 96.
How did you come to that conclusion?
Your last answer will be a multiple of 16, because you have removed already the remainder after dividing by 16. You have done: number - (number modulo 16).
If you divide this by 16, you have a new, smaller number, which again you can convert into hexadecimal in the same way as above. In this manner you can keep extracting one hexadecimal digit at a time, until there is no reminder.
This gives your hexadecimal number, outputted in reverse order.
You can do the same for any base.

Javascript eval() function not working properly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm currently making a calculator with HTML, CSS and Javascript, for practice. I found out that the "built in" eval function did the math from a string. But it doesn't work properly.
I don't know what the problem is. But when i for example do: 11+11/2 which should be 11. Becomes 16.5 for some reason. I have no idea why. I would really appreciate some help.
Here is the code:
function revealAnswer(){
var math = document.getElementById("numbersInputted");
math.innerHTML += " = " + eval(math.innerHTML);
}
There are a whole bunch of reasons why this is the wrong approach.
First, innerHTML returns a string containing, not only the text content of an element, but also any nested HTML elements as well. If it's just the text you want, use textContent.
Next, by having the user input the actual math operator they want to use in the same string with the numbers creates more confusion. Have the user enter that separately and then you can use if/then logic to ultimately use the correct operator.
Next (and this is the important part), don't ever use eval(). It is not required to solve just about any problem you could encounter, but it opens up the door to "Cross Site Scripting" attacks on your website. Additionally, it manipulates the this binding and executes its code in its own scope.
What you really need to do is simply convert the string input into a number so that you can do math with it. You can do this with parseInt() and parseFloat()
So, your line could be:
math.innerHTML += " = " + parseFloat(math.textContent);
Lastly, for math, the order of operations is:
Parenthesis
Exponents
Multiplication
Division
Addtion
Subtraction
You can see that division is done prior to addition and that means that in your expression: 11 + 11/2, first 11/2 is evaluated (5.5) and then it is added to 11 (16.5).
Finally, remember that the + operator in JavaScript can mean mathematical addition or string concatenation and if one of the operands is a string, the other will be converted to a string.

detect country code by a given phone number in javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an object with all available country codes. and I want to know how I can get the country code by a given phone number and display the corresponding country name. the phone number will look like 16041234567(Canada/US +1) or maybe 8601012345678(China +86) or any other country phone number without plus(+) in the front. I just want to get the country code then I know how to display the name. looks like the code can be 1 up to 4 digits.
FIRST ANSWER:
Just a quick thought, why not count from the other side.
take the 10 numbers off the back side of the number. What you are left with will be the truncated country code.
from your example:
num = "16041234567";
code = num.slice(0, num.length-10);
country_name = country_code_object[code];
This code assumes that your object can deal with variable length codes (but you could always buffer the front of the code if you needed to.
BROKEN: China does not use 10 number (cells use 11)
FIX:
After looking into country codes more completely I relised they are a prefix tree. This means that for a liner time you can just check character by character
num = "16041234567";
country_code;
i = 1;
while (!country_code || i < num.length) {
country_code = country_code_obj[num.slice(0, i)];
i++;
}
The nature of country codes will guarantee that the first code that works will be the correct one. (and we can imagine that this must be true, the phone company doesn't know when you are done typing numbers. They just know when you've reached the end of a valid country code)

How can you replace all occurrences of this token in a String? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a String that looks like this:
"My favorite color is {color:tan}."
I'm trying to figure out how to write a javascript function that replaces all occurrences of the token with whatever color I pass in, but defaults to "tan" if the String is too long.
E.g.
var string = "My favorite color is {color:tan}.";
var favoriteColor = "red";
var maxLength = 25;
var tokenPrefix = "color"
var result = replaceToken(string, favoriteColor, maxLength, tokenPrefix);
Now "result" should equal "My favorite color is red.", because the string is 25 characters long.
But if I set favoriteColor = "purple", then result should equal "My favorite color is tan.", because the word "purple" would cause the string to be longer than 25 characters.
I'm not familiar with regex, so I'm not sure if this is possible in regex, but I've tried to do this using an indexOf() to find the token, but there may be multiple instances. I also tried a replaceAll(), but because it's not a simple string, it won't really work.
There are definitely ways to optimize this, but as a quick one off, here is an example of how to do it:
replaceToken(string, favoriteColor, maxLength) {
tokens = string.match(/{(.*?)}/)
color = tokens[1].split(':')[1]
if (favoriteColor.length < maxLength) string = string.replace(tokens[0], favoriteColor)
else string = string.replace(tokens[0], color)
return string
}

convert one string to array with javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a string like this:
data="{'year':'1990/01/01','income':1990/02/01,'expenses':1000668},{'year':'1990/03/01','income':1000778,'expenses':1000778}"
I want to set it on one array like this:
var chart_data = [data];
how can I do this?
Assuming that first income value is meant to be either quoted or a numeric value, try this out
var chart_data = JSON.parse('[' + data.replace(/'/g, '"') + ']')
I had to convert all the single-quotes to double in order to make the string valid for JSON parsing.
Your data is almost JSON so you can manipulate it to match that format and then use JSON.parse() to convert it to a Javascript object.
The first issue is you need to convert ' to " because JSON uses " to wrap strings. You can use String.replace() to do this.
You also have several objects in the one string, but JSON can only give you one result. You can wrap the string in [] to get JSON.parse() to give you an array containing your objects.
The last thing I saw was the income for the first item had some loose /s which are not allowed in JSON. You'll need to wrap the income in " or remove the /s.
Here's an example:
var data = "{'year':'1990/01/01','income':19900201,'expenses':1000668},{'year':'1990/03/01','income':1000778,'expenses':1000778}";
data = '[' + data.replace(/'/g, '"') + ']';
var chart_data = JSON.parse(data);

Categories

Resources