Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Javascript functions not working in console

So I have this code:

<script>
    function add7(n) {
        let x = n + 7;
        console.log(x);
    }
    function lastLetter(theString) {
        let x = lastIndexOf(theString);
        console.log(x);
    })
</script>
<script>
    function multiply(a, b) {
        let ans = a * b;
        console.log(ans);
    }
</script>
<script>
    function capitalize(word) {
        if (word = String) {
            toLowerCase(word);
            charAt(0).toUpperCase(word);
            console.log(word);
        } else {
            console.log("not a string");
        }
    }
</script>

I write functionName(chosenVariable) in the console and expected to see a clear answer but add7 and lastLetter just returns ReferenceError (function) not defined. and the other 2 get undefined as an answer. I know that I am blind but am I also a bit stupid? I've looked at the code and tried different changes but cant get it to work.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There were a few errors with your code

  • Extra ) at the end of the first script block
  • lastIndexOf is not defined anywhere on your script
  • word = String will just assign the value of the String class to the word variable (and always return true)
  • Strings are immutable, so you can't edit an existing string you can however create a new string based on another, so using word.toLowerCase() on it's own won't do anything, you need to reassign the value

add7(2);
lastLetter("123123");
multiply(2, 3);
capitalize("asd");

function add7(n) {
  let x = n + 7;
  console.log(x);
}

function lastLetter(theString) {
  let x = theString[theString.length - 1];
  console.log(x);
}

function multiply(a, b) {
  let ans = a * b;
  console.log(ans);
}

function capitalize(word) {
  if (typeof word === "string") {
    word = word.toLowerCase();
    word = word[0].toUpperCase() + word.substring(1);
    console.log(word);
  } else {
    console.log("not a string");
  }
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda