Whenever I try to modify the capitalization of the input word, an error always pops up. I attempted to utilize toLowerCase() to no avail

const selectUserSelection = (choice=choice.toLowerCase()) => {
  if (choice === 'rock' || choice === 'paper' || choice === 'scissors') {
    return choice;
  } else {
    console.log('Invalid choice entered!');
  }
};

console.log(selectUserSelection('Scissors'));

//Looks like the function only accepts inputs in lowercase letters for it to work correctly.

Answer №1

function validateUserChoice(input) {
  input = input.toLowerCase();
  
  if (input === 'rock' || input === 'paper' || input === 'scissors') {
    return input;
  } else {
    return 'Invalid choice! Please choose between rock, paper, or scissors.';
  }
};

Answer №2

const setUserSelection = (userInput=userInput.toLowerCase()) => {

Within this line, you are setting the default value for the function argument. This default value will be undefined unless userInput is present within the scope. It does not alter the argument you provide initially.

For example, consider a function like this:

const greeting = (name="stranger") => console.log(`hello, ${name}`)

When called without any arguments, it will output "hello, stranger". If called as greeting("Scrissors"), it will output "hello, Scrissors".

Upon calling the function: setUserSelection('Scissors'), the default argument is replaced by your input, making userInput within the function equal to "Scissors".

To perform the conversion, it should be handled within the function body:

const setUserSelection = (userInput) => {
   userInput=userInput.toLowerCase()
   /* userInput has now been converted to lowercase based on the argument provided */
}

Answer №3

If you want to give it a shot

const getPlayerSelection = (input) => {
  let playerInput = input.toLowerCase();
  if (playerInput === 'rock' || playerInput === 'paper' || playerInput === 'scissors') {
    return playerInput;
  } else {
    console.log('Invalid choice!');
  }
};
console.log(getPlayerSelection('Paper'));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The AJAX status is now 0, with a ready state of 4

Struggling to execute an AJAX call (using only JavaScript) to store a user in the database. The JavaScript file I am working with includes this code: var url = "interfata_db.php"; xmlhttp.onreadystatechange = function(){ alert('ready state &apos ...

Is there a way to automate downloading a file in Angular using the browser's built-in download feature?

When I make a request to my webservice to download a zip file, the file content is downloaded secretly and suddenly appears in the download task bar as already fully downloaded (100%). I am using the following method in Angular: const endpoint = "http:// ...

Resource for building user interface components in Javascript

I need assistance with implementing a feature that involves scrolling through different text blocks and corresponding images. Users should be able to select a specific text block and have the associated image on the right scroll into view, similar to a car ...

Issue with Vue canvas $ref not being defined on resize causing error, but oddly still seems to function correctly

Within my application, there exists a tabbed interface. One of the tabs contains a canvas element that dynamically adjusts its size to fit the window whenever it is resized. The functionality works seamlessly at first, but upon switching tabs and returning ...

Is there a way to implement field validation in a Vue wizard form?

Trying to implement form validation using Joi in a Vue wizard form, but not sure how to set it up correctly. The objective is to control the fields before progressing to the next and final page using the next() method. I want to keep the simplicity of th ...

Tips for maximizing the benefits of debounce/throttle and having a truly dynamic experience

Attempting to implement a similar concept in Vue: props(){ debouncing: {type: Number, default: 0} }, methods: { clicked: _.debounce(function() { this.$emit('click'); }, this.debouncing), } Unfortunately, the code breaks when ...

troubleshooting Axios errors in React applications

User/Project.js: import React, { useState, useEffect } from "react"; import Axios from "axios"; const Project = () => { const [projectName, setprojectName] = useState(""); const [projectDescription, setprojectDescriptio ...

Creating an HTML Canvas using an AngularJS template

I am facing an issue with rendering html elements on a canvas using html2canvas JS. I am utilizing AngularJS for data binding on the html page, but unfortunately, the dynamic data is not showing up on the canvas generated from these html elements. For inst ...

How to choose the second anchor element in a list with Protractor

I am facing a challenge where I need to click on the 2nd anchor tag in a list of anchor tags. <ul id="shortcuts"> <li><a ui-sref="app.journeyExplorer" href="#/journey-explorer/"><span class="ng-binding">1</span></a> ...

Angular 12: Running ng test shows code coverage error - TypeError: Unable to access 'initialize' property as undefined

I encountered an error in the code coverage console: TypeError: Cannot read properties of undefined (reading 'initialize') I am trying to call a service method from the component.ts file The code in the component.ts file looks like: this.myAuth ...

The message from Vee-validate indicates that the validator 'required_if' does not exist within the system

I'm currently implementing vee-validate version 3 with Vue 2.7 in my project. Specifically, this is the entry in my package.json file for vee-validate: "vee-validate": "^3.4.5", My issue lies with getting the required_if rule to f ...

The controller doesn't recognize "Factory" as a valid function

Attempting to create a factory and implement it in one of my controllers, I encountered an issue: Issue: TypeError: wordsToTrain is not a function Despite the simplicity of my controller and factory, I am still puzzled by the reason for this error: an ...

Manipulating the DOM using a Flask route in an HTML form

I have a form that allows users to input the names of "Player A" and "Player B". When the user clicks on the "Start Game" button, the game begins by redirecting to a Flask route (/players). I want the "player-text" section to be updated with the player nam ...

What is the process for triggering data-dismiss after the href has been activated?

Could someone help me with this issue? <a class='btn btn-danger' href='page.html'>Delete</a> I am trying to activate the "data-dismiss" attribute after the "href" is activated. My initial attempt was using this code: < ...

Storing customer information securely on the server with the help of Node.js

After spending some time experimenting with Node.js on my local machine, I've realized that my understanding of HTTP requests and XHR objects is quite limited. One particular challenge I've encountered while using Node is figuring out how to effe ...

Tips for accessing a file in AngularJS

Can AngularJS be used to read files? I am interested in placing the file within an HTML5 canvas for cropping purposes. I was considering implementing a directive. Below is the JavaScript code that I plan to include in my directive: function readURL(input ...

Change the name of a file to a name chosen by the user while uploading it to an

I'm a beginner when it comes to node.js and I'm facing an issue that I couldn't find a solution for despite looking into various related topics. My problem involves using Node.js on the server side to upload a file and rename it based on a ...

What is the best approach for rendering HTML on a page both initially and dynamically as it is updated, given the heavy utilization of ajax?

One challenge faced by many web applications today is how to efficiently reuse html templates without redundancy when initially rendering a page and adding new content. What is the most effective approach for this? One option is to render the HTML page ...

Should mutators be encapsulated within a class contained in a JS Module for better code organization and maintainability?

In order to maximize functionality of our new product using JavaScript, we have implemented an Authentication module that manages a tokenPromise which is updated upon user logins or token refreshes. It seems imperative to allow for mutation in this process ...

The table disappears when there is no data available in the database

One of my challenges involves a table that displays data retrieved from a database. The code for this is as follows: <table class="table table-hover table-bordered" style="width:300px" id="contact"> <tbody data-bind="foreach:items"> ...