Created
August 11, 2022 13:13
-
-
Save Hallmanac/a3e5afa036497aadb3da79aff520b566 to your computer and use it in GitHub Desktop.
A sample function showing how to multiply two numbers in Javascript. Used as a reference when teaching someone how to code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function multiplyTwoNumbers(num: number, multiplier: number) { | |
let result = 0; | |
for (let index = 0; index < multiplier; index++) { | |
result += num; | |
} | |
return result; | |
} | |
function multiplyNumbers(...nums: number[]) { | |
let result = 0; | |
for (let i = 1; i < nums.length; i++) { | |
const baseNumber = i === 1 ? nums[0] : result; | |
const multiplier = nums[i]; | |
let loopResult = 0; | |
for (let i3 = 0; i3 < multiplier; i3++) { | |
loopResult += baseNumber; | |
} | |
result = loopResult; | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function multiplyTwoNumbers(num, multiplier) { | |
let result = 0; | |
for (let index = 0; index < multiplier; index++) { | |
result += num; | |
} | |
return result; | |
} | |
function multiplyNumbers(...nums) { | |
let result = 0; | |
for (let i = 1; i < nums.length; i++) { | |
const baseNumber = i === 1 ? nums[0] : result; | |
const multiplier = nums[i]; | |
let loopResult = 0; | |
for (let i3 = 0; i3 < multiplier; i3++) { | |
loopResult += baseNumber; | |
} | |
result = loopResult; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment