0

I have been trying this and i have no idea what i am doing , i think it will use for loop but i dont know how to use it

    <script LANGUAGE="javascript">
    function check(a)
    {
        if(a===list[a])
    {
    document.write("Number is Present");

    }else{
    document.write("Number is not Present");
    }
    }
    </script>
    <script LANGUAGE="javascript">
    var list=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
    var n = prompt("Enter any Number");
    check(n);
    console.log(list);
    </script>
  • 1
    What do you think `list[a]` means? You should learn the basics of Javascript and arrays. Look at the `includes()` method, but beware of the difference between numbers and strings. – SLaks May 07 '18 at 17:16
  • `list.indexOf(a) > -1 ? 'Present' : 'Not present'` – Muhammad Usman May 07 '18 at 17:16
  • 1
    Re the dupetarget: Although it's asking about an "object," really it's just asking about a value (an object *reference*) in an array. Your numbers are also just values in an array, so the answers there answer this question, too. (SO's definition of a "duplicate.") See those answers for details. – T.J. Crowder May 07 '18 at 17:31

4 Answers4

1

Here you have. You should learn some javacript. Code.org is a fun place to start

// Define your list before the function
var list=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
function check(a) {
  a = parseInt(a); // Convert your user input string to integer
  if(list.indexOf(a)>-1) { // Check if value exists in list
    document.write("Number is Present");
  }else{
    document.write("Number is not Present");
  }
}
// Prompt user for data
var n = prompt("Enter any Number");
check(n);
console.log(list);
Sanxofon
  • 759
  • 9
  • 20
1

function check(a) {
  if (list.indexOf(a) > -1) {
    document.write("Number is Present");
  } else {
    document.write("Number is not Present");
  }
}

var list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
var n = prompt("Enter any Number");
check(n);
console.log(list);

The problem with your code is that the parameter 'a' that you are sending to the check function, is the value you entered. But when you check

if(a===list[a])

the list[a] is the number with the index 'a'.

For example, if i pressed 3, then list[3] would mean 40. (since arrays start from 0)

Afron Orana
  • 625
  • 5
  • 14
0

Try indexOf

function check(a)
{
    if(list.indexOf(a) > -1)
    {
        document.write("Number is Present");  
    }
    else{
        document.write("Number is not Present");
    }
}
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
0

What you're doing is just comparing a with the array value present at that index in the array. For example if user types a= 10 then a==list[a] means check if list[10] equals to 10 that means if value in list array at index 10 is equal to the typed value. And of course you'll get undefined many times if user typed a big number.

What you are looking for is .indexOf. This will return you the indexif element is in the array or -1 otherwise.

var list = [1,2,3,4,5,6,7,8,9,10,11,12,14]
var n = prompt("type a number");

console.log(typeof n);

n = parseInt(n);

list.indexOf(n) > -1 ? console.log('In there') : console.log('Not there');

Moreover, you've array of integers and anything you type in prompt will be converted to string. Check console.log(typeof n);. So you'll have to parse it into int to check for your array elements.

Muhammad Usman
  • 10,039
  • 22
  • 39