how to multiply in javascript code example
Example 1: js multiply string
let myString = 'test';
myString.repeat(3);
Example 2: javascript 1 + "1"
console.log(10+"1");
console.log(10-"1");
Example 3: multiply js
const multiply = (x,y)=>{
if(x==1){ return y;}
else if(x>1){
return y +multiply(x-1,y)
}
else throw "Not Numbers"
}
var result = multiply(4,3);
console.log(result)
Example 4: javascript how to multiply numbers
<!-- Multiplying two numbers in HTML and Javascript -->
<main>
<label for="firstNum">Number 1:</label>
<input type="number" id="firstNum" name="firstNum">
<label for="secondNum">Number 2:</label>
<input type="number" id="secondNum" name="secondNum"></br></br>
<button onclick="multiply()">Multiply</button></br></br>
<label for="result">Result</label>
<input type="number" id="result" name="result"/>
</main>
<script>
function multiply(){
num1 = document.getElementById("firstNum").value;
num2 = document.getElementById("secondNum").value;
result = num1 * num2;
document.getElementById("result").value = result;
}
</script>
Example 5: multiply function javascript
const multiply = (num1, num2) => {
return num1 * num2;
};
let resulMultiply = multiply(4, 5);
console.log(resulMultiply);
Example 6: multiply all numbers in an array javascript
The cause is already known. Here's an alternative - using Array.reduce for your method:
console.log( [1, 2, 3].reduce( (a, b) => a * b ) );
console.log( Array.from( {length: 20} )
.map( (v, i) => i + 1 )
.reduce( (a,b) => a * b )
.toLocaleString());
const arr = [];
if (arr.reduce( (a, b) => a * b, -1 ) === -1) {
console.error(`The given array ${arr} is empty`);
}