-2

Can I insert if statement in else statement ? The code :

function swicthNum() {
  'use strict';
  var processDoc = document.getElementById('process').innerHTML;
  if (processDoc == 'Dollars to Algerian Dinar') {
    processDoc = 'Algerian Dinar to Dollars';
  } else {
    processDoc = 'Dollars to Algerian Dinar';
  }
  return processDoc;
}
function moneyCalc() {
  'use strict';
  var num = document.getElementById('moneyNum').value,
      lastResultDoc = document.getElementById('lastResult'),
      result = num * 107.39,
      lastResult = document.getElementById('lastResult');
  if (num < 0) {
    lastResult.innerHTML = 'The process don\'t accept negative numbers';
  } else {
    if (processDoc == 'Dollars to Algerian Dinar') {
      lastResult.innerHTML = result;
    } else {
      result = num * 0.0093;
      lastResult.innerHTML = result;
    }
  }
}
<p id="process">
  Dollars to Algerian Dinar
</p>
<button onclick="swicthNum()">Switch!</button>
<br />
<form action="AdvancedFunction.html" method="post">
  <input id="moneyNum" name="" type="number" value="" />
  <input onclick="moneyCalc()" name="" type="submit" value="Calculate" />
</form>
<div id="lastResult">Result</div>

1 Answers1

0

You definitely can, but in your case you could simply go for:

if (num < 0) {
  lastResult.innerHTML = 'The process don\'t accept negative numbers';
} 
else if (processDoc == 'Dollars to Euro') {
    lastResult.innerHTML = result;
} 
else {
    result = num * 0.0093;
    lastResult.innerHTML = result;
}

It might be simpler to read.

Why your code wasn't working

  • You set the variable processDoc in a function, so it was not available for the other function: you have to set it globally.
  • There were spaces at the beginning of the "process"'s innerHTML, so switchNum() never changed its content: using indexOf, we can check if the desired string is contained in the paragraph, but not necessarily equal to the paragraph's content.
  • You didn't change innerHTML of process at the end of the switchNum function.
  • On click on 'Calculate', the form was submitted, so it wasn't possible to see the result of the calculation. To prevent that, use return false;.

Working code

function swicthNum() {
  'use strict';
  window.processDoc = document.getElementById('process').innerHTML;
  if (window.processDoc.indexOf('Dollars to Algerian Dinar')>-1) {
    window.processDoc = 'Algerian Dinar to Dollars';
  } else {
    window.processDoc = 'Dollars to Algerian Dinar';
  }
    document.getElementById('process').innerHTML = window.processDoc;
  return window.processDoc;
}
function moneyCalc() {
  'use strict';
  var num = document.getElementById('moneyNum').value,
      lastResultDoc = document.getElementById('lastResult'),
      result = num * 107.39,
      lastResult = document.getElementById('lastResult');
  if (num < 0) {
    lastResult.innerHTML = 'The process don\'t accept negative numbers';
  } else {
    if (window.processDoc == 'Dollars to Algerian Dinar') {
      lastResult.innerHTML = result;
    } else {
      result = num * 0.0093;
      lastResult.innerHTML = result;
    }
  }
}
<p id="process">Dollars to Algerian Dinar</p>
<button onclick="swicthNum()">Switch!</button>
<br />
<form action="#" method="post">
  <input id="moneyNum" name="" type="number" value="" />
  <input onclick="moneyCalc(); return false;" name="" type="submit" value="Calculate" />
</form>
<div id="lastResult">Result</div>
Community
  • 1
  • 1
Stubborn
  • 995
  • 4
  • 17
  • 30
  • 1
    May ignore the brackets also (just to input more faster): `if(num<0)lastResult.innerHTML=...,alert(1);else if(proccessDoc...==...);else ...`. There's the `switch` also if they're many checkments. –  Jan 05 '16 at 18:53
  • 2
    @ProHands - It is generally bad practice to skip the braces as this can lead to accidental coding mistakes very easily. – jfriend00 Jan 05 '16 at 18:58
  • thanks man but no, i wan't to use my method not Else if statement but my code doesn't work! – Saad Laggoune Jan 05 '16 at 19:19
  • @jfriend00 It depends on what you're doing, but there you have to use commas instead of semicolons, nor for everything works when using DOM. –  Jan 05 '16 at 19:22
  • @SaadLaggoune could you be more specific? Please have a look at your browser console and post the errors that it points out :) – Stubborn Jan 05 '16 at 19:24
  • 1
    @ProHands - We're clearing advising a newbie here. I wouldn't be going into commas and skipping braces with a Javascript newbie. The advice should be appropriate for the audience. – jfriend00 Jan 05 '16 at 19:24
  • @jfriend00 give me more info please – Saad Laggoune Jan 05 '16 at 19:26
  • @SaadLaggoune could you please update the question with the full code and explain what it should do and what isn't working? – Stubborn Jan 05 '16 at 19:27
  • 1
    @Stubborn HTML doc in coming – Saad Laggoune Jan 05 '16 at 19:31
  • @SaadLaggoune sorry for delay, see my updated answer for the working code – Stubborn Jan 05 '16 at 21:20