[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

function to get frequencies of a char array

jrough

1/22/2015 10:43:00 PM

the output should be:
a:1,b:1,j:4,c:1,d:1

I am trying to use a map and use the keys as the char and the values as the count but I'm getting undefined.
thanks,


function frequencies(){
var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
var key, value;
for(i=0; i< arr.length; i++){
var myMap = new Map();
if (arr[i]==arr[i+1]){
myMap.set(key=i, value= value+1)
}else{
myMap.set(key=i,"1")
};
};

}
frequencies();
39 Answers

Ben Bacarisse

1/22/2015 11:10:00 PM

0

JRough <janis.rough@gmail.com> writes:

> the output should be:
> a:1,b:1,j:4,c:1,d:1

The code does no output. You mean these should be the value in the map,
yes?

> I am trying to use a map and use the keys as the char and the values
> as the count but I'm getting undefined. thanks,

This looks like coursework, so my intuition is just to ask some
questions that I hope will lead you to an answer.

> function frequencies(){
> var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
> var key, value;
> for(i=0; i< arr.length; i++){

You should declare i as well. You can do it in the for statement.

> var myMap = new Map();
> if (arr[i]==arr[i+1]){

This will access beyond the end of the array and I can't see what it has
to do with counting. You want to set the map entry to 1 when you see a
letter for the first time, not when you see a letter that is different
to the one that follows it.

The key thing here is that you get the value out of the map. That is
what tells you if you have seen this letter before.

> myMap.set(key=i, value= value+1)

value has not been set. value = value + 1 sets value to one mode than
undefined. Not what you mean at all.

> }else{
> myMap.set(key=i,"1")

Why "1" rather than 1? Why key=i rather than i? Is i even the right
key?

> };
> };

The function returns nothing, so it has no point.

> }
> frequencies();

--
Ben.

jrough

1/22/2015 11:23:00 PM

0


I removed the map. It isn't homework. It was an interview question come up by some wiley programmer :-) IT doesn't like my arry assignment.
var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
function frequencies(arr){
var arry = arr;

for(i=0; i< arry.length; i++){
var key, value, i;
if (arry[i]==arry[i+1]){
var kvArray = [[key=i,value=value+1 ]];
}else{
kyArray = [[key=i, "1"]];
};
return kyArray;
};

}
frequencies();
frequencies();


On Thursday, January 22, 2015 at 3:09:44 PM UTC-8, Ben Bacarisse wrote:
> JRough <janis.rough@gmail.com> writes:
>
> > the output should be:
> > a:1,b:1,j:4,c:1,d:1
>
> The code does no output. You mean these should be the value in the map,
> yes?
>
> > I am trying to use a map and use the keys as the char and the values
> > as the count but I'm getting undefined. thanks,
>
> This looks like coursework, so my intuition is just to ask some
> questions that I hope will lead you to an answer.
>
> > function frequencies(){
> > var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
> > var key, value;
> > for(i=0; i< arr.length; i++){
>
> You should declare i as well. You can do it in the for statement.
>
> > var myMap = new Map();
> > if (arr[i]==arr[i+1]){
>
> This will access beyond the end of the array and I can't see what it has
> to do with counting. You want to set the map entry to 1 when you see a
> letter for the first time, not when you see a letter that is different
> to the one that follows it.
>
> The key thing here is that you get the value out of the map. That is
> what tells you if you have seen this letter before.
>
> > myMap.set(key=i, value= value+1)
>
> value has not been set. value = value + 1 sets value to one mode than
> undefined. Not what you mean at all.
>
> > }else{
> > myMap.set(key=i,"1")
>
> Why "1" rather than 1? Why key=i rather than i? Is i even the right
> key?
>
> > };
> > };
>
> The function returns nothing, so it has no point.
>
> > }
> > frequencies();
>
> --
> Ben.

jrough

1/22/2015 11:24:00 PM

0


var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
function frequencies(arr){
var arry = arr;

for(i=0; i< arry.length; i++){
var key, value, i;
if (arry[i]==arry[i+1]){
var kvArray = [[key=i,value=value+1 ]];
}else{
kyArray = [[key=i, "1"]];
};
return kyArray;
};

}
output =frequencies();
On Thursday, January 22, 2015 at 2:43:24 PM UTC-8, JRough wrote:
> the output should be:
> a:1,b:1,j:4,c:1,d:1
>
> I am trying to use a map and use the keys as the char and the values as the count but I'm getting undefined.
> thanks,
>
>
> function frequencies(){
> var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
> var key, value;
> for(i=0; i< arr.length; i++){
> var myMap = new Map();
> if (arr[i]==arr[i+1]){
> myMap.set(key=i, value= value+1)
> }else{
> myMap.set(key=i,"1")
> };
> };
>
> }
> frequencies();

Evertjan.

1/22/2015 11:36:00 PM

0

JRough <janis.rough@gmail.com> wrote on 22 jan 2015 in
comp.lang.javascript:

> the output should be:
> a:1,b:1,j:4,c:1,d:1
>
> I am trying to use a map and use the keys as the char and the values as
> the count but I'm getting undefined. thanks,
>
>
> function frequencies(){
> var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
> var key, value;
> for(i=0; i< arr.length; i++){
> var myMap = new Map();

Do you want a new map for every loop?

> if (arr[i]==arr[i+1]){

eh?

> myMap.set(key=i, value= value+1)
> }else{
> myMap.set(key=i,"1")
> };
> };

Don't you want to use the result somewhere?

>}
> frequencies();
>

Try this, using an object as target:

=============================
var arr = 'abjcdjjj'.split(''); // source array
var res = {}; // result object

function frequencies() {
for(i=0; i< arr.length; i++) {
if (!res[arr[i]])
res[arr[i]] = 0;
res[arr[i]] +=1;
};
};
frequencies();

console.log(res); // Object {a: 1, b: 1, j: 4, c: 1, d: 1}
=============================


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Denis McMahon

1/23/2015 2:16:00 AM

0

On Thu, 22 Jan 2015 14:43:18 -0800, JRough wrote:

> function frequencies(){
> var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
> var key, value;
> for(i=0; i< arr.length; i++){

i wasn't defined with a var statement. Not a critical error, but sloppy
if everything else is defined with var.

> var myMap = new Map();
> if (arr[i]==arr[i+1]){

This is the wrong test. You want to test if arr[i] is a key in myMap

if (myMap.has(i)) {

> myMap.set(key=i, value= value+1)

This isn't how you increment a value in a map. You have to retrieve the
value properly, and it helps to terminate the line with a semi-colon.
Additionally, do we use named arguments in javascript? As far as I know
only in user space functions using parameter objects, and even the ES6
support for these requires wrapping them in an object.

myMap.set(i, myMap.get(i) + 1);

> }else{
> myMap.set(key=i,"1")

Oops another missing semi-colon and a named parameter. Also, you probably
want to use a numeric 1 here for the value, not the character 1!

myMap.set(i, 1);

> };

Ah, that's where the semi-colons went to. But you don't need them here.

> };

or here!

> }
> frequencies();





--
Denis McMahon, denismfmcmahon@gmail.com

Denis McMahon

1/23/2015 2:18:00 AM

0

On Fri, 23 Jan 2015 02:16:26 +0000, Denis McMahon wrote:

> On Thu, 22 Jan 2015 14:43:18 -0800, JRough wrote:

>> var myMap = new Map();

And as Evertjan said, this should probably come before the for loop
starts too!

--
Denis McMahon, denismfmcmahon@gmail.com

Evertjan.

1/23/2015 8:06:00 AM

0

Denis McMahon <denismfmcmahon@gmail.com> wrote on 23 jan 2015 in
comp.lang.javascript:

>> };
>
> Ah, that's where the semi-colons went to. But you don't need them here.
>
>> };
>
> or here!
>

