get help in general - we have more specialized help rooms here: https://gitter.im/FreeCodeCamp/home
Hi guys! I’m on the task “Check for palindromes”. It asks me to remove all the non-alphanumeric characters and turn things into lower case in order to check for palindromes. Here’re the codes I’ve put but they don’t work…
function palindrome(str) {
// Good luck!
if (str.replace(/[^\w\s]/gi, '').toLowerCase())
return true;
else {
return false;
}
}
palindrome("not a palindrome”);
Does anyone know what’s wrong with my codes?
str.replace(/[^\w\s]/gi, '').toLowerCase()
then if the string reverse and it’s still spelt the same,
and it's returning true always because of this lineif (str.replace(/[^\w\s]/gi, '').toLowerCase())
this just checks if what's inside the if
is a truthy value, and unless you enter an empty string, it will always return true
Wo$rD
word
if(word)
is always true
var cleanedUp = str.replace(/[^\w\s]/gi, '').toLowerCase();
crystalclea sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6562 | @manish-giri |http://www.freecodecamp.com/manish-giri
function pairwise(arr, arg) {
if (arr.length == 0) return 0;
var objarr = [];
var final = 0;
for (i=0;i<arr.length;i++) {
objarr.push({
"id":i,
"value":arr[i],
"canused": true
});
}
for (j=0;j<arr.length;j++) {
if (objarr[j].canused) {
for (k=1;k<arr.length;k++) {
if (objarr[j].canused && objarr[k].canused && objarr[j].value + objarr[k].value == arg) {
final = final + j+k;
objarr[j].canused = objarr[k].canused = false;
console.log(objarr[j].canused + objarr[k].canused);
}
}
}
}
return final;
}
pairwise([1, 1, 1], 2);
ericmiller777 sends brownie points to @gersho :sparkles: :thumbsup: :sparkles:
:cookie: 443 | @gersho |http://www.freecodecamp.com/gersho
darrenfj sends brownie points to @thekholm80 :sparkles: :thumbsup: :sparkles:
:star2: 1480 | @thekholm80 |http://www.freecodecamp.com/thekholm80
heroiczero sends brownie points to @darrenfj :sparkles: :thumbsup: :sparkles:
:star2: 2042 | @darrenfj |http://www.freecodecamp.com/darrenfj
darrenfj sends brownie points to @gersho and @heroiczero :sparkles: :thumbsup: :sparkles:
:cookie: 444 | @gersho |http://www.freecodecamp.com/gersho
:star2: 1760 | @heroiczero |http://www.freecodecamp.com/heroiczero
gersho sends brownie points to @darrenfj :sparkles: :thumbsup: :sparkles:
:star2: 2043 | @darrenfj |http://www.freecodecamp.com/darrenfj
manish-giri sends brownie points to @darrenfj :sparkles: :thumbsup: :sparkles:
:star2: 2044 | @darrenfj |http://www.freecodecamp.com/darrenfj
heroiczero sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6563 | @manish-giri |http://www.freecodecamp.com/manish-giri
gersho sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6564 | @manish-giri |http://www.freecodecamp.com/manish-giri
manish-giri sends brownie points to @heroiczero :sparkles: :thumbsup: :sparkles:
:star2: 1761 | @heroiczero |http://www.freecodecamp.com/heroiczero
function sumPrimes(num) {
var arr = [];
var newArr;
for (var i =2; i<= num; i++){
arr.push(i);
}
for (var x = 2; x < arr.length; x++){
var m = Math.sqrt(arr[x]);
for (var y = 2; y<= m; y++){
if (arr[x]%y === 0){
arr.splice(x,1);
}
}
}
console.log(newArr);
return arr;
}
sumPrimes(20); //[2,3,5,7,9,11,13,16,17,19]
I need help understanding why 9 and 16 have shown up where they aren't wanted... based on the for(y) loop m = 3, which should splice out on the second running when 3%3 = 0, right?
var m = Math.sqrt(arr[x]);
arr[x]
... for the example of 9: m=3
meaning 'for(y)would test 2 and 3. giving
if (9%3 == 0) {splice(9)}`
rscales02 sends brownie points to @korzo :sparkles: :thumbsup: :sparkles:
:cookie: 530 | @korzo |http://www.freecodecamp.com/korzo
function isPrime(){}
? Or is that built in?
function isPrime(){}
Because you have to return sum of primes.
If you extract
var m = Math.sqrt(arr[x]);
for (var y = 2; y<= m; y++){
if (arr[x]%y === 0){
arr.splice(x,1);
}
}
to function and the just test if arr[x] is prime and add it to sum
var x = ;
true
and false
results could be turned back on the sumPrime()
function usefully...
function sumPrimes(num) {
var arr = [];
var newArr;
for (var i = 2; i <= num; i++) {
arr.push(i);
}
for (var x = 2; x < arr.length; x++) {
if (isPrime() === false) {
arr.splice(x, 1);
x--;
}
}
function isPrime() {
var m = Math.sqrt(arr[x]);
for (var y = 2; y <= m; y++) {
if (arr[x] % y === 0) {
return false;
} else {
return true;
}
}
}
console.log(arr);
}
sumPrimes(20);
something like that?
if return else return
inside a loop or it won't loop - your true condition is probably not valid until the loop completes
function isPrime(v) {
var m = Math.sqrt(v);
function sumPrimes(num) {
var arr = [];
var newArr;
for (var i =2; i<= num; i++){
arr.push(i);
}
return arr.reduce((a,e) => isPrime(e) ? a+e : a , 0);
}
function isPrime(n) {
var m = Math.sqrt(n);
for (var y = 2; y<= m; y++){
if (n%y === 0){
return false;
}
}
return true;
}
function sumPrimes(num) {
var sum = 0;
for (var i =2; i<= num; i++){
if(isPrime(i)) {
sum +=i;
}
}
return sum;
}
function isPrime(n) {
var m = Math.sqrt(n);
for (var y = 2; y<= m; y++){
if (n%y === 0){
return false;
}
}
return true;
}
if(isPrime(i) == true
?
:cookie: 531 | @korzo |http://www.freecodecamp.com/korzo
rscales02 sends brownie points to @korzo :sparkles: :thumbsup: :sparkles:
start sum at zero
for each number from 2 to num
if number isPrime
add number to sum
rscales02 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8475 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
([0, height]);
.range([0, height]);
artbohr sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8476 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
return a
and the result is d
, but when I try return a.match(/a-z/)
it returns null
shouldn't it return 0
since the string is only one letter long?
@artbohr you're using
const y = d3.scaleLinear()
.domain([0,20000])
.range([height,0]);
Which means when you pass a value to the scaling function you're going to return a number for the range. So if 0 goes in, height
will come out. You just need to swap that
return word !== array[i];
only inspect the 2nd argument and not the 3rd argument. I can't think of any other ways for the filter to check the third argument. Any help is appreciated!function destroyer(arr) {
// Remove all the values
var array = [];
for(var i = 1; i<arguments.length; i++) {
array.push(arguments[i]);
}
return arr.filter(function(word){
for(var i = 0; i < arguments.length; i++) {
// console.log(word !== arguments[i]);
return word !== array[i];
}
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
.attr("height", d => {
return height - y(d[1]);
})
...
.attr("y", d => {
return y(d[1]);
});
artbohr sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
:star2: 1220 | @cmccormack |http://www.freecodecamp.com/cmccormack
.domain(d3.extent(data.data, (d)=>d[1]))
artbohr sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
cmccormack sends brownie points to @artbohr :sparkles: :thumbsup: :sparkles:
:cookie: 309 | @artbohr |http://www.freecodecamp.com/artbohr
quickspeedy sends brownie points to @alexanderkopke :sparkles: :thumbsup: :sparkles:
:star2: 1657 | @alexanderkopke |http://www.freecodecamp.com/alexanderkopke
How to center a div, and to be responsive at the same time? plss
pleaseeee <3
margin-left: auto;
margin-right: auto;
max-width: 100%;
var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
// this.getFullName = function() {
// return "";
// };
var first = firstAndLast.split(' ')[0];
var last = firstAndLast.split(' ')[1];
var fullName = first + ' ' + last;
return {
getFullName: function() {
return fullName;
},
getFirstName: function() {
return first;
},
getLastName: function() {
return last;
},
setFirstName: function(newFirst) {
first = newFirst;
},
setLastName: function(newLast) {
last = newLast;
},
setFullName: function(newFullname) {
firstAndLast = newFullname;
}
};
};
var bob = new Person('Bob Ross');
bob.setFirstName('check');
bob.getFullName();
margin: 0 auto;
getFullName: function(){
fullName = first + ' ' +last;
return fullName;
}
function destroyer(arr) {
// Remove all the values
var args = Array.prototype.slice.call(arguments);
var newArray;
newArray=arr.filter(function(arr){
for (var i=0; i < arr.length; i++) {for (var j=1; j < args.length; j++)
return arr[i] != args[j]; }
});
return args[1];
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
YDKJS
book. That would be a good option and infact that is quite interesting as well