Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add javascript implementation for rp
- Loading branch information
1 parent
e4de4a2
commit a2ab8e5
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Russian_Peasant/Javascript/ZoranPandovski/rusian_peasant.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const russian_peasant = function(num1,num2){ | ||
let x = num1, | ||
y = num2, | ||
sum = 0; | ||
while(x > 0){ | ||
if(x % 2 == 1){ | ||
sum+=y; | ||
} | ||
x = x >> 1; | ||
y = y << 1; | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
//test | ||
|
||
const assert = function(condition, message) { | ||
if (!condition) { | ||
message = message || "Assertion failed"; | ||
if (typeof Error !== "undefined") { | ||
throw new Error(message); | ||
} | ||
throw message; // Fallback | ||
} | ||
} | ||
|
||
const num1 = 12; | ||
const num2 = 10; | ||
const result = russian_peasant(num1,num2) | ||
assert(result === 120); |