Depending on your hour of need,
I submit they are needed just as much
for a readable code,
showing the end of a statement.

> }

and here too.

> frequencies();



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Stefan Weiss

1/23/2015 8:54:00 AM

0

On 2015-01-23 09:06, Evertjan. wrote:
> Denis McMahon <denismfmcmahon@gmail.com> wrote on 23 jan 2015 in
> comp.lang.javascript:
>
>>> };
>>
>> Ah, that's where the semi-colons went to. But you don't need them here.
>>
>>> };
>>
>> or here!
>>
>
> Depending on your hour of need,
> I submit they are needed just as much
> for a readable code,
> showing the end of a statement.

The end of an extra empty statement, maybe.

Blocks do not end with a semicolon - ever. Adding one, possibly with the
intention of improving readability, actually changes the code
(marginally) by creating an unnecessary empty statement.

- stefan

Christoph M. Becker

1/23/2015 12:32:00 PM

0

Denis McMahon wrote:

> On Thu, 22 Jan 2015 14:43:18 -0800, JRough wrote:
>
>> function frequencies(){
>> var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>> var key, value;
>> for(i=0; i< arr.length; i++){
>
> i wasn't defined with a var statement. Not a critical error, but sloppy
> if everything else is defined with var.

It would still be sloppy, even if the other variables were not be
declared inside the function body. IOW, avoid "implied globals".

>> var myMap = new Map();
>> if (arr[i]==arr[i+1]){
>
> This is the wrong test. You want to test if arr[i] is a key in myMap
>
> if (myMap.has(i)) {
>
>> myMap.set(key=i, value= value+1)
>
> This isn't how you increment a value in a map. You have to retrieve the
> value properly, and it helps to terminate the line with a semi-colon.
> Additionally, do we use named arguments in javascript? As far as I know
> only in user space functions using parameter objects, and even the ES6
> support for these requires wrapping them in an object.

These are not named arguments, but rather assignment expressions.

--
Christoph M. Becker

Evertjan.

1/23/2015 12:38:00 PM

0

Stefan Weiss <krewecherl@gmail.com> wrote on 23 jan 2015 in
comp.lang.javascript:

> On 2015-01-23 09:06, Evertjan. wrote:
>> Denis McMahon <denismfmcmahon@gmail.com> wrote on 23 jan 2015 in
>> comp.lang.javascript:
>>
>>>> };
>>>
>>> Ah, that's where the semi-colons went to. But you don't need them here.
>>>
>>>> };
>>>
>>> or here!
>>>
>>
>> Depending on your hour of need,
>> I submit they are needed just as much
>> for a readable code,
>> showing the end of a statement.
>
> The end of an extra empty statement, maybe.
>
> Blocks do not end with a semicolon - ever. Adding one, possibly with the
> intention of improving readability, actually changes the code
> (marginally) by creating an unnecessary empty statement.

An unnecessary statement of you.
As I said, put a ; at the end of a statement:

if (condition) {statement;} else {statement;statement;};


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)