1

I am working on small shopping cart project. I have products in JSON.file, also I have input for finding price of products. I am using class method question is: this are strings or numbers? -> (3) ['35', '35', '35']


searchItem(data) {
    let that = this

    searchBtn.addEventListener('click', function(e) {
      
       const input = document.querySelector('.input').value

           
       const findItem = data.filter(function(item) {
          if(item.price === input) {
            return item
          }
        })   // this return all data of product so I filtered only prices bellow


        const getItem = findItem.map(item => {
          return item.price
        })                      
 
       // this give:    (3) ['35', '35', '35']  


    
         if(input === getItem) {     
             console.log('same') 
        } else {
          console.log('try it again') 
        }
                         
         // this cond. show me :  console.log('try it again')
         // HOW TO GET:   console.log('same')  
      
       e.preventDefault()
    })


3
  • 2
    Get the type with typeof myVariableToTest. By the way, the array contains strings. You could easily convert them all to numbers with something like arrayToConvert.map(i => +i);. Also, you are comparing an array to a string. What do you expect?? If you don't want to bother converting them to numbers you could take advantage of type coercion with using two == instead of three ===. Commented Jan 12, 2022 at 0:05
  • 1
    They are strings, but you are comparing the input (a string) with getItem (an array). Try if (input === getItem[0]) but that is guaranteed to match, since getItem is filtered based on the input value. Commented Jan 12, 2022 at 0:06
  • @code actually have a very good point by suggesting two == instead of three === Commented Jan 12, 2022 at 0:21

2 Answers 2

3

You can always run a typeof to find out what data types you dealing with

for example

console.log(typeof getItem[0])

also here :

        if(input === getItem) {     
             console.log('same') 
        } else {
          console.log('try it again') 
        }

you are checking the input variable against a whole array,you have to specify which item of the array to check against like :

if(input === getItem[0]) {     
             console.log('same') 
        } else {
          console.log('try it again') 
        }
Sign up to request clarification or add additional context in comments.

Comments

0

A quick and straight to your question without the code analysis, Any value gotten from your input is a string and not numbers.

Therefore, if the values of the price you are getting from data are integer, then you will have to consider parsing the values from the input by using the parseInt() method. Example;

const findItem = data.filter(function(item) {
      if(item.price === parseInt(input)) {
        return item
      }
    })

Another thing is that getItem is an array so, evaluating with a particular value from input is bound to fail. Therefore, use getItem[0] instead and two "==" instead of "===" just as suggested on the comment.

function searchItem(data) {
        let that = this

        searchBtn.addEventListener('click', function(e) {
        
        const input = document.querySelector('.input').value

            
        const findItem = data.filter(function(item) {
            if(item.price == input) {
                return item
            }
        })   // this return all data of product so I filtered only prices bellow


            const getItem = findItem.map(item => {
                return item.price
            })  
            
            console.log(getItem);
    
        // this give:    (3) ['35', '35', '35']  

            if(input == getItem[0]) {     
                console.log('same') 
            } else {
                console.log('try it again') 
            }
                            
            // this cond. show me :  console.log('try it again')
            // HOW TO GET:   console.log('same')  
        
        e.preventDefault()
        })
    }

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.