JavaScript JavaScript adding two numbers, adding vs concatenate, FCC Golf Code Challenge javascript adding numbers as strings,javascript adding numbers in an

I've been struggling to figure out why my If Else statement is returning incorrectly with any value after Par. I believe it's because my If Else is attempting to concatenate instead of add, but I can't figure out how to fix it. Thanks! http://codepen.io/SalvatoreSantamaria/pen/XNGNqZ

function x(par, strokes) {

  if (strokes == 1) {
    return "Hole-in-one!";
  } else if (strokes <= par - 2) {
    return "Eagle";
  } else if (strokes == par - 1) {
    return "Birdie";
  } else if (strokes == par) {
    return "Par";
  } else if (strokes == par + 1) {
    return "Bogey";
  } else if (strokes == par + 2) {
    return "Double Bogey";
  } else if (strokes >= par + 3) {
    return "Go Home!";
  }
}

document.getElementById("submitText").addEventListener("click", function() {
  input = document.getElementById("inputText").value;
  input2 = document.getElementById("inputText2").value;
  document.getElementById("output").innerHTML = x(input, input2);
});
</br>
Input Par
<input type="number" id="inputText" />
</br>
Input Strokes
<input type="number" id="inputText2" />
</br>
<input type="Submit" id="submitText" value="Submit" </input>
<div id="output"></div>
Answer:1

It's actually very simple. ALL values taken from form elements (input elements in your case) are strings, not numbers.

In order to do math with them, you must first convert them to numbers.

Modify your line to use the parseInt() JavaScript function:

x(parseInt(input,10), parseInt(input2,10));

A better way to structure your code would be to not store the values of the input elements, but to store references to the elements themselves. That way you can keep referencing the objects over and over without having to scan for them over and over.

input = document.getElementById("inputText");
input2 = document.getElementById("inputText2");

var input1Num = parseInt(input.value, 10);
var input2Num = parseInt(input2.value, 10);
x(input1Num, input2Num);

Finally, as others have pointed out, some of your HTML is incorrect as you have markup like this:

</br>  

and

<input type="Submit" id="submitText" value="Submit" </input>

Both of these errors are rooted in the misunderstanding of what elements must be terminated and what elements may not be explicitly terminated with a closing tag. Along with this is the (legal but largely irrelevant these days) syntax of "self-terminating" elements, which is a form of syntax that is used in a variant of HTML called XHTML. While this syntax is legal in HTML5, I highly discourage its use. For details on why, read my post here.

So finally, a finished version of the code would be:

// Get references to the DOM elements you'll need (not properties of the objects)
var input1 = document.getElementById("inputText1");
var input2 = document.getElementById("inputText2");
var output = document.getElementById("output");
var btn = document.getElementById("submitText");

// Wire up the button to run your function when it's clicked
btn.addEventListener("click", function(){
  // Only get the input values when you need to run your function
  var input1Num = parseInt(input1.value, 10);
  var input2Num = parseInt(input2.value, 10);

  // Call the function and pass it the values it needs
  output.textContent = x(input1Num, input2Num);
});
    

function x(par, strokes) {

  // You are going to return a string no matter what
  var result = "";
  
  if (strokes == 1) {
    result = "Hole-in-one!";
  } else if (strokes <= par - 2) {
    result = "Eagle";
  } else if (strokes == par - 1) {
    result = "Birdie";
  } else if (strokes == par) {
    result = "Par";
  } else if (strokes == par + 1) {
    result = "Bogey";
  } else if (strokes == par + 2) {
    result = "Double Bogey";
  } else if (strokes >= par + 3) {
    result = "Go Home!";
  }
  
  // Now just return once:
  return result;
}
<br>
Input Par
<input type="number" id="inputText1">
<br>
Input Strokes
<input type="number" id="inputText2">
<br>
<!-- Just use a regular button here, since you aren't actually
     submitting any form data anywhere -->
<input type="button" id="submitText" value="Submit">
<div id="output"></div>
Answer:2

If I'm rendering combobox like this $('#MyCombo').html('<option value=""></option>'); $.each(mydata, function (i, v) { $('#MyCombo').append('<option value="' + v....

If I'm rendering combobox like this $('#MyCombo').html('<option value=""></option>'); $.each(mydata, function (i, v) { $('#MyCombo').append('<option value="' + v....

  1. setting selected value in dropdownlist jquery
  2. setting selected value in dropdownlist javascript
  3. setting selected value in dropdownlist c#
  4. setting selected value in dropdownlist mvc
  5. setting selected value in dropdown list angularjs
  6. setting selected value jquery
  7. setting selected value in dropdown list angular
  8. jquery setting selected value in dropdown
  9. setting dropdown selected value in javascript
  10. setting the selected value of a dropdownlist
  11. javascript setting selected value
  12. dropdownlist not setting selected value
  13. dropdownlistfor not setting selected value
  14. selectlist not setting selected value
  15. c# combobox setting selected value
  16. html.dropdownlist setting selected value
  17. html.dropdownlistfor not setting selected value
  18. mvc dropdownlistfor not setting selected value
  19. html dropdownlist not setting selected value
  20. mvc dropdownlist not setting selected value

I am communicating from page2 controller to page1 controller in different views using $.emit and $on app.controller('mainController', ['$scope', function($scope) { $scope.message = "hello ...

I am communicating from page2 controller to page1 controller in different views using $.emit and $on app.controller('mainController', ['$scope', function($scope) { $scope.message = "hello ...

  1. angularjs communication between components
  2. angularjs communication between controllers
  3. angularjs communication between directives
  4. angularjs communication between sibling directives
  5. angularjs communication between modules
  6. angularjs communication between sibling components
  7. angularjs communication between controller and directive
  8. angularjs communication between services
  9. angularjs communication between tabs
  10. angularjs communication with server
  11. angularjs directive communication between link and controller
  12. communication between angularjs and angular
  13. angularjs communication between child

I am developing a small website for a school project. On one page I iterate over an array which is populated via SQL query. The data will be displayed like this: <ul class="list-group"> <...

I am developing a small website for a school project. On one page I iterate over an array which is populated via SQL query. The data will be displayed like this: <ul class="list-group"> <...

  1. value from element jquery
  2. element value from
  3. value element of art
  4. value element of art definition
  5. value element of design
  6. value element of art example
  7. value element of art photography
  8. value element in photography
  9. value element of art worksheet
  10. value element in html
  11. value of element javascript
  12. value of element n403 is incorrect
  13. value of element nm109 is incorrect
  14. value of element js
  15. value of element xpath
  16. element value in xml
  17. value of element 118
  18. value of element xslt
  19. value of element xsl
  20. element value in selenium

I'm building an hybrid app using ionic. Inside this app I decided to have Require.js as a dependency loader (maybe not the best choice but I'm used to work with that and since I have lots of projects ...

I'm building an hybrid app using ionic. Inside this app I decided to have Require.js as a dependency loader (maybe not the best choice but I'm used to work with that and since I have lots of projects ...

  1. warning tried to load angular twice