<script type="text/javascript">
function NewHouse(){
if(firstHouse >= 1) alert(window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")){
window.location.href='url here';
}
};
</script>
Thank you for replying. I tried what you said and got this.
<script type="text/javascript">
function NewHouse(){
if(firstHouse >= 1){
window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
window.location.replace("url goes herel");
};
};
</script>
i am confused by what you want, do you want to know if the user pressed ok or cancel on the confirm dialog?
that is simple, the confirm function returns true (ok button) or false (cancel button), so all we need to do is capture the returned result in a variable, and use it in if/else comparison
Thank your pointing that out. I did what you said got this
<script type="text/javascript">
function NewHouse(){
if(firstHouse >= 1){
var Confirm = window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
if(Confirm = true ){
window.location.replace("url goes here.");
};
};
};
</script>
But still nothing. What i want to do is have a button that you can buy something (i already have the button and it works) once you buy it the first house var = 1. i want it so that once the first house var = 1 the window confirm will open
why do we only get such a small portion of the code? With the full code, we can help much better, please provide full example, otherwise we just keep going back and forth
Because currently, i can’t see what should trigger the NewHouse function.
also, here:
if(Confirm = true )
a single equal sign means assign, so Confirm gets assigned a value of true, then is evaluated to true, so this condition is always true
then this function which increases first house value, should call NewHouse function, given we are not provided with the full code, difficult to help you with this
I know this isn’t an indentation issue, as JS doesn’t have those, but it can’t hurt to fix the indentation in your code.
Your code currently looks like this:
function NewHouse(){
if(firstHouse >= 1){
var Confirm = window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
if(Confirm = true ){
window.location.replace("url goes here.");
};
};
};
Whereas it should look like this:
function NewHouse(){
if(firstHouse >= 1){
var Confirm = window.confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
if(Confirm = true){
window.location.replace("url goes here.");
};
};
};
Again, just to re-iterate, both versions shown will operate in the same way. However, the latter of the two is considered best-practice.
There are six things that still do not follow conventional practice…
Only classes should start with a capital. NewHouse is not a class in the strictest sense; and, neither is Confirm. Both should start with a lowercase letter.
window.confirm is overkill. confirm() works fine since it is a global function.
if (Confirm = true) should be, if (confirmed === true), or better still, if (confirmed).
if does not end with a semi-colon.
Same again, on the outer if statement.
Function declarations do not end with a semi-colon.
I was really only talking about the indentation, but I agree that this:
function newHouse(){
if(firstHouse >= 1){
var confirmed = confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
if(confirmed){
window.location.replace("url goes here.");
}
}
}
In JavaScript the indentation is optional, and only added to improve readability. JS statements are their own miniature program since they are interpreted and JIT compiled one at a time, and executed. We do not need to tell the interpreter how our blocks are nested.
Thank you guys so much for helping me with this. I’m pretty sure what i want to do though is impossible because even after all your guys help it still doesn’t work. Thank again for your feedback. This is the full code.
<p>First House: <span id="firstHouse">0</span><button style=" height: 50px; width: 200px; background-color: red; border-color: #009688;" onclick="buyHouse1()">Buy First house</button> Cost: <span id="House1Cost">100000</span></p>
<script>
var firstHouse = 0;
function buyHouse1(){
var House1Cost = Math.floor(100000 * Math.pow(1.1,firstHouse))
if (fruit_sliced >= House1Cost){
firstHouse = (firstHouse + 1);
fruit_sliced = fruit_sliced - House1Cost;
document.getElementById('firstHouse').innerHTML = firstHouse;
document.getElementById('fruit_sliced').innerHTML = fruit_sliced;
};
};
</script>
<script type="text/javascript">
function newHouse(){
if(firstHouse >= 1){
var confirmed = confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
if(confirmed){
window.location.replace("url goes here.");
}
}
}
</script>
why do you have the function in two different scripts? We can have many functions withing a single script
i seem to have already guessed the cause of the problem correctly, function only execute when called. The newHouse is never called, so it never execute
you could call the newHouse function from the buyHouse1 function, yes, function can call functions
Yes finally it worked Thank you guys so much for your help this is how i got it to work.
function buyHouse1(){
newHouse1();
var House1Cost =Math.floor(100000 * Math.pow(1.1,firstHouse))
if (fruit_sliced >= House1Cost){
firstHouse = (firstHouse + 1);
fruit_sliced = fruit_sliced - House1Cost;
document.getElementById('firstHouse').innerHTML = firstHouse;
document.getElementById('fruit_sliced').innerHTML = fruit_sliced;
};
function newHouse1(){
if(firstHouse >= 1){
var confirmed = confirm("Congratulations on buying your brand new house. P.S. It's only new to you. Also click ok to continue.")
if(confirmed){
window.location.replace("url goes here.");
}
}
}
};
buyHouse1();