jrough
1/22/2015 11:23:00 PM
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.