I want to start with a large number in a variable, and while it is larger than another number, subtract 100 from the variable. I then want the loop to put the variable in the "demo" paragraph, which should end up being 89. Any help is appreciated.
var x = 789
while x > 100 {
x = x-100
}
document.getElementById("demo").innerHTML = x;
<p id="demo"></p>
Your while condition must be wrapped in parentheses.
var x = 789
while (x > 100) {
x = x-100
}
document.getElementById("demo").innerHTML = x;
<p id="demo"></p>
Using parenthesis in the while will fix your problem, just a small syntactical error
var x = 789
while (x > 100) {
x = x-100
}
try this,
function sub(x){
while(x > 100){
x = x-100
}
return x
}
document.getElementById("demo").innerHTML = sub(789);
Uhm - ok - I am really really really rusty at programming, but... you want to generate a predetermined static number and place that static number in a static html element programmatically? WTF man?
<p id="demo">89</p>
Is your solution.
No Javascript req'd. Unless you were hoping for a 'countdown' effect which:
1) Wont work with this code.
2) Wont work with corrected code.
3) Would work with a different methodology so fast you wont see it (you would have to introduce 'sleep' timers and so on).
It's been a bad day please don't take a picture.
Related
I've been having a problem that when my auto clicker in my clicker game goes fast enough to get to 200 thousand, it starts to lag, and then it doesn't function properly, or as fast.
Is there a way to make 100 thousand turn into 100K, and 101 thousand turn into 101K without being repetitive?
I tried this with my original code, and realized putting up to 1000 suffixes into each function would be a little too hard:
if (number >= 100000) {
document.getElementById(ID).innerHTML = "100K"
}
if (number >= 101000) {
document.getElementById(ID).innerHTML = "101K"
}
and on and on.
I don't want multiple if statements!
This would work, but it would take up way too much space, and I know there is an easier way to it, but I just couldn't find it. Can anyone provide a way to do this?
Try separating the job of formatting your number into a different function.
SUFFIXES = 'KMBTqQsSOND' // or whatever you'd like them to be
function getSuffixedNumber(num) {
var power = Math.floor(Math.log10(num));
var index = Math.floor(power / 3);
num = Math.round(num / Math.pow(10, (index * 3))); // first 3 digits of the number
return num + (SUFFIXES[index - 1] || ''); // default to no suffix if we get an out of bounds index
}
You can call the function like this: var x = getSuffixedNumber(101000), the value of x will be "101K".
I´m learning javascript, so please be gentle with me.
About my question above, this is the code I made, the difficult part for me is related to the prompt, if the task were "show all the numbers from 1 to 10 using a while", I know how to do it.
var x = Number(prompt("Ingrese un número"));
var contador = 1;
while (contador <= x) {
console.log(contador + x);
contador++;
}
If you want to show number from 1 to user entered number then why you add x in console log try below code it will show 1 to x,
var x = Number(prompt("Ingrese un número"));
var contador = 1;
while (contador<=x) {
console.log(contador);
contador++;
}
If u need using while do this:
var x = Number(prompt("Ingrese un número"));
var contador = 1;
while (contador<=x) {
console.log(contador++); //return number and increase it for next loop
}
But the best and fastest solution is this:
var x = Number(prompt("Ingrese un número"));
for(var i = 1; i <= x; i++)
console.log(i);
I think you've understood the while part.
Here is what the prompt part does:
prompt creates a prompt window (like an alert window) which asks for user input. The user input, by default, is a string.
So we we need to convert it into a number. we use Number(someStrongValue) to convert this user input to a number.
Number("1") == 1
Number("a") == NaN
If you didn't convert the string to a number, you may end up with an infinite loop, or the loop may not even run.
If the user enters a string in prompt, Number will return NaN.
and any number < NaN is false. Hence, your loop won't run (and no infinite loop)
You can read more about prompt here: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt
I've had a look round and I'm a little confused.
There seems to be several responses to my particular problem but I don't fully understand.
So if anybody could help that'd be great!
Basically, I set a variable
var firstThumb = "02"
And I want to increase it
function thumbIncrease() {
firstThumb++
};
on an interval
setInterval(function(){
getThumb.trigger("click");
thumbIncrease();
}, 5000);
Basically what I expect to happen is -
Every 5 seconds a click is triggered, and I want my firstThumb variable to increase by 1.
So 02, click, 03, click, 04, click....
So each time it clicks a different element.
Any help would be great!
Thanks!!
EDIT
Thanks for the responses! I kind of understand now! So the reason I've got a 0, is more for me than for the code. I currently have 15 things that I need clicking. 01 and 15 being 2 characters, meaning my code looks neater. Should I just man up and remove the 0? Or is there a better way of doing it?
var counter = 1;
setInterval(function() {
var _text = counter + "<br>";
$("div").append(_text);
counter++;
},5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
</div>
If you want it to replace the old number use .html() instead of .append()
"01" is a string, 1 is a number you can increment.
If you just need zero padding:
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
var thumbNumber = "02";
thumbNumber = parseInt(thumbNumber);
var nextNumber = pad(++thumbNumber,2);
console.log(nextNumber);
Live Demo -- http://jsfiddle.net/v02vgw6w/2/
Very simple code, very simple problem. When a link is pressed, it moves a div either up or down. However, I cannot get it to move incrementally. I know this is a simple syntax error, but google isn't revealing the error of my ways. Anyone willing to enlighten me?
<a class="galsel" onclick="document.getElementById('innerscroll').style.bottom -='167px';">«</a>
<a class="galsel" onclick="document.getElementById('innerscroll').style.bottom +='167px';">»</a>
I already have it so that the div tiles itself vertically, so I'm not worried about it going "too high" or "too low"
Here's what it looks like right now: drainteractive.com/FBD/projects.php
You have to parse the value from the string containing px
// Increase by 167
document.getElementById('innerscroll').style.bottom = (parseInt(document.getElementById('innerscroll').style.bottom, 10) + 167) + ' px'
// Decrease by 167
document.getElementById('innerscroll').style.bottom = (parseInt(document.getElementById('innerscroll').style.bottom, 10) - 167) + ' px'
// Abstracted
function addToBottom(el, amount) {
// You probably add lower and upper bound check conditions
el.style.bottom = (parseInt(el.style.bottom) + amount) + ' px';
}
var el = document.getElementById('innerscroll');
addToBottom(el, 167);
addToBottom(el, -167);
Also be sure to make it work for cases where bottom wasn't set initially
var currentBottom = parseInt(document.getElementById('innerscroll').style.bottom) || 0;
+='167px' will concatinate it an it will become '167px167px167px167px167px'. Not sure what will result -='167px', but probably will result an error.
You need to rip the 'px' off the string, convert(?) it to an int, then subtract from that.
onclick="var mElem = document.getElementById('innerScroll'); mCur = parseInt(mElem.style.bottom.replace('px', 0)); mElem.style.bottom = (mCur-167)+'px'"
Naturally, this should all be put into a separate function, who is then called in the onclick, rather than the monstrosity above.
function moveUp()
{
var mElem = document.getElementById('innerScroll');
var mCur = parseInt(mElem.style.bottom.replace('px', 0));
mElem.style.bottom = (mCur-167)+'px';
}
...
<strike>onlick="moveUp()"</strike>
onclick="moveUp()"
My mind must have been somewhere else..
Hey again everyone. Yet again i am having some problems with trying to get the match correct on this Excel Spreadsheet to JavaScript conversion.
Here is the excel formula:
=IF(IF($B$7=TRUE,$B$28/$B$10,$B$28/$B$5)>1,1,IF($B$7=TRUE,$B$28/$B$10,$B$28/$B$5))
WHERE
B7 = TRUE
B28 = 76800
B10 = 892015
B5 = 999500
And this is my JavaScript i have so far:
function percent(x) { return Math.round((x-0)*100) + '%'; }
if($('#section179').is(':checked'))
{
var percentRepaid = $("#rev3DScanYear").val() / $("#section179Real").val();
if (percentRepaid > 1)
{
$("#paymentCashPer").val('100.00');
}else
{
percentRepaid = $("#rev3DScanYear").val() / $("#SalePrice").val();
$("#paymentCashPer").val(percent(percentRepaid));
}
}else
{
//to be done
}
WHERE
rev3DScanYear = 76800
SalePrice = 999500
section179Real = 892015
For the JavaScript code i keep getting a value of 8% and i should be getting a value of 8.61% as it has on the spreadsheet.
As always, any help would be great! :o)
David
Math.round((x-0)*100) makes x an integer.
You could try Math.round(((x-0)*100)*100)/100 which makes the x = 8.609720... into x=861 and then divides it to get the x=8.61 you're looking for, which is what they would suggest here.
...Also, not really sure why you're subtracting 0 from x...?
Ok, so I've been looking at this again, and I think I didn't look deeply enough the first time.
The logic, if I understand it, is this:
If Section179 is checked then Divisor is Section179Real, else it is SalePrice.
Give me the smaller of 1.00 or (rev3DScanYear / Divisor).
If that's correct, you can do it in excel with =MIN(1,$B28/IF($B$7=TRUE,$B$10,$B$5)) (same references), which means that the following should do what you want it to:
var Divisor = $("#SalePrice");
if($('#section179').is(':checked'))
{
Divisor = $("#section179Real");
}
$("#paymentCashPer").val(Math.round(100*(Math.min(1, $("#rev3DScanYear")/Divisor)*100)/100;