Jump to content
ELFORUM - Forumul electronistilor

Vectori Javascript


AlexSD

Recommended Posts

Salut, fiind o rubrică și de WEB, pun și eu o intrebare legată de JS.

A lucrat cineva cu vectori în Javascript? Am o problemă, în care trebuie să verific dacă elementele unui vector sunt în ordine crescătoare, algoritmul îl știu, dar nu știu cum pot citi de exemplu vector[i+1].

 

Codul este acesta:

 

<!DOCTYPE html>
<html>
<body>
<script>

var N = Number(prompt("N: "));
var vector = new Array(N);
var ok=1;//presupunem ca sunt in ordine crescatoare

for(var i=0; i<N; i++){
vector=prompt("N valori numerice:");
if(vector[i]>vector[i+1])ok=0;}

if(ok==1)
	alert("Numerele din vector sunt in ordine crescatoare");
else
	alert("Numerele din vector nu sunt in ordine crescatoare");
</script>
</body>
</html>

Mulțumesc anticipat.

Link to comment
  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

  • AlexSD

    3

  • modoran

    2

Popular Days

Top Posters In This Topic

Codul asta merge, nu am folosit JS pana acum, se poate scrie mai elegant de atat

<!DOCTYPE html>
<html>
<body>
<script>

var N = Number(prompt("N: "));
var vector = new Array();
var ok = 1;//presupunem ca sunt in ordine crescatoare

for(var i=0; i < N; i++){
vector.push ( prompt("N valori numerice:"));
   //if(vector[i] > vector[i+1]) ok = 0;

}

for(var i = 0; i < N; i++){ 
  if(vector[i] > vector[i+1]) {
  ok = 0;
  }
}


if(ok==1) {
	alert("Numerele din vector sunt in ordine crescatoare");
	}
else
{
	alert("Numerele din vector nu sunt in ordine crescatoare");
	}
</script>
</body>
</html>

 

Link to comment
Acum 18 ore, modoran a spus:

Codul asta merge, nu am folosit JS pana acum, se poate scrie mai elegant de atat


<!DOCTYPE html>
<html>
<body>
<script>

var N = Number(prompt("N: "));
var vector = new Array();
var ok = 1;//presupunem ca sunt in ordine crescatoare

for(var i=0; i < N; i++){
vector.push ( prompt("N valori numerice:"));
   //if(vector[i] > vector[i+1]) ok = 0;

}

for(var i = 0; i < N; i++){ 
  if(vector[i] > vector[i+1]) {
  ok = 0;
  }
}


if(ok==1) {
	alert("Numerele din vector sunt in ordine crescatoare");
	}
else
{
	alert("Numerele din vector nu sunt in ordine crescatoare");
	}
</script>
</body>
</html>

 

Mersi frumos, merge, încercasem și eu metoda aceasta, dar știi care a fost problema, și chiar nu știu de ce.. încearcă N=3, apoi numerele 10, 20, 5 și îți va zice că sunt în ordine crescătoare, aici m-am blocat.. La un număr din 2 cifre, nu mai funcționează codul.

Edited by AlexSD
Link to comment

Da, se pare ca prompt asta nu stie automat sa transforme un string in numar :

 

<!DOCTYPE html>
<html>
<body>
    <script>

        var N = Number(prompt("N: "));
        var vector = new Array();
        var ok = 1;  //presupunem ca sunt in ordine crescatoare

        for (var i = 0; i < N; i++) {
            vector.push(Number(prompt("N valori numerice:")));
        }


        for (var i = 0; i < N; i++) {
            if (vector[i] > vector[i + 1]) {
                ok = 0;
            }
        }

        if (ok == 1) {
            alert("Numerele din vector sunt in ordine crescatoare");
        }
        else {
            alert("Numerele din vector nu sunt in ordine crescatoare");
        }
    </script>
</body>
</html>

 

Link to comment
Acum 4 ore, modoran a spus:

Da, se pare ca prompt asta nu stie automat sa transforme un string in numar :

 


<!DOCTYPE html>
<html>
<body>
    <script>

        var N = Number(prompt("N: "));
        var vector = new Array();
        var ok = 1;  //presupunem ca sunt in ordine crescatoare

        for (var i = 0; i < N; i++) {
            vector.push(Number(prompt("N valori numerice:")));
        }


        for (var i = 0; i < N; i++) {
            if (vector[i] > vector[i + 1]) {
                ok = 0;
            }
        }

        if (ok == 1) {
            alert("Numerele din vector sunt in ordine crescatoare");
        }
        else {
            alert("Numerele din vector nu sunt in ordine crescatoare");
        }
    </script>
</body>
</html>

 

Este bine, mersi frumos. :aplauze

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines