none
JavaScript (.indexOf()) RRS feed

  • General discussion

  • Hello I am beginner in learning JavaScript. 

    I have the code of adding items to a list, but I want now to detect the duplicated items and delete it from the list.

    I have tried to create a function at the beginning of my code and call it inside the code block of the (do-while loop). But it did ot work. 

    I hope someone can help me in doing this. 

    
    
    const readline = require('readline-sync')
    
    const items = []
    /*var input = []*/
    function checkingTheArray (input, item, items){
    		if (input.indexOf(item) === -1){
    			input.indexOf(item) 
    		}
    		else if (input.indexOf(item) > -1){
    			console.log(item + `already exist in the list`)
    		}
    	}
    
    do {
    	var input = String(readline.question('enter command: ')).trim()
    	if (input.indexOf('add ') === 0) {
    		const space = input.indexOf(' ')
    		const item = input.substring(space).trim()
    		console.log(`adding "${item}"`)
    		items.unshift(item)
    	}
    	if (input.indexOf('list') === 0) {
    		for (let i=0; i< items.length; i++) {
    			console.log(`${i}. ${items[i]}`)	
    			checkingTheArray(input)    /*calling the function*/
    		}
    	}
    	
    } while (input !== 'exit')


      
    • Changed type Bill_Stewart Monday, March 19, 2018 5:48 PM
    • Moved by Bill_Stewart Monday, March 19, 2018 5:49 PM This is not javascript training forum
    Wednesday, February 21, 2018 3:17 PM

All replies