#how do i use the if and else statement to prove that the input i wrote in the text is in the array?

25 messages · Page 1 of 1 (latest)

patent temple
#

hi, im still a beginner and im having a hard time understanding array and const and booleans.

#

so ive got this problem

#

i let x be the input to navigate or search the array if the input is found inside the array

#

and then i have to display a boolean true if the input is inside and false if its not

#

i tried using array.includes but i didnt really know how that worked so i got myself seated on just using if and else

#
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Find the largest of two numbers</title> 
    </head>

    <body>

        <input type="button" value="Find" id = "input" onclick="findelement(x)">
        <input type="text" id="x">
    
        <script type="text/javascript">
    
        // X = input, to find the element inside the array
        // Condition: 
        // If an element is found inside : true
        // If not : false

        function findelement(){
        const language = ['PHP', 'CSS', 'Javascript', 'Hello', 'Laravel', 'Node'];
        let x = document.getElementById("x").value;
            if (x === language){
                console.log("True")
            } else {
                console.log("False")
            }
        }
            
    </script>

    </body>

</html>
#

it should display true if i write the right element but it doesnt

#

it just says false on the console

#

i searched it up on google and youtube, but i cant find a thing that is close to giving me an idea on how to solve this problem

rose root
#

You're comparing a single value to an entire array

rose root
#

Your x is "PHP" in your screenshot

#

Your comparison (x === language) is essentially this:

if "PHP" === ['PHP', 'CSS', 'Javascript', 'Hello', 'Laravel', 'Node']:
#

Which will never be equal

patent temple
#

you save my life

rose root
#

if (x === language){

patent temple
#

it worked

rose root
#

Replace the (x === language)

patent temple
#

yesss

#

i just did it just minutes ago

#

thank you sooo much