Multiply all the values in array with a specific number using map() - Javascript Array

Javascript examples for Array:map

Description

Multiply all the values in array with a specific number using map()

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Multiply with: <input type="number" id="multiplyWith" value="10"></p>
<button onclick="myFunction()">Test</button>

<p>New array: <span id="demo"></span></p>

<script>
var numbers = [65, 44, 12, 4];/*from   w  w  w  .j a  va2 s.c  o  m*/

function multiplyArrayElement(num) {
    return num * document.getElementById("multiplyWith").value;
}

function myFunction() {
    document.getElementById("demo").innerHTML = numbers.map(multiplyArrayElement);
}
</script>

</body>
</html>

Related Tutorials