Jump to content
GraphLog

Problema cu un cod JS

Recommended Posts

Posted

Salutari, m-am hotarat si eu sa invat JavaScript si am citit niste chestii pe net.

Se da urmatoarea problema :

Printing your own name ten times.

Eu, stiu sa fac cu numere, de exemplu:

var counter="1";
for (counter = 1; counter <= 10; counter = counter + 1) {
console.log(counter);
}

Dar, daca incerc cu un string, cum e "Alecs":

var counter="Alecs";
for (counter = "Alecs"; counter <= "Alecs" * 10; counter = counter + 1) {
console.log(counter);
}

Imi returneaza:

Alecs

Cum as putea face sa imi arate numele de 10 ori?

Multumesc anticipat :).

Posted

Vrei sa iti arate numele de 10 ori?

Desi nu am lucrat mult cu JS sper ca este corect.

var counter=1;
for (counter = 1; counter <= 10; counter = counter + 1) {
console.log("Alecs");
}

sau

var counter=1;
var nume="Alecs";
for (counter = 1; counter <= 10; counter = counter + 1) {
console.log(nume);
}

Variabila counter trebuie sa fie un numar care ajunge de la 1 la 10 astfel incat de fiecare data cand creste sa se intample ceva adica sa iti scrie numele tau.

Posted

i++ == i = i + 1 == i += 1 == ++i

Atentie, exista diferente intre i++ si ++i (post-incrementare, pre-incrementare). Exemplu:

Pre-incrementare;


var i = 0;
var pre = ++i;
alert(pre); // 1
alert(i); // 1

Post-incrementare;


var i = 0;
var post = i++;
alert(post); // 0
alert(i); // 1

Posted

@totti93 Daca tot esti pus pe ajutat, mai am o problema :))

Deci, cerintele sunt:

Write a for loop that prints out numbers 100 through 150.

The test will fail, but you can check the console to make sure you did it right.

Now, inside the block, change your code so you are calculating the square of i and store it in the variable square. Use multiplication to calculate the square, not the ^ character.

Change the console.log statement so it outputs the square instead of i.

Save the world.

Eu am facut codul asta:

var square=(Math.sqrt(i));
for (var i=100; i<=150; i++) {
console.log(square);
}

Dar imi arata:

Oops, try again.

Make sure you're 1) starting the loop on 100 and 2) squaring it.

Ai vreo idee?

Multumesc mult pt indrumari :)

Posted

Nu ai cum sa calculezi patratul unui numar pe care il declari mai tarziu. Si Math.sqrt() calculeaza radacina patrata, adica scoate de sub radical, iar acolo zice sa calculezi patratul, adica la puterea a doua si asta folosind inmultirea.


for (i = 100; i<=150; i++)
{
var square = i*i;
console.log(square);
}

Posted

Daca nu va enervez prea tare, am mai gasit o problema .

Cerintele sunt:

Define the calculateTotalCosts function with three parameters - salary, numWorkers, city.

Declare fixedCosts and assign it a value of 5000. Declare variableCosts and assign it the value of salary * numWorkers.

If the startup is in NYC, the rent is 30000.

If the startup is in "BEJ" (Beijing), the rent is 25000.

If the startup is based anywhere else, the rent is 10000.

Use the function to calculate the costs for a startup with salaries of 40000, 3 workers, and based in any city other than "NYC" or "BEJ". This means taking in those numbers as parameters for the calculation, and using an if statement to check which city it is in.

Acuma, am scris eu ceva ceva, dar imi da "Oops!Try again."

Codul meu e:

var TotalCosts = function(salary, numWorkers, city) {
var fixedCosts = 5000;
var variableCosts = salary * numWorkers;
if (city === "NYC") {
return fixedCosts + variableCosts + 30000;
}
else if (city === "BEJ") {
return fixedCosts + variableCosts + 25000;
}
else {
return fixedCosts + variableCosts + 10000;
}
};
TotalCosts(40000,3,"BUC");

Multumesc ^^.

Posted
Salutari, m-am hotarat si eu sa invat JavaScript si am citit niste chestii pe net.

cine înva?? javascript aici, ?i ar trebui s? rezolve problemele pe baza a ce a înv??at, noi sau tu? :|

btw:

Define the calculateTotalCosts

tu ai f?cut:

var TotalCosts = function(salary, numWorkers, city) {

Posted

Da, aia era problema..

Mersi mult.

Pai,eu incerc sa le rezolv, dar tinand cont ca e sectiune de webdesign ce tine si de asta, mai pun niste intrebari.

Nu te obliga nimeni sa-mi citesti threadul sau sa raspunzi, din cate vezi nu am cerut scripturile de la 0, sau "da-mi si mie un script sa fac asa asa", ci doar niste completari / indrumari.

Posted (edited)

Ai inceput cu stangul. Nu poti invata JS cand nu ai o formata o baza in programare in general.

Din ce vad, tu arunci "cuvinte" prin source-code fara sa ai idee cum functioneaza.

Pai avand in vedere ce-ai scris in primu' post, (counter='Alecs'; counter<=..etc..) nu are rost sa treci mai departe pana nu intelegi cum merge treaba.

Tu postezi acilea ca nu intelegi, astia iti da mura-n gura ca n-au chef sa explice...ce castigi?

Edited by AlStar

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...