-3

I am learning to code and I've been working on this one item for over an hour. The code for the string length property is not correct.

// I have tried:

length.length;
num.toString(); //"6"
length.length; //6

//////The exercise is below

function exerciseThree(str){
  // In this exercise, you will be given a variable, it will be called: str
  // On the next line create a variable called 'length' and using the length property assign the new variable to the length of str
  var length = 'length';
  length.length;
  // Please write your answer in the line above.
  return length;
Sabrina73
  • 1
  • 1
  • I'm confused about where `num` came from and what the problem is. The length of the string `length` should be 6, so what's the problem? – watzon Jul 24 '19 at 22:02
  • 2
    @Sabrina73 stackoverflow is not meant to help you solve your assignments. see [How to ask](https://stackoverflow.com/help/how-to-ask). The code comments in your exercise tells you what to do. It has a variable named `length` and receive a string parameter named `str`. The exercise ask you to have the lengh of the `str` variable stored in to the `length` variable. – Léa Gris Jul 24 '19 at 22:04
  • @LéaGris - What's the best place for people who are beginners learning to code? :) Because I will need help from time to time. Thanks! – Sabrina73 Jul 24 '19 at 22:19
  • 2
    Probably the person that gave you this exercise ... – Jonas Wilms Jul 24 '19 at 22:22

3 Answers3

0

According to the assignment you have to take the .length property of str, currently you create a string "length".

The length.length in the following line won't help you, as that takes the length of the string "length" stored in the length variable which is 6, which is probably not the length of the passed str, also you don't do anything with that value.

 function exerciseThree(str) {
   var length = str./*some magic here which i'll leave up to you :)*/;
   return length;
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

As the instructions in comments ask you to assign the value of variable str to a new variable length which you have to create in the function body and return it. So basically str is the variable which will hold the string value whenever the function exerciseThree is called with a value passed with a (parameter)

Here's the working example of the code:

function exerciseThree(str){
    // In this exercise, you will be given a variable, it will be called: str
    // On the next line create a variable called 'length' and using the length property assign the new variable to the length of str
   var length = str.length; // ** storing the "length" of "str" in a "length" variable
    return length;
}   // Please write your answer in the line above

var name="Alex B"; // string to pass to the function
var result = exerciseThree(name); // calling the function and storing "return" value to variable "result"
console.log(result); // printing the value of result on the screen.
0000
  • 11
  • 4
-1

I do not really know what to teach this exercise.

In the real world, your function would look like this:

function exerciseThree(str) {
    return str.length;
}

excerciseThree("Hokuspokus"); // 10

because:

  1. using the variable only for this purpose is a waste of resources

  2. naming variables in the same way as names of language elements is asking for trouble

Slawomir Dziuba
  • 1,265
  • 1
  • 6
  • 13
  • (1) disagree, it's a waste of time to consider the memory usage of every variable, write code that is good to read, only consider memory / performance if you have a problem with one of them. – Jonas Wilms Jul 24 '19 at 22:11
  • @Jonas Wilms 'resources' is not just RAM, the resource is also the next name in the pool of names in the code. Yes, writing styles can be different. This is not a dogma. – Slawomir Dziuba Jul 24 '19 at 22:15