im trying to obfuscate a string in vb.net and deobfuscate in javascript.
For i = 0 To Len(sData) / 4
For j = 1 To 4
ConvertData2 = ConvertData2 & Mid$(sData, (4 * i) + 5 - j, 1)
Next j
Next i
the above said code works well in vb.net. I need the equivalent code in javascript
i tried the below one but not working.
for (i = 0; i<(sData.length)/4; i++)
{
for (j = 1;j<4;j++)
s=s+sData.substr((4 * i) + 5 - j,1)
}
anyone can suggest where i made mistake..
Well, for one thing, Javascript string indices start at 0, not 1. And your for (j=1; j<4; j++) loop will only count from 1 to 3 anyway; you want to go either from 0 to 3 (j=0 and j<4) or 1 to 4 (j=1 and j<=4).
A direct translation of the VB looks like this:
convertData2 = ''
for (var i=0; i < sData.length / 4; ++i) {
for (var j=1; j <= 4; ++j) {
convertData2 += sData.substr(4 * i + 4 - j, 1)
}
}
the first glaring difference is that in vb.net you use integer division while in javascript you are not...
use
for(var i = 0, len = Math.floor(sData.length / 4); i<=len; i++)
The second is that Mid starts counting from 1 while subst starts from 0
so use
s = s + sData.substr((4 * i) + 5 - j -1,1)
or simplified
s = s + sData.substr((4 * i) + 4 - j,1)
Lastly the loops, when using < do not use the final number ... while the from.. to uses the last number as well (so use <=)
so alltogether
var s = ''; // define s (if not yet defined) other wise use s = '' to make sure it starts empty..
for(var i = 0, len = Math.floor(sData.length / 4); i <= len; i++) {
for (var j = 1 ; j <= 4; j++) {
s = s + sData.substr( (4 * i) + 4 - j,1);
}
}
Related
I am trying to implement the while and do while loop without using an actual array to print an list of number. Here is my code and the output I want.
My code
console.log("While Loop")
let j = 0;
while(j < 5){
j += 1;
console.log (j);
}
console.log("Do While Loop")
var i = 0; //set varible i = 0
do {
i += 1; //return i + 1 to i
console.log (i);
}
while (i < 5); //where i suppose to less than 5
Actual Output I want:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
There is two things here:
You need an inner loop to create the countdown of numbers.
If you are only using JavaScript, you need to form a string with your output and then use console.log() to print. This is because console.log() will append a newline.
The example below will print the first half of the triangle.
console.log("While Loop")
let j = 1;
while (j <= 5) {
let k = 1;
let str = "";
while (k <= j) {
str += k + " ";
k++;
}
j += 1;
console.log(str);
}
If you are using Node.js you can use process.stdout.write to print to the same line and then explicitly write the newline character when needed.
console.log("While Loop")
let j = 1;
while (j <= 5) {
let k = 1;
while (k <= j) {
process.stdout.write(k + " ");
k++;
}
j += 1;
process.stdout.write("\n");
}
I'm trying to make a loop where one value goes up while the second goes down.. I cant figure it out. As far as I can see checkNumber counts down correctly, and x and i are incorrect
I know i'm making a silly mistake somewhere but I'm brand new to coding
var checkNumber = 5;
for (var x = 0; x < 5; x++) {
for (var i = 0; i < checkNumber; i++) {
console.log(checkNumber);
checkNumber = checkNumber - 1;
console.log("x",x,"i",i);
}
}
Just use a single loop and take the difference of the max value and the actual value (minus one, because of the zero based nature) for the second value.
var value = 5,
i;
for (i = 0; i < value; i++) {
console.log(i, value - i - 1);
}
I'm assuming you're trying to do this:
var checkNumber = 5;
for (var x = 0; x < checkNumber; x++) {
for (var i = checkNumber - 1; i >= 0; i--) {
console.log(checkNumber);
console.log("x", x, "i", i);
}
}
This will start i at 4 (minus one to avoid index issues if that's what you're looking for, otherwise remove the -1) and go down to 0.
The first loop will count up until 4.
The trick is to use i-- and set i to something higher, then stop the loop with the second condition in the for.
Does that make sense?
This will make i start at 0 and j start at 4. While i goes up to 4, j will go down to 0.
var checkNumber = 5;
for(var i = 0, j = checkNumber - 1; i < checkNumber; i++, j--){
console.log(i + " " + j);
}
The idea behind the following code is to test if any number between 0 and 13 + any other number equals 13. If one does both numbers should be saved to a different array but on the same index. So i should have all possible combinations to reach 13 in 2 arrays. But when i run my code I only get 2 combinations which are 0+13 and 13+0. Here is the code:
var number1 = [];
var number2 = [];
var index = 0;
var i = 0;
var j = 0;
//Tests if i + j (from the loop) add up to 13
var test = function(i, j) {
if (i + j === 13) {
number1[index] = i;
number2[index] = j;
index =+ 1;
}
}
//1st loop generates i from 0 to 13 in 0.5 step.
for (i = 0; i < 13.5; i += 0.5) {
//same for j, this number should test with i every loop
for (j = 0; j < 13.5; j += 0.5) {
test(i, j);
}
}
//outputs the 2 arrays, the matching numbers should be stored in
for (i = 0; i < number1.length; i++) {
console.log(number1[i]);
console.log(number2[i]);
}
Change index =+ 1 to index += 1
Then index =+ 1 sets the index to 1 it does not increment it by 1 (as you want)
See Expressions and operators: Assignment operators MDN
I have implemented the string search algorithm using the naive method to count the number of times a substring occurs in a string. I did the implementation in javascript and python.
Algorithm (From Topcoder):
function brute_force(text[], pattern[])
{
// let n be the size of the text and m the size of the
// pattern
count = 0
for(i = 0; i < n; i++) {
for(j = 0; j < m && i + j < n; j++)
if(text[i + j] != pattern[j]) break;
// mismatch found, break the inner loop
if(j == m) // match found
count+=1
return count
}
}
Javascript Implementation:
a = "Rainbow";
b = "Rain";
count = 0;
function findSubStr(Str, SubStr){
for (i = 0; i<a.length; i++){
//document.write(i, '<br/>');
for (j = 0; j < b.length; j++)
//document.write('i = ',i, '<br/>');
//document.write(j, '<br/>');
if(a[i + j] != b[j]) break;
document.write('j = ', j, '<br/>')
//document.write('i = ',i, '<br/>');
if (j == b.length)
count+=1;
}
return count;
}
document.write("Count is ",findSubStr(a,b), '<br/>');
Python Implementation:
a = "Rainbow"
b = "Rain"
def SubStrInStr(Str, SubStr):
count = 0
for i in range(len(Str)):
for j in range(len(SubStr)):
print j
if (a[i + j] != b[j]):
break
if (j+1 == len(SubStr)):
count+=1
return count
print(SubStrInStr(a, b))
Now my question is for the line that implements if (j == b.length): It works well in javascript but for python I need to add 1 to the value of j or deduct 1 from the length of b. I don't know why this is happening.
for x in range(4)
Unlike Javascript in Python for loop is used for every element in the list. Last value x will take is the last element of the list [0, 1, 2, 3] which is 3.
for(x = 0; x < 4; x++)
In Javascript x will take value for 4 and the loop will end because x < 4 condition no longer can be applied. Last value x will take is 4.
You have this confusion because your code isn't identical. Executing for (j = 0; j < b.length; j++) the final value for j will be b.length (in case that b is a substring of a), but for Python, things are a little bit different. Running range(len("1234")) will result in [0, 1, 2, 3], so your for is more like a foreach, j storing the last value from the array and this is the reason why you have to add one. I hope that I was clear enough. If not, please ask for details.
I don't know about javascript , But I have implemented naive search in Python with all the cases with easiest way.
Take a glance on it as below.
It will return no of time pattern got found.
def naive_pattern_search(data,search):
n = len(data) #Finding length of data
m = len(search) #Finding length of pattern to be searched.
i = 0
count = c = 0 #Taking for counting pattern if exixts.
for j in range(m-1):#Loop continue till length of pattern to be Search.
while i <= (n-1):#Data loop
#if searched patten length reached highest index at that time again initilize with 0.
if j > (m-1):
j = 0
#Data and search have same element then both Index increment by 1.
if data[i]==search[j]:
#print(f"\n{ data[i] } { search[j] }")
#print(f"i : {i} {data[i]} j : {j} {search[j]}")
i+=1
j+=1
count+=1
#If one pattern compared and found Successfully then Its Counter for pattern.
if count== (m-1):
c = c + 1
#Initilise pattern again with 0 for searching with next element in data.
else:
j = 0 #Direct move to 0th index.
i+=1
count=0 #If data not found as per pattern continuously then it will start counting from 0 again.
#Searched pattern occurs more then 0 then its Simply means that pattern found.
if c > 0:
return c;
else:
return -1;
Input : abcabcabcabcabc
Output: Pattern Found : 5 Times
I find your python implementation has some problem. If you set the b = "raiy", the function will incorrectly return 1. You may misunderstand the edge condition.
Those two condition statements should be in the same level.
a = "Rainbow"
b = "Rain"
def SubStrInStr(Str, SubStr):
count = 0
for i in range(len(Str)):
for j in range(len(SubStr)):
# print (j)
if (a[i + j] != b[j]):
break
if (j+1 == len(SubStr)):
count+=1
return count
print(SubStrInStr(a, b))here
So, I'm new to javascript, and - after completing the JS unit on codecademy, I'm working through Project Euler to get a better feel for the language. The problem is that I'm stuck on the second challenge. I feel pretty dumb at this point. The problem is to find the sum of all the even fibonacci numbers that are less than four million.
var fib = [1,2];
var stack = [];
for (i = 2; i < 4000000; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
That part works. I'm using JSFiddle, and it will print out the fibonacci sequence up to four million. The problem is the next part:
for (j = 0; j < fib.length; j++) {
if (fib[i] % 2 === 0) {
stack[j] = fib[i];
}
}
I've tried that bit both inside and outside the for loop, and I can't figure this out. I have this feeling that I'm missing something obvious. Any help would be appreciated. Thanks :D
EDIT: I figured it out. Thank all of you! Here's what I did:
var total = 0;
var fib = [1, 2];
//In my first attempt, I made a set of the first 4,000,000
//fibonacci numbers. I just left the "4000000" there
//arbitrarily.
for (i = 2; i < 4000000; i++) {
//This makes sure that I don't go over 4000000 in the array.
if (fib[i - 1] < 4000000) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
for (j = 0; j < fib.length; j++) {
if (fib[j] % 2 === 0) {
total += fib[j];
}
}
alert(total);
And it printed out the correct answer! Woot.
Replace this
for (j = 0; j < fib.length; j++) {
if (fib[i] % 2 === 0) {
stack[j] = fib[i];
}
}
with this
for (j = 0; j < fib.length; j++) {
if (fib[j] % 2 === 0) {
stack[j] = fib[i];
}
}
And also, the problem says to only find fibonacci numbers below 4_000_000. There is no need to create 4 million fibonacci numbers. That would take forever. Try something smaller like 70.
I see a couple of issues with this.
You say that you want to find the sum of all even fibonacci numbers? I don't get the point of putting the even results into another array and then dealing with them. Try something like this:
var fib = [1,2];
var result = 0;
for (i = 2; i < 4000000; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
for (j = 0; j < fib.length; j++) {
if(fib[j] % 2 == 0){
result = result + fib[j];
}
}
Secondly, you phrase it as "all even fibonacci numbers less than 4 million". Are you sure that they want the first 4 million fibonacci numbers, or the fibonacci numbers that are under 4 million?