0

I want to iterate over two arrays and check if the second array has any values which match the first one. I am currently trying to do it as follows, but im sure its incorrect. How can I do it ?

changeType = (el,shapeType) ->
 elTypes = ['circle','square','triangle']
 elClasses = el.attr 'class'
 classTypes = elClasses.split " " 

  for type of elTypes
    for types of classTypes
    el.removeClass type  if classTypes[types] is elTypes[type] 
 el.addClass shapeType

[Edit] while structure which seems to also be incorrect

 x = 0
 while x <= classTypes.length
   i = 0
   while i <= elTypes.length
     if elTypes[i] is classTypes[x]
        el.removeClass elTypes[i]
   i++
  x++
  el.addClass type
2
  • 1
    The attr and removeClass calls suggest that you're using jQuery, is this true? Commented May 24, 2014 at 16:56
  • yes it is true.. however, the syntax of this loop is wrong when compiled to js .. Can you be kind enough and direct me to any resoure which explains how i can iterate over two arrays and check against each of their values? I also tried using while structure Commented May 24, 2014 at 16:58

3 Answers 3

1

This should work

changeType = (el,shapeType) ->
    elTypes = ['circle','square','triangle']
    elClasses = el.attr 'class'
    classTypes = elClasses.split " " 

    for type of elTypes
        for types of classTypes
            if types == type then el.removeClass type 
     el.addClass shapeType
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer! However, this still doesn't work for some weird reason. Im baffled...
0

Your indentation right here is wrong:

for type of elTypes
  for types of classTypes
  el.removeClass type  if classTypes[types] is elTypes[type]

That el.removeClass should be indented to get it inside the for types loop.

But all that is pointless busywork. removeClass can remove multiple classes on its own:

More than one class may be removed at a time, separated by a space, from the set of matched elements, like so:

$( "p" ).removeClass( "myClass yourClass" )

Don't bother with the loops, let jQuery deal with it:

changeType = (el, shapeType) ->
  el.removeClass('circle square triangle').addClass(shapeType)

I think that's what you're really after.


I also notice that you seem to be mixing one-space and two-space indentation in your CoffeeScript. That's not exactly the best idea you've ever had. Choose an indentation amount and use it consistently in your CoffeeScript: indentation defines the structure of your CoffeeScript so you must be very careful with it.

4 Comments

Thanks sir for your honest and spot on answer! However, I am seeking to preserve the classes, but replace the shape class, ie. circle class, with the square class, for instance.
But replacing circle with square could be done using el.removeClass('circle').addClass('square') if(el.hasClass('circle')) so I'd still do it will jQuery rather than loops.
True, but the idea is to remove the class dynamically. Which calls for loops.. the loops are simply there to iterate of a list of classes. Have i misunderstood you ?
Maybe. jQuery doesn't know the difference between el.removeClass('x') and c = 'x'; el.removeClass(c). And you can el.removeClass(c) even if el doesn't have the c class. So your whole loop is just a complicated way of saying el.removeClass('circle square triangle'), there classes will be left alone.
0

OK, this was a petty mistake.This worked of course :

 for types of classTypes
        for type of elTypes
            if classTypes[types] == elTypes[type] then el.removeClass elTypes[type] 
     el.addClass shapeClass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.