Value on index place to number value?

Continuing the discussion from Alert() does not work with input:

Hello!
Please, how can I convert a sign on certain index place in the array t oa number value?

F.e I have an array:

array[0]=5;
array[1]=8;
...

so all these signs ( 5 and 8) are STRINGS (CHARACTERS)
hOW DO i TRUN THEM TO NUMBERS?‘’

I tried like:
var v=parseInt('c') but got Nan as answer…:think
MMANY THANKS!!!

Hi,

Use Number - something like this:

var arr = ["0", "5"]
var new_arr = arr.map(function(item) {
  return Number(item);
});

console.log(arr, new_arr);
// ["0", "5"]
// [0, 5]

Hello!
Please, I have input value which I wanbted to “slice” to array index places and then turn the value on each index to a number, like…

<!DOCTYPE html>
<html>

<head>
<script>
function m(){


var a=document.getElementById("demo").value;
var array = a.split('');


var a2=array.length;
alert(a2);
alert(array[3]);
var c=document.getElementsByName("marc")[0].value;


alert(c);

var m1=parseInt('c',10);
alert(m1);
for (x=0;x<c;x=x+2){
                    if (a2[x]==c){alert("Letter is there!")}
else
{alert("This letter is not there.")}


                    }


}

</script>
</head>


<body>
<form>



<input type="text" id="demo" value=""/><br>

<p>Write a letter</>
<input type="text" name="marc" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>


</body>
</html>

I amthe beginner with JS, will that be too complicatd _-
Manz thanks!!

I converted c to number value, but I cant convert a2 into number value :think…


<!DOCTYPE html>
<html>

<head>
<script>
function m(){


var a=document.getElementById("demo").value;
var array = a.split('');


var a2=array.length;
alert(a2);
alert(array[3]);
var c=document.getElementsByName("marc")[0].value;


alert(c);

var m1=parseInt(c,10);

alert(m1);
m2=m1+3;
alert(m2);
for (x=0;x<c;x=x+2){
                    if (parseInt(a2[x])==c){alert("Letter is there!")}
else
{alert("This letter is not there.")}


                    }


}

</script>
</head>


<body>
<form>



<input type="text" id="demo" value=""/><br>

<p>Write a letter</>
<input type="text" name="marc" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>


</body>

</html>

Hi,

I’m afraid I don’t quite understand what you are trying to do.
You have two inputs (demo and marc).
What input should they receive?
What output should be produced when one clicks on the button?
Please give us a concrete example.

Hello!
I shoul dactually input numbers but then I have to sumarise fist third, fifth…inother words, if I imagine numbers input as an numbers array then I shoudl get in output sum of all numbers which index is odd…

f.e.:
i imaput:
234567
and I have to get as output sum of 2+4+6
so I get in output: 12 in this case

…???

So if you enter “123456789”, the output should be 1+3+5+7+9=25
Is that correct?

yes. but the lenght of the queue of numbers is not determinated in advance-.

can be
123
1+3

or

23456
2+4+6 etc

I thought only string can be “sliced” to pieces (index) or???That is why i started that way…

You can do it like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Sum elements with odd index</title>
  </head>
  <body>
    <input type="text" id ="numbers" />
    <button class="calculate">Calculate</button>

    <script>
      function calculateSumOfOddIndex(){
        var sum = numbers.value.split("").map(function(el){
          return Number(el);
        }).reduce(function(tot, num, i){
          return (i%2 === 0)? tot + num : tot;
        });
        alert(sum);
      }

      var calculate = document.querySelector("button.calculate"),
          numbers = document.getElementById("numbers");

      calculate.addEventListener("click", calculateSumOfOddIndex, false);
    </script>
  </body>
</html>

Be warned - this contains no error checking.

Thank U, but I am a very beginner, is there any easier way to doit?

is map() javascript method? can’t find it in w3school methods for javascript?

That’s ok - we all have to begin somewhere.
Let’s take it from the top.

You have a basic document, with a text input and a button.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title></title>
  </head>
  <body>
    <input type="text" id ="myInput" />
    <button>Submit</button>

    <script>
      // JavaScript goes here
    </script>
  </body>
</html>

You put your JavaScript at the bottom of the page, so that it is only evaluated after any of the elements it might reference (the input and the button in this case) are present on the page.

Now, as was pointed out in your other thread, you should use unobtrusive event handlers, i.e.

element.addEventListener(event, function, useCapture);

not:

<element onclcik="doSomething"></element>

So, this would be how you would have a user input something and alert it back at them:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Alert input</title>
  </head>
  <body>
    <input type="text" id ="myInput" />
    <button>Submit</button>

    <script>
      function alertInput(){
        alert("You entered: " + myInput.value);
      }

      var button = document.querySelector("button"),
          myInput = document.getElementById("myInput");

      button.addEventListener("click", alertInput, false);
    </script>
  </body>
</html>

Any questions, or is that ok so far?

Would it help to extract out the functions, so that the way it works can be more easily examined.

The flow is: numbers => split => odd filter => add together

function convertToNumbers(txt) {
    console.log(txt);
    return Number(txt);
}

function isEvenIndex(value, index) {
    return index % 2 === 0;
}

function add(num1, num2) {
    return num1 + num2;
}

function calculateSumOfOddIndex() {
    var splitString = numbers.value.split(""),
        numberValues = splitString.map(convertToNumbers),
        oddNumbers = numberValues.filter(isEvenIndex),
        sum = oddNumbers.reduce(add);
    alert(sum);
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.