JS function looping double as expected - javascript

This is Javascript code for some quiz i am making.
In my case table.length = 2, and function is repeating 2 times with parameter for first table and second.
But why command shows 4 times in console?
console.log("Hello");
.
function start(){
var brojac =0;
var table = document.getElementsByTagName("table");
for (i =0; i<table.length ; i++){
jednoPitanje(i);
brojac += parseInt(jednoPitanje(i))
}
console.log("Sakupili ste ukupno " + brojac + " bodova");
}
function jednoPitanje(x) {
var odgovori ="";
var table = document.getElementsByTagName("table");
var tableN = table[x];
var input = tableN.getElementsByTagName("input")
var brojInputa = tableN.getElementsByTagName("input").length;
//Uzima bodove,kategoriju i index tocnih odgovora
var bodovi =tableN.classList[2];
var kategorija =tableN.classList[1];
var tocni = tableN.classList[0];
console.log("Hello");
//Iteracija kroz sve checkboxsove u tablici
for (j =0; j<brojInputa ; j++){
if(input[j].checked==true){
odgovori += tableN.getElementsByTagName("input")[j].value;
}
}
if(odgovori == tocni){
}
else{bodovi = 0;}
return bodovi;
}

You are calling console.log("Hello"); in the function jednoPitanje(). You call this function twice inside your loop:
jednoPitanje(i); // <-- this cause console.log() to run
brojac += parseInt(jednoPitanje(i)) // <-- this also causes the console.log()
and since your loop runs twice it prints four times.
It's not immediately clear if you need that function to run twice, but if you don't, you can just remove the first call:
for (i =0; i<table.length ; i++){
brojac += parseInt(jednoPitanje(i))
}
or if you prefer the extra clarity:
for (i =0; i<table.length ; i++){
var bodovi = jednoPitanje(i);
brojac += parseInt(bodovi)
}

Related

Why is this removeChild not working?

function usePotion(){
for (var i = 0; i < inventoryItemNumber.length; i++){
if (inventoryItemNumber [count] == 5){
player.hp += 5;
inventoryItemNumber.splice(inventoryItemNumber.indexOf(5),1);
inventory.pop()
document.getElementById("HP").innerHTML = "HP: " + player.hp;
let inventoryList = document.getElementById("inventory");
inventoryList.removeChild (inventoryList.childNodes[count]);
return;
}
count++
}
return;
}
When I use my button to call the function everything except the removeChild command works, but if I go into console and copy/paste my 2 lines of code and execute it works fine.
There's a lot more code obviously and my count variable is a global variable = 0;

how to decode string in javascript?

I am trying to decode my string using JavaScript. Here is my code on JSBin.
decordMessage('oppeeennnn','1234');
function decordMessage(m,k) {
var msg = m.split('');
var keysplit = k.split('');
var str ='';
var j =0
for (var i=0;i<msg.length;){
str += msg[i];
if(j < keysplit.length -2 &&i < keysplit.length && keysplit[j]){
i = i + parseInt(keysplit[j]);
j++;
}
console.log(i +"i")
console.log(str);
}
console.log("after");
console.log(str);
}
I make a function in which message and key is passed.
Expected output :: open
Actually string charters are repeated in input message (encrypted message) using key. So I need to decode the message.
You forgot to put a break in the else condition, that's why it was looping infinitely till it ran out of memory. Run it in a browser and the tab will crash:
decordMessage('oppeeennnn','1234');
function decordMessage(m,k) {
var msg = m.split('');
var keysplit = k.split('');
var str ='';
var j =0
for (var i=0;i<msg.length;){
str += msg[i];
if(j < keysplit.length &&i < keysplit.length && keysplit[j]){
i = i + parseInt(keysplit[j]);
j++;
}
else
break;
}
console.log("after");
console.log(str); // prints open
}
By the way, a better way to write the loop would be:
function decordMessage(m,k) {
var msg = m.split('');
var keysplit = k.split('');
var str = '';
var j = 0, i = 0;
while (j < keysplit.length
&& i < msg.length) {
str += msg[i];
i += parseInt(keysplit[j]);
j++;
}
console.log(str)
}
This may helps you.
decordMessage('oppeeennnn', '1234');
function decordMessage(m, k) {
var arr = m.split("");
uniqueArray = arr.filter(function(item, pos) {
return arr.indexOf(item) == pos;
});
console.log(uniqueArray.join(""));
}
Assuming encryption logic goes as 123456....
Sample here

Why does the program exit main loop?

I have the following code that transmits the essence of the problem:
var body = "";
for(var i=0; i<=5000; i++) {
body += "if(str==='value" + i + "') 1==1;\n";
}
body += "return str;";
var f1 = new Function("str", body);
var f2 = new Function("str", body);
console.log(f1('test1'));
console.log(f2('test2'));
// main loop
for(var i=0; i<100000; i++) {
f1("string");
f2("string");
console.log(i);
}
console.log("fin!");
Why the main loop cycle does not work out until the end, and the program exits (thus no error information is not displayed)?
PS line "fin!" will not be displayed.
My nodejs version is 5.0.0
My actual output:
test1
test2
1
2
3
.
.
~1971
Try this:
var i = 0;
for(i=0; i < 5001; i++) {
body += "if(str==='value" + i + "') 1==1;\n";
}
But i think the functions are bugged...

Javascript News Ticker

I'm using this code to fade in and out different messages.
It works ok. The only problem is that it delays the first loop. And I have to wait before the first message shows up. I tried to fix it but with no luck!
var loops = 0;
function t() {
var news = new Array("Text01", "Text02");
var l = news.length;
var fade = 300;
var delay = 6000;
var process = delay+fade+fade;
var n = 0;
var f = '';
for(i=0; i<l; i++){
f += "<span class='ticker' id='str"+i+"'>"+news[i]+"</span> ";
$("#newsticker").html(f);
}
for(i=0; i<l; i++){
n++;
var offset = i*process;
$("#str"+i).delay(offset).fadeIn(fade).delay(delay).fadeOut(fade);
}
var loop_process = n*process;
if(loops==0){
setTimeout("t()", 0);
}
setTimeout("t()", loop_process);
loops++;
}
t();
HTML
<span id="newsticker"></span>

Javascript while and do/while loops

I have this code who works properly with for loop, how to made while and do/while loop to work properly on same way when i click on button?
<body>
<div>
<button onclick = "fun()">Click
</button>
<p id = "dr">
</p>
<script>
function fun() {
var str = "";
for (var i = 0; i < 11; i++) {
str = str + i + "<br/>";
document.getElementById("dr").innerHTML = str;
}
}
</script>
</div>
</body>
This sounds a bit like HW, so I won't give you the entire code.
However, you have this code in your for loop
for (var i=0; I<11; i++)
}
In that, I is never defined, so you should probably change it to i.
For changing it to a do{}..while() loop, remember that every for loop of the form for(a;b;c){d;} can be expanded to the while loop
a;
while(b){
d;
c;
}
and therefore to the do..while loop of
a;
do{
d;
c;
}while(b);
while:
function fun()
{
var str ="";
while( i < 11 )
{
str=str + i +"<br/>";
document.getElementById("dr").innerHTML =srt;
i++;
}
}
do/while:
function fun()
{
var str ="";
do
{
str=str + i +"<br/>";
document.getElementById("dr").innerHTML =srt;
i++;
}while( i < 11 );
}

Categories