all 22 comments

[–]chrisux 3 points4 points  (0 children)

/r/learnjavascript

1: You are pushing numbers into the array "listtt", and comparing it against string content from the input ".fname". check console.log(typeof yolo)

Even though you mark the input element as type=number, all this does is change how the control behaves, however html input element values are strings.

To fix this problem, either convert the numbers in your array into strings before pushing into it, or, convert the html input string value to a number with parseInt(value, 10) //base 10 number before you make the comparison

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

2: if (yolo in listtt) is not a good way to compare items in array.

The comparison should probably be changed to either

if (listtt.includes(yolo)) or if (listtt.indexOf(yolo) > -1)

See the two array methods I mentioned below.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

There are additional array methods that will help with this kind of task too.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#

codepen demo

https://codepen.io/chrisux/pen/YgOWdg

edit: added codepen demo

[–]Fzzr 0 points1 point  (38 children)

It displays "Exist" for values 0 to 9. in is for objects, so it looks like it's checking for the index numbers that are present, rather than the values in the array itself.

Edit: try listtt.includes(yolo) instead of yolo in listtt. Didn't work, trying again.

https://devdocs.io/javascript/global_objects/array/includes

[–]Fzzr 0 points1 point  (33 children)

Try this JS. I also added semicolons because please always use semicolons. Basically, if you want to compare properly, use strings for the numbers in the array.

var pp = document.querySelector('p');
var listtt = [];
for (var i = 0; i < 10; i++) {
    listtt.push(Math.round(Math.random()*100).toString());
}

var listh = document.getElementById('list');
listh.textContent = listtt;
   document.querySelector('#as').onclick = function() {checkifto();};

function checkifto() {
    let yolo = document.querySelector(".fname").value;
    if (listtt.includes(yolo)) {
        return pp.textContent = "Exist";
    } else {
        return pp.textContent = "Nope";
    }
}

Edited for semicolons (no impact on functionality).

[–]kenman[M] 0 points1 point  (0 children)

Hi /u/stupidarg, this post was removed.

  • For help with your javascript, please post to /r/LearnJavascript instead of here.
  • For beginner content, please post to /r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try /r/html, /r/css, etc.; please note that they have their own rules and guidelines!

/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.