This question already has answers here:
How can I display letters using html table cells as colored pixels?
(2 answers)
Closed 7 years ago.
Quick question. I am trying to give out numbers with a for loop in JavaScript.
Unfortunately this is not working, if I would code in java then the solution would be replace var with char and cosole.log with println and got it, but hereā¦ do you have a solution for that ?
for ( var i = 'a'; i < 'z'; i++) {
console.log(i);
}
for (var i = 'a'; i !== nextChar('z'); i = nextChar(i)) {
console.log(i);
}
function nextChar(c) {
return String.fromCharCode(c.charCodeAt(0) + 1);
}
Related
This question already has answers here:
For loop through Array only shows last value
(2 answers)
Closed 12 months ago.
// program to display text 10 times
const n = 10;
// looping from i = 1 to 10
for (let i = 1; i <= n; i++) {
console.log(`I love JavaScript.`);
}
I try to replace console.log with document.getElementById("demo").innerHTML = "I love JavaScript", but can't replicate the example.
You have to use += "I love JavaScript."
This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
How to get first character of string?
(22 answers)
How can I get last characters of a string
(25 answers)
Closed 3 years ago.
I'm ought to do it without loops. with "if" and "else".
The user writes a random word in the text field at the html and the function needs to check if the first and last letters are equals.
If they are equal - the function will return the string without the first and the last characters.
if the characters are not equal - the function will return the written string by the user.
unction firstLastletter(){
let k = document.getElementsByClassName('text').value;
let other = 'changed the string';
if(k.slice(0) == k.slice(-1)){
return console.log(k);
}
else {
return console.log(other);
}
}
i know that i miss a lot.
but i have no clue how to implement the code in a right way.
many thanks..
Something like this?
function firstlastletter(s) {
var first = s.charAt(0);
var last = s.charAt(s.length-1);
if(first == last) return s.substring(1, s.length-1)
else return s;
}
https://jsfiddle.net/mte97hg1/19/
This question already has answers here:
Repeat String - Javascript [duplicate]
(30 answers)
Closed 6 years ago.
I'f you're familiar with Python, I'm sure you're aware that you can multiply a String by an Integer to get that desired amount of Strings back.
Example:
'J' * 3 --> 'JJJ'
What's the most efficient way to do this in JavaScript?
I was looking for an inline method; similar to that of Pythons behaviour
A few of my ideas:
var str = 'J',
strOld = str,
timesToExtend = 3;
for(var i = 0; i < timesToExtend; i++){
str += strOld;
}
= 'JJJ'
var str = 'J',
timesToExtend = 5,
strOld = str;
while(!timesToExtend){
str += strOld;
timesToExtend--;
}
These are just rough ideas, so don't expect them to be 100% accurate and working. I assume this MUST contain a loop; however any method without a loop will be praised!
Thanks for reading; thank you for your response in advance!
In ES6 you can use Array.prototype.fill() and then join the array:
Array(3).fill('J').join('');
This question already has answers here:
String Conversion in Javascript (Decimal to Binary)
(5 answers)
Closed 7 years ago.
I would like to ask help whether am I doing the right thing or not. You see I am trying to test myself by displaying the bit pattern of a number in the most efficient way as possible. But I'm having trouble on how to display the pattern cause I'm still learning javascript. Here's my code.
<script>
var bitPattern = function(given) {
for(var i = 1 << 31; i > 0; i = i / 2){
document.write((given & i) ? 1 : 0);
}
};
var number = prompt("Enter a number to convert: ");
bitPattern(number);
</script>
The best way to do this is:
var number = prompt("Enter a number to convert: ");
var bitPattern = parseInt(number).toString(2);
document.write(bitPattern);
This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 7 years ago.
I have code to take the input of a text box and tell the user whether it is a Palindrome or not. What codes do you use to take that word and show the reversed spelling? Here is my current code:
<script type="text/javascript" language="javascript">
function checkPalindrome()
{
var revStr = "";
var str = document.info.string.value;
var i = str.length;
for(var j=i; j>=0; j--)
{
revStr = revStr+str.charAt(j);
}
if(str == revStr)
{
window.alert(str+" is Palindrome");
}
else
{
window.alert(str+" is not a Palindrome");
}
}
</script>
str.split('').reverse().join('')