Good evening!
I wanted to start learning HTML, and im having a hard time understanding the Loops. Im trying to make a code, that checks if the numbers in a variable are either positive or negative. If i have, lets say 5 numbers, the result should give 5 lines and each line like this The number "checkednumber " is negative.
This is what i have now, and i just cant get the hang of it.
edit: i dont know why i cant paste my code, so i took a screenshot in the comments! ^^
The problem is that you’re attempting to do a comparison between an array and an integer. Since var numbers is of type array, you can’t compare it directly with an integer to get your intended result.
You will need to declare a different variable for your while loop comparison and then increment it within your loop to achieve your desired result. If you’re not using x, then it could be the perfect candidate.
Edit: Well, there are other issues that I see as I review the code further. (I kind of stopped on the first issue, my bad). Your format for the while loop is wrong. You can’t do while () {} else {}, that’s just invalid syntax: https://www.w3schools.com/js/js_loop_while.asp
You’re going to need to use an if statement inside of your while loop to check if a given number is positive or negative, and then you can set the variable text inside of it’s if/else block to whatever you want.
First of all to paste code on here you need to use backticks x 3 before your code and after >like this
e.g
myStr += "Can you see" + myStr +;
The problem is, i have no idea how to increment it. I was googling around a bit, and i only see things like array.push. Is this correct?
Aha alright, good to know, thank you for telling me!
Are you familiar with this type of logic?
var x = 1;
x = x +1?
That’s how you increment a variable. Then, use an if statement inside the while loop to check if it reaches a threshold and call break. My w3schools link above shows you how to do it. (https://www.w3schools.com/js/js_loop_while.asp)
You may need to do a bit more study on beginner material to get a good on how to achieve your desired result. I described what you need to do, but actually coding it out may be difficult without a more solid foundation. Here is the solution basically:
var num = [1,2,3];
var x = 1;
while (x < 5) {
if (some code to check if a num is positive) {
text = 'this'
} else {
text = 'that'
}
x++;
if (x > 5) {
break
}
}
edit, sec… got interrupted and accidentally hit enter,
Now i have this, but it gives me " This number is negative.This number is negative.This number is negative.This number is negative. "
Why does it give me negative? Its clearly not checking var num [1,2,3]
Here is one answer of how to do it, there may be more efficient ways, but I think this example is good enough for simply demonstrating what your trying to do. reply if you have any more questions, for further homework research the ‘switch statement’
codepen
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>positive or negative</title>
</head>
<body>
<div id="array"></div>
<script>
$(document).ready(function () {
var array = [32, -4, -1.2, 5, -340, 4.5, 0];
for (var i in array) {
var element = document.createElement("h2");
var check=''
if ( array[i]> 0){
check='positive';
} else if ( array[i]< 0){
check='negative';
} else {
check= 'neither positive nor negative'
}
element.innerHTML = array[i] +' is '+ check+' number';
$("#array").append(element);
}
});
</script>
</body>
</html>
What does the script src=“ajax.google…” do exactly? is there any way to not have that there?
I used jquery to do it, so that is a reference to that library, I had to include it in order to use the jquery functionality, but you dont need it to run a loop to check every element in your array.
I saw your code, there are some things that were already said, you are concatenating the wrong way and the way you are looping is wrong too; there are some things that you should learn that are well explained in the FCC challenges, in the javascript section; things like how to access to an Array and how to loop through an Array.
Plus, im not sure if you want to show these 5 lines in your html code, that is a different thing, if im not wrong jQuery is what you need. There is a jQuery section in the FCC challenges too; that is a way to edit the html code using Javascript.
I really recommend you to check the challenges, if you wanna just jump into javascript section, i think you can do it, since the basic javascript section is not really related to html and css.
Alright, Jquery is something ive never used before too, ill do some research about it.
Im trying to place a Span to make the word Positive green, and negative red, but its very confusing to add it. Is this possible?
ok if you dont want to use jquery ok here is the same code without jquery:
play around with it. Jquery just saves you from typing some extra things. Up to you how you want to do it, choose either way.
no jquery
And also, lets say, i want to check multiple things at once. ill give you an example, i want to check if its positive or negative, but also if its a point number ( number with a , in it, dont know the translation ). Is this possible in the same loop? If yes how would i do that?
i cant comment more since its my first day on this site, i was about to say: Aha, so it is possible without jquery too! good to know!