0

I need to edit an value in the object. Add a new word(value) to the object's value. But add this new word if only it is'n already there.

e.g.:

var obj = {className: 'open menu'}

new word (value) - 'new', result -> obj.className='open menu new'

new word (value) - 'open ', result -> obj.className='open menu' (the same)

THis is my code:

function addClass(object, value){   
  var str = object.className;  //taking a string from object value
  var arr = str.split(' ');  // converting it to massive

  if (arr.indexOf(value) !== undefined) { //proceed if value exist 
       str = str + " " + value     // making new sring   
       object.className = str 

     {
  return object.className
}

var obj = {
  className: 'open menu'
}

console.log(addClass(obj, 'new')); // obj.className='open menu new' 
console.log(addClass(obj, 'open'));  // No change    , but I get 'open menu open' !

What is wrong? Help, please.

2 Answers 2

5
  if (arr.indexOf(value) !== undefined) { //proceed if value exist 

indexOf will never return undefined. If it's not found, it returns -1. Use that, and it should be fine:

  if (arr.indexOf(value) !== -1) { //proceed if value exist 
Sign up to request clarification or add additional context in comments.

1 Comment

You have another error with a backwards brace a couple lines down.
2

There is a syntax error in your example above. The if statement is not properly closed. I suspect it was just a typo since it seems you were running it in your application and getting results.

Aside from this, you were so close. You actually want to run the code in the if statement when the word does not exist. Array.prototype.indexOf() returns -1 if a value is not found, it does not return undefined.

function addClass(object, value){   
  var str = object.className;  //taking a string from object value
  var arr = str.split(' ');  // converting it to massive

  if (arr.indexOf(value) === -1) { //proceed if value DOES NOT exist 
    str = str + " " + value;     // making new sring   
    object.className = str; 
  }
  return object.className;
}

var obj = {
  className: 'open menu'
}

console.log(addClass(obj, 'new')); // obj.className='open menu new' 
console.log(addClass(obj, 'open'));  // No change    , but I get 'open menu open' !

2 Comments

think here is mistake in ligic. It dosn't work. addClass(obj, 'new') suppose return 'open menu open', but actuel result 'open menu'
Ok, I'm confused now. What you are saying seems to contradict your initial question. As I understood it, you previously wrote that if the new word is 'new', the result would be 'open menu new', and if the new word is 'open', the result would be 'open menu'(the same since the new word in this case already exists in the obj.className string). My example above, in my tests, is doing exactly that. Can you clarify?

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.